// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry.instrumentation.file;

import io.sentry.SentryOptions;
import java.io.Closeable;
import java.io.IOException;
import io.sentry.ISpan;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import io.sentry.IScopes;
import io.sentry.ScopesAdapter;
import java.io.File;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import java.io.FileOutputStream;

public final class SentryFileOutputStream extends FileOutputStream
{
    @NotNull
    private final FileOutputStream delegate;
    @NotNull
    private final FileIOSpanManager spanManager;
    
    public SentryFileOutputStream(@Nullable final String name) throws FileNotFoundException {
        this((name != null) ? new File(name) : null, false, ScopesAdapter.getInstance());
    }
    
    public SentryFileOutputStream(@Nullable final String name, final boolean append) throws FileNotFoundException {
        this(init((name != null) ? new File(name) : null, append, null, ScopesAdapter.getInstance()));
    }
    
    public SentryFileOutputStream(@Nullable final File file) throws FileNotFoundException {
        this(file, false, ScopesAdapter.getInstance());
    }
    
    public SentryFileOutputStream(@Nullable final File file, final boolean append) throws FileNotFoundException {
        this(init(file, append, null, ScopesAdapter.getInstance()));
    }
    
    public SentryFileOutputStream(@NotNull final FileDescriptor fdObj) {
        this(init(fdObj, null, ScopesAdapter.getInstance()), fdObj);
    }
    
    SentryFileOutputStream(@Nullable final File file, final boolean append, @NotNull final IScopes scopes) throws FileNotFoundException {
        this(init(file, append, null, scopes));
    }
    
    private SentryFileOutputStream(@NotNull final FileOutputStreamInitData data, @NotNull final FileDescriptor fd) {
        super(fd);
        this.spanManager = new FileIOSpanManager(data.span, data.file, data.options);
        this.delegate = data.delegate;
    }
    
    private SentryFileOutputStream(@NotNull final FileOutputStreamInitData data) throws FileNotFoundException {
        super(getFileDescriptor(data.delegate));
        this.spanManager = new FileIOSpanManager(data.span, data.file, data.options);
        this.delegate = data.delegate;
    }
    
    private static FileOutputStreamInitData init(@Nullable final File file, final boolean append, @Nullable FileOutputStream delegate, @NotNull final IScopes scopes) throws FileNotFoundException {
        final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write");
        if (delegate == null) {
            delegate = new FileOutputStream(file, append);
        }
        return new FileOutputStreamInitData(file, append, span, delegate, scopes.getOptions());
    }
    
    private static FileOutputStreamInitData init(@NotNull final FileDescriptor fd, @Nullable FileOutputStream delegate, @NotNull final IScopes scopes) {
        final ISpan span = FileIOSpanManager.startSpan(scopes, "file.write");
        if (delegate == null) {
            delegate = new FileOutputStream(fd);
        }
        return new FileOutputStreamInitData(null, false, span, delegate, scopes.getOptions());
    }
    
    @Override
    public void write(final int b) throws IOException {
        this.spanManager.performIO(() -> {
            this.delegate.write(b);
            return Integer.valueOf(1);
        });
    }
    
    @Override
    public void write(final byte[] b) throws IOException {
        this.spanManager.performIO(() -> {
            this.delegate.write(b);
            return Integer.valueOf(b.length);
        });
    }
    
    @Override
    public void write(final byte[] b, final int off, final int len) throws IOException {
        this.spanManager.performIO(() -> {
            this.delegate.write(b, off, len);
            return Integer.valueOf(len);
        });
    }
    
    @Override
    public void close() throws IOException {
        this.spanManager.finish(this.delegate);
        super.close();
    }
    
    private static FileDescriptor getFileDescriptor(@NotNull final FileOutputStream stream) throws FileNotFoundException {
        try {
            return stream.getFD();
        }
        catch (final IOException error) {
            throw new FileNotFoundException("No file descriptor");
        }
    }
    
    public static final class Factory
    {
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @Nullable final String name) throws FileNotFoundException {
            final IScopes scopes = ScopesAdapter.getInstance();
            FileOutputStream fileOutputStream;
            if (isTracingEnabled(scopes)) {
                final SentryFileOutputStream sentryFileOutputStream;
                fileOutputStream = sentryFileOutputStream;
                sentryFileOutputStream = new SentryFileOutputStream(init((name != null) ? new File(name) : null, false, delegate, ScopesAdapter.getInstance()), (SentryFileOutputStream$1)null);
            }
            else {
                fileOutputStream = delegate;
            }
            return fileOutputStream;
        }
        
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @Nullable final String name, final boolean append) throws FileNotFoundException {
            final IScopes scopes = ScopesAdapter.getInstance();
            FileOutputStream fileOutputStream;
            if (isTracingEnabled(scopes)) {
                final SentryFileOutputStream sentryFileOutputStream;
                fileOutputStream = sentryFileOutputStream;
                sentryFileOutputStream = new SentryFileOutputStream(init((name != null) ? new File(name) : null, append, delegate, ScopesAdapter.getInstance()), (SentryFileOutputStream$1)null);
            }
            else {
                fileOutputStream = delegate;
            }
            return fileOutputStream;
        }
        
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @Nullable final File file) throws FileNotFoundException {
            final IScopes scopes = ScopesAdapter.getInstance();
            return isTracingEnabled(scopes) ? new SentryFileOutputStream(init(file, false, delegate, ScopesAdapter.getInstance()), (SentryFileOutputStream$1)null) : delegate;
        }
        
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @Nullable final File file, final boolean append) throws FileNotFoundException {
            final IScopes scopes = ScopesAdapter.getInstance();
            return isTracingEnabled(scopes) ? new SentryFileOutputStream(init(file, append, delegate, ScopesAdapter.getInstance()), (SentryFileOutputStream$1)null) : delegate;
        }
        
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @NotNull final FileDescriptor fdObj) {
            final IScopes scopes = ScopesAdapter.getInstance();
            return isTracingEnabled(scopes) ? new SentryFileOutputStream(init(fdObj, delegate, ScopesAdapter.getInstance()), fdObj, null) : delegate;
        }
        
        public static FileOutputStream create(@NotNull final FileOutputStream delegate, @Nullable final File file, @NotNull final IScopes scopes) throws FileNotFoundException {
            return isTracingEnabled(scopes) ? new SentryFileOutputStream(init(file, false, delegate, scopes), (SentryFileOutputStream$1)null) : delegate;
        }
        
        private static boolean isTracingEnabled(@NotNull final IScopes scopes) {
            final SentryOptions options = scopes.getOptions();
            return options.isTracingEnabled();
        }
    }
}
