@Override public synchronized void reset() throws IOException { if (!in.markSupported()) { throw new IOException("Mark not supported"); } if (mark == -1) { throw new IOException("Mark not set"); } in.reset(); count = mark; } }
public void mark(int len) throws IOException { if (!input.markSupported()) { throw new IOException("Mark not supported for input stream " + input.getClass()); } input.mark(len); }
/** * 将{@link InputStream}转换为支持mark标记的流<br> * 若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之 * * @param in 流 * @return {@link InputStream} * @since 4.0.9 */ public static InputStream toMarkSupportStream(InputStream in) { if (null == in) { return null; } if (false == in.markSupported()) { return new BufferedInputStream(in); } return in; }
public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException { this.headers = inputMessage.getHeaders(); InputStream inputStream = inputMessage.getBody(); if (inputStream.markSupported()) { inputStream.mark(1); this.body = (inputStream.read() != -1 ? inputStream : null); inputStream.reset(); } else { PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream); int b = pushbackInputStream.read(); if (b == -1) { this.body = null; } else { this.body = pushbackInputStream; pushbackInputStream.unread(b); } } }
public void testReadSingleByte() throws Exception { ByteSource source = newByteSource(0, 10); ByteSource joined = ByteSource.concat(source, source); assertEquals(20, joined.size()); InputStream in = joined.openStream(); assertFalse(in.markSupported()); assertEquals(10, in.available()); int total = 0; while (in.read() != -1) { total++; } assertEquals(0, in.available()); assertEquals(20, total); }
@Test public void testResetNoMark() throws Exception { InputStream input = makeInputStream(makeOrderedArray(10)); if (!input.markSupported()) { return; // Not supported, skip test } int read = input.read(); assertEquals(0, read); // No mark may either throw exception, or reset to beginning of stream. try { input.reset(); assertEquals("Re-read of reset data should be same", 0, input.read()); } catch (Exception e) { assertTrue("Wrong no mark IOException message", e.getMessage().contains("mark")); } }
InputStream bufIn = is.markSupported() ? is : new BufferedInputStream(is); bufIn.mark(4); DataInputStream in = new DataInputStream(bufIn); int magic = in.readInt();
/** 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; }
if (! stream.markSupported()) { stream = new BufferedInputStream(stream); stream.reset(); TikaInputStream tstream = TikaInputStream.get(stream, tmp);
InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException { if (!stream.markSupported()) { stream = new BufferedInputStream(stream); } stream.mark(maxEntitySize + 1); final byte[] entity = new byte[maxEntitySize + 1]; final int entitySize = stream.read(entity); b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset)); if (entitySize > maxEntitySize) { b.append("...more..."); } b.append('\n'); stream.reset(); return stream; }
/** * 将{@link InputStream}转换为支持mark标记的流<br> * 若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之 * * @param in 流 * @return {@link InputStream} * @since 4.0.9 */ public static InputStream toMarkSupportStream(InputStream in) { if (null == in) { return null; } if (false == in.markSupported()) { return new BufferedInputStream(in); } return in; }
private void validateInputStreamReads(InputStream test, InputStream check) throws IOException { byte[] bt = new byte[7]; byte[] bc = new byte[7]; while (true) { int t = test.read(); int c = check.read(); Assert.assertEquals(c, t); if (-1 == t) break; t = test.read(bt); c = check.read(bc); Assert.assertEquals(c, t); Assert.assertArrayEquals(bt, bc); if (-1 == t) break; t = test.read(bt, 1, 4); c = check.read(bc, 1, 4); Assert.assertEquals(c, t); Assert.assertArrayEquals(bt, bc); if (-1 == t) break; } Assert.assertEquals(0, test.skip(5)); Assert.assertEquals(0, test.available()); Assert.assertFalse(test.getClass() != ByteArrayInputStream.class && test.markSupported()); test.close(); }
/** * 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; } }
@Override public synchronized void reset() throws IOException { if (!in.markSupported()) { throw new IOException("Mark not supported"); } if (mark == -1) { throw new IOException("Mark not set"); } in.reset(); left = mark; }
public InputStreamByteIterator(InputStream ins, long len) { this.len = len; this.ins = ins; off = 0; resetable = ins.markSupported(); if (resetable) { ins.mark((int) len); } }
InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset) throws IOException { if (!stream.markSupported()) { stream = new BufferedInputStream(stream); } stream.mark(maxEntitySize + 1); final byte[] entity = new byte[maxEntitySize + 1]; final int entitySize = stream.read(entity); b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset)); if (entitySize > maxEntitySize) { b.append("...more..."); } b.append('\n'); stream.reset(); return stream; }
/** * Checks if an {@link InputStream} can be reset (i.e. used for checking the header magic) and wraps it if not * * @param stream stream to be checked for wrapping * @return a mark enabled stream */ public static InputStream prepareToCheckMagic(InputStream stream) { if (stream.markSupported()) { return stream; } // we used to process the data via a PushbackInputStream, but user code could provide a too small one // so we use a BufferedInputStream instead now return new BufferedInputStream(stream); } }
@SuppressFBWarnings("OS_OPEN_STREAM") public static String getXMLType(@WillNotClose InputStream in) throws IOException { if (!in.markSupported()) { throw new IllegalArgumentException("Input stream does not support mark"); } in.mark(5000); BufferedReader r = null; try { r = new BufferedReader(Util.getReader(in), 2000); String s; int count = 0; while (count < 4) { s = r.readLine(); if (s == null) { break; } Matcher m = tag.matcher(s); if (m.find()) { return m.group(1); } } throw new IOException("Didn't find xml tag"); } finally { in.reset(); } }
@Override public synchronized void reset() throws IOException { if (!in.markSupported()) { throw new IOException("Mark not supported"); } if (mark == -1) { throw new IOException("Mark not set"); } in.reset(); count = mark; } }
/** * The readlimit parameter is ignored. */ @Override public void mark(int readlimit) { abortIfNeeded(); if ( !isAtStart ) throw new UnsupportedOperationException("Chunk-encoded stream only supports mark() at the start of the stream."); if (is.markSupported()) { if (log.isDebugEnabled()) { log.debug("AwsChunkedEncodingInputStream marked at the start of the stream " + "(will directly mark the wrapped stream since it's mark-supported)."); } is.mark(readlimit); } else { if (log.isDebugEnabled()) { log.debug("AwsChunkedEncodingInputStream marked at the start of the stream " + "(initializing the buffer since the wrapped stream is not mark-supported)."); } decodedStreamBuffer = new DecodedStreamBuffer(maxBufferSize); } }