package com.ea.nimble; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; /* loaded from: classes2.dex */ public class ByteBufferIOStream { protected static final int SEGMENT_SIZE = 4096; protected int m_availableSegment; protected LinkedList m_buffer; protected boolean m_closed; protected ByteBufferInputStream m_input; protected ByteBufferOutputStream m_output; protected int m_readPosition; protected int m_writePosition; public void clear() { this.m_closed = false; this.m_availableSegment = 0; this.m_writePosition = 0; this.m_readPosition = 0; } public void closeIOStream() { this.m_closed = true; } public InputStream getInputStream() { return this.m_input; } public OutputStream getOutputStream() { return this.m_output; } public ByteBufferIOStream() { this(1); } public ByteBufferIOStream(int i) { this.m_closed = false; this.m_availableSegment = 0; this.m_writePosition = 0; this.m_readPosition = 0; this.m_buffer = new LinkedList<>(); this.m_input = new ByteBufferInputStream(); this.m_output = new ByteBufferOutputStream(); int i2 = (((i <= 0 ? 1 : i) - 1) / 4096) + 1; for (int i3 = 0; i3 < i2; i3++) { this.m_buffer.add(new byte[4096]); } } public int available() throws IOException { return this.m_input.available(); } public byte[] prepareSegment() { if (this.m_availableSegment + 1 >= this.m_buffer.size()) { return new byte[4096]; } if (this.m_buffer.size() == 0) { return null; } return this.m_buffer.removeLast(); } public void appendSegmentToBuffer(byte[] bArr, int i) throws IOException { if (this.m_writePosition == 0 && bArr.length == 4096) { ListIterator listIterator = this.m_buffer.listIterator(); for (int i2 = 0; i2 < this.m_availableSegment; i2++) { listIterator.next(); } listIterator.add(bArr); if (i != 4096) { this.m_writePosition = i; return; } else { this.m_availableSegment++; return; } } getOutputStream().write(bArr, 0, i); } public byte[] growBufferBySegment() throws IOException { if (this.m_writePosition != 0) { throw new IOException("Bad location to grow buffer"); } ListIterator listIterator = this.m_buffer.listIterator(); for (int i = 0; i < this.m_availableSegment; i++) { listIterator.next(); } byte[] bArr = new byte[4096]; listIterator.add(bArr); this.m_availableSegment++; return bArr; } public class ByteBufferInputStream extends InputStream { @Override // java.io.InputStream public boolean markSupported() { return false; } public ByteBufferInputStream() { } @Override // java.io.InputStream public int available() throws IOException { ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; if (byteBufferIOStream.m_closed) { throw new IOException("ByteBufferIOStream is closed"); } return ((byteBufferIOStream.m_availableSegment * 4096) + byteBufferIOStream.m_writePosition) - byteBufferIOStream.m_readPosition; } @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { ByteBufferIOStream.this.closeIOStream(); } @Override // java.io.InputStream public int read(byte[] bArr) throws IOException { return read(bArr, 0, bArr.length); } @Override // java.io.InputStream public int read() throws IOException { if (available() <= 0) { throw new IOException("Nothing to read in ByteBufferIOStream"); } byte[] first = ByteBufferIOStream.this.m_buffer.getFirst(); ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; int i = byteBufferIOStream.m_readPosition; byte b = first[i]; int i2 = i + 1; byteBufferIOStream.m_readPosition = i2; if (i2 >= 4096) { LinkedList linkedList = byteBufferIOStream.m_buffer; linkedList.add(linkedList.poll()); ByteBufferIOStream.this.m_readPosition = 0; r1.m_availableSegment--; } return b; } @Override // java.io.InputStream public int read(byte[] bArr, int i, int i2) throws IOException { if (i < 0 || i2 < 0 || i + i2 > bArr.length) { throw new IndexOutOfBoundsException("The reading range of out of buffer boundary."); } int available = available(); if (available <= 0) { return -1; } if (i2 > available) { i2 = available; } ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; int i3 = 4096 - byteBufferIOStream.m_readPosition; if (i2 < i3) { System.arraycopy(byteBufferIOStream.m_buffer.getFirst(), ByteBufferIOStream.this.m_readPosition, bArr, i, i2); ByteBufferIOStream.this.m_readPosition += i2; } else { System.arraycopy(byteBufferIOStream.m_buffer.getFirst(), ByteBufferIOStream.this.m_readPosition, bArr, i, i3); LinkedList linkedList = ByteBufferIOStream.this.m_buffer; linkedList.add(linkedList.poll()); int i4 = i2 - i3; int i5 = i + i3; int i6 = i4 / 4096; for (int i7 = 0; i7 < i6; i7++) { System.arraycopy(ByteBufferIOStream.this.m_buffer.getFirst(), 0, bArr, i5, 4096); LinkedList linkedList2 = ByteBufferIOStream.this.m_buffer; linkedList2.add(linkedList2.poll()); i4 -= 4096; i5 += 4096; } System.arraycopy(ByteBufferIOStream.this.m_buffer.getFirst(), 0, bArr, i5, i4); ByteBufferIOStream byteBufferIOStream2 = ByteBufferIOStream.this; byteBufferIOStream2.m_readPosition = i4; byteBufferIOStream2.m_availableSegment -= i6 + 1; } return i2; } @Override // java.io.InputStream public long skip(long j) throws IOException { int available = available(); if (available <= 0) { return 0L; } long j2 = available; if (j > j2) { j = j2; } int i = (int) j; ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; int i2 = byteBufferIOStream.m_readPosition; int i3 = 4096 - i2; if (i < i3) { byteBufferIOStream.m_readPosition = i2 + i; } else { LinkedList linkedList = byteBufferIOStream.m_buffer; linkedList.add(linkedList.poll()); int i4 = i - i3; int i5 = i4 / 4096; for (int i6 = 0; i6 < i5; i6++) { LinkedList linkedList2 = ByteBufferIOStream.this.m_buffer; linkedList2.add(linkedList2.poll()); i4 -= 4096; } ByteBufferIOStream byteBufferIOStream2 = ByteBufferIOStream.this; byteBufferIOStream2.m_readPosition = i4; byteBufferIOStream2.m_availableSegment -= i5 + 1; } return j; } } public class ByteBufferOutputStream extends OutputStream { public ByteBufferOutputStream() { } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { ByteBufferIOStream.this.closeIOStream(); } @Override // java.io.OutputStream public void write(byte[] bArr, int i, int i2) throws IOException { byte[] bArr2; if (i < 0 || i2 < 0 || i + i2 > bArr.length) { throw new IndexOutOfBoundsException("The writing range is out of buffer boundary."); } ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; if (byteBufferIOStream.m_closed) { throw new IOException("ByteBufferIOStream is closed"); } int i3 = 4096 - byteBufferIOStream.m_writePosition; Iterator it = byteBufferIOStream.m_buffer.iterator(); for (int i4 = 0; i4 < ByteBufferIOStream.this.m_availableSegment; i4++) { it.next(); } if (i2 < i3) { System.arraycopy(bArr, i, it.next(), ByteBufferIOStream.this.m_writePosition, i2); ByteBufferIOStream.this.m_writePosition += i2; return; } System.arraycopy(bArr, i, it.next(), ByteBufferIOStream.this.m_writePosition, i3); int i5 = i2 - i3; int i6 = i + i3; ByteBufferIOStream byteBufferIOStream2 = ByteBufferIOStream.this; byteBufferIOStream2.m_availableSegment++; byteBufferIOStream2.m_writePosition = 0; while (i5 > 0) { if (it.hasNext()) { bArr2 = it.next(); } else { bArr2 = new byte[4096]; ByteBufferIOStream.this.m_buffer.add(bArr2); } if (i5 < 4096) { System.arraycopy(bArr, i6, bArr2, 0, i5); ByteBufferIOStream.this.m_writePosition = i5; i5 = 0; } else { System.arraycopy(bArr, i6, bArr2, 0, 4096); i5 -= 4096; i6 += 4096; ByteBufferIOStream.this.m_availableSegment++; } } } @Override // java.io.OutputStream public void write(byte[] bArr) throws IOException { write(bArr, 0, bArr.length); } @Override // java.io.OutputStream public void write(int i) throws IOException { ByteBufferIOStream byteBufferIOStream = ByteBufferIOStream.this; if (byteBufferIOStream.m_closed) { throw new IOException("ByteBufferIOStream is closed"); } byte[] first = byteBufferIOStream.m_buffer.getFirst(); ByteBufferIOStream byteBufferIOStream2 = ByteBufferIOStream.this; int i2 = byteBufferIOStream2.m_writePosition; first[i2] = (byte) i; int i3 = i2 + 1; byteBufferIOStream2.m_writePosition = i3; if (i3 == 4096) { byteBufferIOStream2.m_writePosition = 0; byteBufferIOStream2.m_availableSegment++; } } } }