mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-12-06 20:39:26 +01:00
115 lines
3.9 KiB
Java
115 lines
3.9 KiB
Java
package com.facebook.common.memory;
|
|
|
|
import com.facebook.common.internal.Preconditions;
|
|
import com.facebook.common.logging.FLog;
|
|
import com.facebook.common.references.ResourceReleaser;
|
|
import com.facebook.infer.annotation.Nullsafe;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import javax.annotation.concurrent.NotThreadSafe;
|
|
import okio.Buffer$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
@NotThreadSafe
|
|
@Nullsafe(Nullsafe.Mode.LOCAL)
|
|
/* loaded from: classes6.dex */
|
|
public class PooledByteArrayBufferedInputStream extends InputStream {
|
|
private static final String TAG = "PooledByteInputStream";
|
|
private final byte[] mByteArray;
|
|
private final InputStream mInputStream;
|
|
private final ResourceReleaser<byte[]> mResourceReleaser;
|
|
private int mBufferedSize = 0;
|
|
private int mBufferOffset = 0;
|
|
private boolean mClosed = false;
|
|
|
|
public PooledByteArrayBufferedInputStream(InputStream inputStream, byte[] bArr, ResourceReleaser<byte[]> resourceReleaser) {
|
|
this.mInputStream = (InputStream) Preconditions.checkNotNull(inputStream);
|
|
this.mByteArray = (byte[]) Preconditions.checkNotNull(bArr);
|
|
this.mResourceReleaser = (ResourceReleaser) Preconditions.checkNotNull(resourceReleaser);
|
|
}
|
|
|
|
private boolean ensureDataInBuffer() throws IOException {
|
|
if (this.mBufferOffset < this.mBufferedSize) {
|
|
return true;
|
|
}
|
|
int read = this.mInputStream.read(this.mByteArray);
|
|
if (read <= 0) {
|
|
return false;
|
|
}
|
|
this.mBufferedSize = read;
|
|
this.mBufferOffset = 0;
|
|
return true;
|
|
}
|
|
|
|
private void ensureNotClosed() throws IOException {
|
|
if (this.mClosed) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m$2("stream already closed");
|
|
}
|
|
}
|
|
|
|
@Override // java.io.InputStream
|
|
public int available() throws IOException {
|
|
Preconditions.checkState(this.mBufferOffset <= this.mBufferedSize);
|
|
ensureNotClosed();
|
|
return this.mInputStream.available() + (this.mBufferedSize - this.mBufferOffset);
|
|
}
|
|
|
|
@Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
|
|
public void close() throws IOException {
|
|
if (this.mClosed) {
|
|
return;
|
|
}
|
|
this.mClosed = true;
|
|
this.mResourceReleaser.release(this.mByteArray);
|
|
super.close();
|
|
}
|
|
|
|
public void finalize() throws Throwable {
|
|
if (!this.mClosed) {
|
|
FLog.e(TAG, "Finalized without closing");
|
|
close();
|
|
}
|
|
super.finalize();
|
|
}
|
|
|
|
@Override // java.io.InputStream
|
|
public int read(byte[] bArr, int i, int i2) throws IOException {
|
|
Preconditions.checkState(this.mBufferOffset <= this.mBufferedSize);
|
|
ensureNotClosed();
|
|
if (!ensureDataInBuffer()) {
|
|
return -1;
|
|
}
|
|
int min = Math.min(this.mBufferedSize - this.mBufferOffset, i2);
|
|
System.arraycopy(this.mByteArray, this.mBufferOffset, bArr, i, min);
|
|
this.mBufferOffset += min;
|
|
return min;
|
|
}
|
|
|
|
@Override // java.io.InputStream
|
|
public long skip(long j) throws IOException {
|
|
Preconditions.checkState(this.mBufferOffset <= this.mBufferedSize);
|
|
ensureNotClosed();
|
|
int i = this.mBufferedSize;
|
|
int i2 = this.mBufferOffset;
|
|
long j2 = i - i2;
|
|
if (j2 >= j) {
|
|
this.mBufferOffset = (int) (i2 + j);
|
|
return j;
|
|
}
|
|
this.mBufferOffset = i;
|
|
return this.mInputStream.skip(j - j2) + j2;
|
|
}
|
|
|
|
@Override // java.io.InputStream
|
|
public int read() throws IOException {
|
|
Preconditions.checkState(this.mBufferOffset <= this.mBufferedSize);
|
|
ensureNotClosed();
|
|
if (!ensureDataInBuffer()) {
|
|
return -1;
|
|
}
|
|
byte[] bArr = this.mByteArray;
|
|
int i = this.mBufferOffset;
|
|
this.mBufferOffset = i + 1;
|
|
return bArr[i] & 255;
|
|
}
|
|
}
|