what-the-bank/sources/org/jmrtd/io/InputStreamBuffer.java

229 lines
8.0 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.jmrtd.io;
import com.google.common.primitives.UnsignedBytes;
import java.io.IOException;
import java.io.InputStream;
import org.jmrtd.io.FragmentBuffer;
/* loaded from: classes6.dex */
public class InputStreamBuffer {
public FragmentBuffer buffer;
private PositionInputStream carrier;
public InputStreamBuffer(InputStream inputStream, int i) {
PositionInputStream positionInputStream = new PositionInputStream(inputStream);
this.carrier = positionInputStream;
positionInputStream.mark(i);
this.buffer = new FragmentBuffer(i);
}
public void updateFrom(InputStreamBuffer inputStreamBuffer) {
this.buffer.updateFrom(inputStreamBuffer.buffer);
}
public SubInputStream getInputStream() {
SubInputStream subInputStream;
synchronized (this.carrier) {
subInputStream = new SubInputStream(this, this.carrier);
}
return subInputStream;
}
public int getPosition() {
int position;
synchronized (this) {
position = this.buffer.getPosition();
}
return position;
}
public int getBytesBuffered() {
int bytesBuffered;
synchronized (this) {
bytesBuffered = this.buffer.getBytesBuffered();
}
return bytesBuffered;
}
public int getLength() {
return this.buffer.getLength();
}
public String toString() {
StringBuilder sb = new StringBuilder("InputStreamBuffer [");
sb.append(this.buffer);
sb.append("]");
return sb.toString();
}
/* loaded from: classes6.dex */
public class SubInputStream extends InputStream {
static final boolean $assertionsDisabled = false;
private Object syncObject;
final InputStreamBuffer this$0;
private int position = 0;
private int markedPosition = -1;
@Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
public void close() throws IOException {
}
@Override // java.io.InputStream
public boolean markSupported() {
return true;
}
public SubInputStream(InputStreamBuffer inputStreamBuffer, Object obj) {
this.this$0 = inputStreamBuffer;
this.syncObject = obj;
}
public FragmentBuffer getBuffer() {
return this.this$0.buffer;
}
@Override // java.io.InputStream
public int read() throws IOException {
synchronized (this.syncObject) {
if (this.position >= this.this$0.buffer.getLength()) {
return -1;
}
if (!this.this$0.buffer.isCoveredByFragment(this.position)) {
if (this.this$0.carrier.markSupported()) {
syncCarrierPosition(this.position);
}
try {
int read = this.this$0.carrier.read();
if (read < 0) {
return -1;
}
FragmentBuffer fragmentBuffer = this.this$0.buffer;
int i = this.position;
this.position = i + 1;
fragmentBuffer.addFragment(i, (byte) read);
return read;
} catch (IOException e) {
throw e;
}
}
byte[] buffer = this.this$0.buffer.getBuffer();
int i2 = this.position;
this.position = i2 + 1;
return buffer[i2] & UnsignedBytes.MAX_VALUE;
}
}
@Override // java.io.InputStream
public int read(byte[] bArr) throws IOException {
int read;
synchronized (this.syncObject) {
read = read(bArr, 0, bArr.length);
}
return read;
}
@Override // java.io.InputStream
public int read(byte[] bArr, int i, int i2) throws IOException {
synchronized (this.syncObject) {
if (bArr == null) {
throw new NullPointerException();
}
if (i < 0 || i2 < 0 || i2 > bArr.length - i) {
throw new IndexOutOfBoundsException();
}
if (i2 == 0) {
return 0;
}
if (i2 > this.this$0.buffer.getLength() - this.position) {
i2 = this.this$0.buffer.getLength() - this.position;
}
if (this.position >= this.this$0.buffer.getLength()) {
return -1;
}
if (this.this$0.carrier.markSupported()) {
syncCarrierPosition(this.position);
}
FragmentBuffer.Fragment smallestUnbufferedFragment = this.this$0.buffer.getSmallestUnbufferedFragment(this.position, i2);
if (smallestUnbufferedFragment.getLength() > 0) {
int offset = smallestUnbufferedFragment.getOffset() - this.position;
int length = smallestUnbufferedFragment.getLength();
System.arraycopy(this.this$0.buffer.getBuffer(), this.position, bArr, i, offset);
this.position += offset;
if (this.this$0.carrier.markSupported()) {
syncCarrierPosition(this.position);
}
int i3 = i + offset;
int read = this.this$0.carrier.read(bArr, i3, length);
this.this$0.buffer.addFragment(smallestUnbufferedFragment.getOffset(), bArr, i3, read);
this.position += read;
return offset + read;
}
int min = Math.min(i2, this.this$0.buffer.getLength() - this.position);
System.arraycopy(this.this$0.buffer.getBuffer(), this.position, bArr, i, min);
this.position += min;
return min;
}
}
@Override // java.io.InputStream
public long skip(long j) throws IOException {
long skip;
synchronized (this.syncObject) {
int bufferedLength = this.this$0.buffer.getBufferedLength(this.position);
long j2 = bufferedLength;
if (j <= j2) {
this.position = (int) (this.position + j);
return j;
}
this.position += bufferedLength;
if (this.this$0.carrier.markSupported()) {
syncCarrierPosition(this.position);
skip = this.this$0.carrier.skip(j - j2);
this.position += (int) skip;
} else {
skip = super.skip(j - j2);
}
return j2 + skip;
}
}
@Override // java.io.InputStream
public int available() throws IOException {
return this.this$0.buffer.getBufferedLength(this.position);
}
@Override // java.io.InputStream
public void mark(int i) {
synchronized (this) {
this.markedPosition = this.position;
}
}
@Override // java.io.InputStream
public void reset() throws IOException {
synchronized (this) {
int i = this.markedPosition;
if (i < 0) {
throw new IOException("Invalid reset, was mark() called?");
}
this.position = i;
}
}
private void syncCarrierPosition(int i) throws IOException {
if (i == this.this$0.carrier.getPosition()) {
return;
}
this.this$0.carrier.reset();
int i2 = 0;
while (i2 < i) {
i2 = (int) (i2 + this.this$0.carrier.skip(i - i2));
}
}
public int getPosition() {
return this.position;
}
}
}