/** 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; }
@Synthetic InputStreamRewinder(InputStream is, ArrayPool byteArrayPool) { bufferedStream = new RecyclableBufferedInputStream(is, byteArrayPool); bufferedStream.mark(MARK_LIMIT); }
/** * Returns the orientation for the given InputStream. */ public static int getOrientation(@NonNull List<ImageHeaderParser> parsers, @Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException { if (is == null) { return ImageHeaderParser.UNKNOWN_ORIENTATION; } 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 { int orientation = parser.getOrientation(is, byteArrayPool); if (orientation != ImageHeaderParser.UNKNOWN_ORIENTATION) { return orientation; } } finally { is.reset(); } } return ImageHeaderParser.UNKNOWN_ORIENTATION; } }
@Test(expected = IOException.class) public void testCloseThrowsIfWrappedStreamThrowsOnClose() throws IOException { InputStream wrapped = mock(InputStream.class); doThrow(new IOException()).when(wrapped).close(); stream = new RecyclableBufferedInputStream(wrapped, byteArrayPool); stream.close(); }
@Test public void testCloseClosesWrappedStream() throws IOException { InputStream wrapped = mock(InputStream.class); stream = new RecyclableBufferedInputStream(wrapped, byteArrayPool); stream.close(); verify(wrapped).close(); }
@Before public void setUp() { data = new byte[DATA_SIZE]; for (int i = 0; i < DATA_SIZE; i++) { data[i] = (byte) i; } byteArrayPool = new LruArrayPool(); InputStream wrapped = new ByteArrayInputStream(data); stream = new RecyclableBufferedInputStream(wrapped, byteArrayPool, BUFFER_SIZE); }
@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;
public InputStream build(InputStream is, byte[] buffer) { return new RecyclableBufferedInputStream(is, buffer); } }
RecyclableBufferedInputStream bufferedStream = new RecyclableBufferedInputStream( is, bytesForStream);
InputStreamRewinder(InputStream is, ArrayPool byteArrayPool) { bufferedStream = new RecyclableBufferedInputStream(is, byteArrayPool); bufferedStream.mark(MARK_LIMIT); }
/** Returns the ImageType for the given InputStream. */ public static ImageType getType(List<ImageHeaderParser> parsers, @Nullable InputStream is, ArrayPool byteArrayPool) throws IOException { if (is == null) { return ImageType.UNKNOWN; } if (!is.markSupported()) { is = new RecyclableBufferedInputStream(is, byteArrayPool); } is.mark(MARK_POSITION); for (ImageHeaderParser parser : parsers) { try { ImageType type = parser.getType(is); if (type != ImageType.UNKNOWN) { return type; } } finally { is.reset(); } } return ImageType.UNKNOWN; }
/** Returns the orientation for the given InputStream. */ public static int getOrientation(List<ImageHeaderParser> parsers, @Nullable InputStream is, ArrayPool byteArrayPool) throws IOException { if (is == null) { return ImageHeaderParser.UNKNOWN_ORIENTATION; } if (!is.markSupported()) { is = new RecyclableBufferedInputStream(is, byteArrayPool); } is.mark(MARK_POSITION); for (ImageHeaderParser parser : parsers) { try { int orientation = parser.getOrientation(is, byteArrayPool); if (orientation != ImageHeaderParser.UNKNOWN_ORIENTATION) { return orientation; } } finally { is.reset(); } } return ImageHeaderParser.UNKNOWN_ORIENTATION; } }
ownsBufferedStream = false; } else { bufferedStream = new RecyclableBufferedInputStream(source, byteArrayPool); ownsBufferedStream = true;