/** Returns the ImageType for the given InputStream. */ @NonNull public static ImageType getType(@NonNull List<ImageHeaderParser> parsers, @Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException { if (is == null) { return ImageType.UNKNOWN; } if (!is.markSupported()) { is = new RecyclableBufferedInputStream(is, byteArrayPool); } is.mark(MARK_POSITION); //noinspection ForLoopReplaceableByForEach to improve perf for (int i = 0, size = parsers.size(); i < size; i++) { ImageHeaderParser parser = parsers.get(i); try { ImageType type = parser.getType(is); if (type != ImageType.UNKNOWN) { return type; } } finally { is.reset(); } } return ImageType.UNKNOWN; }
InputStream localIn = in; if (localBuf == null || localIn == null) { throw streamClosed(); if (pos >= count && fillbuf(localIn, localBuf) == -1) { localBuf = buf; if (localBuf == null) { throw streamClosed();
@Override public void onObtainBounds() { // Once we've read the image header, we no longer need to allow the buffer to expand in // size. To avoid unnecessary allocations reading image data, we fix the mark limit so that it // is no larger than our current buffer size here. See issue #225. bufferedStream.fixMarkLimit(); }
@Synthetic InputStreamRewinder(InputStream is, ArrayPool byteArrayPool) { bufferedStream = new RecyclableBufferedInputStream(is, byteArrayPool); bufferedStream.mark(MARK_LIMIT); }
@Test public void testCanSafelyBeClosedMultipleTimes() throws IOException { InputStream wrapped = mock(InputStream.class); stream = new RecyclableBufferedInputStream(wrapped, byteArrayPool); stream.close(); stream.close(); stream.close(); verify(wrapped, times(1)).close(); }
ownsBufferedStream = false; } else { bufferedStream = new RecyclableBufferedInputStream(source, byteArrayPool); ownsBufferedStream = true; exceptionStream.release(); if (ownsBufferedStream) { bufferedStream.release();
@NonNull @Override public InputStream rewindAndGet() throws IOException { bufferedStream.reset(); return bufferedStream; }
/** * Returns an estimated number of bytes that can be read or skipped without blocking for more * input. This method returns the number of bytes available in the buffer plus those available in * the source stream, but see {@link InputStream#available} for important caveats. * * @return the estimated number of bytes available * @throws IOException if this stream is closed or an error occurs */ @Override public synchronized int available() throws IOException { // in could be invalidated by close(). InputStream localIn = in; if (buf == null || localIn == null) { throw streamClosed(); } return count - pos + localIn.available(); }
@Test public void testCallsMarkOnWrapped() { int toMark = 50; is.mark(toMark); verify(wrapped).mark(eq(toMark)); }
@Override public void cleanup() { bufferedStream.release(); }
@Test public void testCanMarkInMiddleOfBufferAndStillReadUpToBufferLengthBeforeResetting() throws IOException { int markPos = BUFFER_SIZE / 2; for (int i = 0; i < markPos; i++) { stream.read(); } stream.mark(BUFFER_SIZE); for (int i = 0; i < BUFFER_SIZE; i++) { stream.read(); } stream.reset(); assertEquals(markPos, stream.read()); }
InputStreamRewinder(InputStream is, ArrayPool byteArrayPool) { bufferedStream = new RecyclableBufferedInputStream(is, byteArrayPool); bufferedStream.mark(MARK_LIMIT); }
@Test public void testCloseClosesWrappedStream() throws IOException { InputStream wrapped = mock(InputStream.class); stream = new RecyclableBufferedInputStream(wrapped, byteArrayPool); stream.close(); verify(wrapped).close(); }
ownsBufferedStream = false; } else { bufferedStream = new RecyclableBufferedInputStream(source, byteArrayPool); ownsBufferedStream = true; exceptionStream.release(); if (ownsBufferedStream) { bufferedStream.release();
@Test(expected = RecyclableBufferedInputStream.InvalidMarkException.class) public void testResetThrowsIfMarkNotSet() throws IOException { stream.reset(); }
/** * Returns an estimated number of bytes that can be read or skipped without blocking for more * input. This method returns the number of bytes available in the buffer * plus those available in the source stream, but see {@link InputStream#available} for * important caveats. * * @return the estimated number of bytes available * @throws IOException if this stream is closed or an error occurs */ @Override public synchronized int available() throws IOException { // in could be invalidated by close(). InputStream localIn = in; if (buf == null || localIn == null) { throw streamClosed(); } return count - pos + localIn.available(); }
@Override public void cleanup() { bufferedStream.release(); }
throw streamClosed(); throw streamClosed(); if (fillbuf(localIn, localBuf) == -1) { return read;