package IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9;

import IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
import android.system.Os;
import android.system.OsConstants;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/* compiled from: DownloadUriOutputStream.java */
/* loaded from: classes.dex */
public class QxJ9d_1 implements IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0 {
    public final ParcelFileDescriptor KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final BufferedOutputStream f188KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final FileOutputStream f189KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final FileChannel f190KQgRr5e_0;

    /* compiled from: DownloadUriOutputStream.java */
    /* loaded from: classes.dex */
    public static class KQgRr5e_0 implements KQgRr5e_0.InterfaceC0011KQgRr5e_0 {
        @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0.InterfaceC0011KQgRr5e_0
        public IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0 KQgRr5e_0(Context context, Uri uri, int i) throws FileNotFoundException {
            return new QxJ9d_1(context, uri, i);
        }

        @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0.InterfaceC0011KQgRr5e_0
        public boolean KQgRr5e_0() {
            return true;
        }
    }

    public QxJ9d_1(Context context, Uri uri, int i) throws FileNotFoundException {
        ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "rw");
        if (openFileDescriptor != null) {
            this.KQgRr5e_0 = openFileDescriptor;
            FileOutputStream fileOutputStream = new FileOutputStream(openFileDescriptor.getFileDescriptor());
            this.f189KQgRr5e_0 = fileOutputStream;
            this.f190KQgRr5e_0 = fileOutputStream.getChannel();
            this.f188KQgRr5e_0 = new BufferedOutputStream(this.f189KQgRr5e_0, i);
            return;
        }
        throw new FileNotFoundException("result of " + uri + " is null!");
    }

    @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0
    public void KQgRr5e_0(byte[] bArr, int i, int i2) throws IOException {
        this.f188KQgRr5e_0.write(bArr, i, i2);
    }

    @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0
    public void QxJ9d_1(long j) throws IOException {
        this.f190KQgRr5e_0.position(j);
    }

    @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0
    public void close() throws IOException {
        this.f188KQgRr5e_0.close();
        this.f189KQgRr5e_0.close();
        this.KQgRr5e_0.close();
    }

    @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0
    public void KQgRr5e_0() throws IOException {
        this.f188KQgRr5e_0.flush();
        this.KQgRr5e_0.getFileDescriptor().sync();
    }

    @Override // IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.XnFJ5_9.KQgRr5e_0
    public void KQgRr5e_0(long j) {
        if (Build.VERSION.SDK_INT >= 21) {
            try {
                Os.posix_fallocate(this.KQgRr5e_0.getFileDescriptor(), 0L, j);
                return;
            } catch (Throwable th) {
                if (th instanceof ErrnoException) {
                    int i = th.errno;
                    if (i == OsConstants.ENOSYS || i == OsConstants.ENOTSUP) {
                        IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.PGPF6hQ_2.QxJ9d_1("DownloadUriOutputStream", "fallocate() not supported; falling back to ftruncate()");
                        try {
                            Os.ftruncate(this.KQgRr5e_0.getFileDescriptor(), j);
                            return;
                        } catch (Throwable th2) {
                            IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.PGPF6hQ_2.QxJ9d_1("DownloadUriOutputStream", "It can't pre-allocate length(" + j + ") on the sdk version(" + Build.VERSION.SDK_INT + "), because of " + th2);
                            return;
                        }
                    }
                    return;
                }
                IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.PGPF6hQ_2.QxJ9d_1("DownloadUriOutputStream", "It can't pre-allocate length(" + j + ") on the sdk version(" + Build.VERSION.SDK_INT + "), because of " + th);
                return;
            }
        }
        IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.PGPF6hQ_2.QxJ9d_1("DownloadUriOutputStream", "It can't pre-allocate length(" + j + ") on the sdk version(" + Build.VERSION.SDK_INT + ")");
    }
}
