private ImageInputStream createBufferedInput(final ImageInputStream input) throws IOException { return input instanceof BufferedImageInputStream ? input : new BufferedImageInputStream(input); }
@Override public long readLong() throws IOException { readFully(integralCache.array(), 0, 8); return integralCache.getLong(0); }
@Override public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { checkClosed(); bitOffset = 0; if (!buffer.hasRemaining()) { // Bypass buffer if buffer is empty for reads longer than buffer if (pLength >= buffer.capacity()) { return readDirect(pBuffer, pOffset, pLength); } else { fillBuffer(); } } return readBuffered(pBuffer, pOffset, pLength); }
@Override public int read() throws IOException { checkClosed(); if (!buffer.hasRemaining()) { fillBuffer(); } if (!buffer.hasRemaining()) { return -1; } bitOffset = 0; streamPos++; return buffer.get() & 0xff; }
@Test public void testReadIntegralOnBufferBoundary() throws IOException { // Make sure we use the exact size of the buffer int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; fill(bytes, size - 4, size + 4, (byte) 0xff); // Create wrapper stream BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes)); // Read to fill the buffer, then seek to almost end of buffer assertEquals(0, stream.readInt()); stream.seek(size - 3); assertEquals(0xffffffff, stream.readInt()); assertEquals(size + 1, stream.getStreamPosition()); }
@Test public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; random.nextBytes(bytes); // Create wrapper stream BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes)); // Read to fill the buffer, then reset stream.readLong(); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; stream.readFully(result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); stream.readFully(result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
@Test public void testBufferPositionCorrect() throws IOException { // Fill bytes byte[] bytes = new byte[1024]; random.nextBytes(bytes); ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes); input.readByte(); input.readByte(); input.skipBytes(124); input.readByte(); input.readByte(); // Sanity check assertEquals(128, input.getStreamPosition()); BufferedImageInputStream stream = new BufferedImageInputStream(input); assertEquals(input.getStreamPosition(), stream.getStreamPosition()); stream.skipBytes(128); //assertTrue(256 <= input.getStreamPosition()); assertEquals(256, stream.getStreamPosition()); stream.seek(1020); assertEquals(1020, stream.getStreamPosition()); }
@Override public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { bitOffset = 0; if (!isBufferValid()) { // Bypass cache if cache is empty for reads longer than buffer if (pLength >= mBuffer.length) { return readDirect(pBuffer, pOffset, pLength); } else { fillBuffer(); } } return readBuffered(pBuffer, pOffset, pLength); }
@Override public int read(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException { bitOffset = 0; if (!buffer.hasRemaining()) { // Bypass cache if cache is empty for reads longer than buffer if (pLength >= buffer.capacity()) { return readDirect(pBuffer, pOffset, pLength); } else { fillBuffer(); } } return readBuffered(pBuffer, pOffset, pLength); }
@Override public int read() throws IOException { if (!isBufferValid()) { fillBuffer(); } if (mBufferLength <= 0) { return -1; } bitOffset = 0; streamPos++; return mBuffer[mBufferPos++] & 0xff; }
@Override public int read() throws IOException { if (!buffer.hasRemaining()) { fillBuffer(); } if (!buffer.hasRemaining()) { return -1; } bitOffset = 0; streamPos++; return buffer.get() & 0xff; }
public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; random.nextBytes(bytes); // Create wrapper stream BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes)); // Read to fill the buffer, then reset stream.readLong(); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; stream.readFully(result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); stream.readFully(result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
public void testBufferPositionCorrect() throws IOException { // Fill bytes byte[] bytes = new byte[1024]; random.nextBytes(bytes); ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes); input.readByte(); input.readByte(); input.skipBytes(124); input.readByte(); input.readByte(); // Sanity check assertEquals(128, input.getStreamPosition()); BufferedImageInputStream stream = new BufferedImageInputStream(input); assertEquals(input.getStreamPosition(), stream.getStreamPosition()); stream.skipBytes(128); //assertTrue(256 <= input.getStreamPosition()); assertEquals(256, stream.getStreamPosition()); stream.seek(1020); assertEquals(1020, stream.getStreamPosition()); }
@Override public int readBit() throws IOException { checkClosed(); if (!buffer.hasRemaining()) { fillBuffer(); if (!buffer.hasRemaining()) { throw new EOFException(); } } // Compute final bit offset before we call read() and seek() int newBitOffset = (this.bitOffset + 1) & 0x7; int val = buffer.get() & 0xff; if (newBitOffset != 0) { // Move byte position back if in the middle of a byte buffer.position(buffer.position() - 1); // Shift the bit to be read to the rightmost position val >>= 8 - newBitOffset; } else { streamPos++; } this.bitOffset = newBitOffset; return val & 0x1; }
return new BufferedImageInputStream(new FileImageInputStream(new File(url.toURI()))); return new BufferedImageInputStream(new FileCacheImageInputStream(stream, pCacheDir) { @Override public void close() throws IOException {
public void testReadResetReadDirectBufferBug() throws IOException { // Make sure we use the exact size of the buffer final int size = BufferedImageInputStream.DEFAULT_BUFFER_SIZE; // Fill bytes byte[] bytes = new byte[size * 2]; mRandom.nextBytes(bytes); // Create wrapper stream BufferedImageInputStream stream = new BufferedImageInputStream(new ByteArrayImageInputStream(bytes)); // Read to fill the buffer, then reset stream.readLong(); stream.seek(0); // Read fully and compare byte[] result = new byte[size]; stream.readFully(result); assertTrue(rangeEquals(bytes, 0, result, 0, size)); stream.readFully(result); assertTrue(rangeEquals(bytes, size, result, 0, size)); }
public void testBufferPositionCorrect() throws IOException { // Fill bytes byte[] bytes = new byte[1024]; mRandom.nextBytes(bytes); ByteArrayImageInputStream input = new ByteArrayImageInputStream(bytes); input.readByte(); input.readByte(); input.skipBytes(124); input.readByte(); input.readByte(); // Sanity check assertEquals(128, input.getStreamPosition()); BufferedImageInputStream stream = new BufferedImageInputStream(input); assertEquals(input.getStreamPosition(), stream.getStreamPosition()); stream.skipBytes(128); //assertTrue(256 <= input.getStreamPosition()); assertEquals(256, stream.getStreamPosition()); stream.seek(1020); assertEquals(1020, stream.getStreamPosition()); }
@Override public short readShort() throws IOException { readFully(integralCache.array(), 0, 2); return integralCache.getShort(0); }
@Override public long readBits(int numBits) throws IOException { checkClosed(); while (bitsToRead > 0) { if (!buffer.hasRemaining()) { fillBuffer();
imageInput.seek(lastKnownSOIOffset); try (ImageInputStream stream = new BufferedImageInputStream(imageInput)) { // Extreme (10s -> 50ms) speedup if imageInput is FileIIS for (int i = streamOffsets.size() - 1; i < imageIndex; i++) { long start = 0;