/** * Decompress ZLIB (RFC 1950) compressed data * * @param compressedData A byte array containing the ZLIB-compressed data. * @param maxBytes The maximum number of uncompressed bytes to read. * @return A string containing the decompressed data */ public static String decompressZlib(byte[] compressedData, long maxBytes) throws IOException { try (final ByteArrayInputStream dataStream = new ByteArrayInputStream(compressedData); final InflaterInputStream in = new InflaterInputStream(dataStream); final InputStream limited = ByteStreams.limit(in, maxBytes)) { return new String(ByteStreams.toByteArray(limited), StandardCharsets.UTF_8); } }
/** * Decompress GZIP (RFC 1952) compressed data * * @param compressedData A byte array containing the GZIP-compressed data. * @param maxBytes The maximum number of uncompressed bytes to read. * @return A string containing the decompressed data */ public static String decompressGzip(byte[] compressedData, long maxBytes) throws IOException { try (final ByteArrayInputStream dataStream = new ByteArrayInputStream(compressedData); final GZIPInputStream in = new GZIPInputStream(dataStream); final InputStream limited = ByteStreams.limit(in, maxBytes)) { return new String(ByteStreams.toByteArray(limited), StandardCharsets.UTF_8); } }
private InputStream sliceStream(InputStream in) throws IOException { if (offset > 0) { long skipped; try { skipped = ByteStreams.skipUpTo(in, offset); } catch (Throwable e) { Closer closer = Closer.create(); closer.register(in); try { throw closer.rethrow(e); } finally { closer.close(); } } if (skipped < offset) { // offset was beyond EOF in.close(); return new ByteArrayInputStream(new byte[0]); } } return ByteStreams.limit(in, length); }
private InputStream sliceStream(InputStream in) throws IOException { if (offset > 0) { long skipped; try { skipped = ByteStreams.skipUpTo(in, offset); } catch (Throwable e) { Closer closer = Closer.create(); closer.register(in); try { throw closer.rethrow(e); } finally { closer.close(); } } if (skipped < offset) { // offset was beyond EOF in.close(); return new ByteArrayInputStream(new byte[0]); } } return ByteStreams.limit(in, length); }
private InputStream sliceStream(InputStream in) throws IOException { if (offset > 0) { long skipped; try { skipped = ByteStreams.skipUpTo(in, offset); } catch (Throwable e) { Closer closer = Closer.create(); closer.register(in); try { throw closer.rethrow(e); } finally { closer.close(); } } if (skipped < offset) { // offset was beyond EOF in.close(); return new ByteArrayInputStream(new byte[0]); } } return ByteStreams.limit(in, length); }
public void testLimit_skip() throws Exception { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); // also test available lin.mark(2); assertEquals(2, lin.available()); lin.skip(1); assertEquals(1, lin.available()); lin.reset(); assertEquals(2, lin.available()); lin.skip(3); assertEquals(0, lin.available()); }
@Override protected void _send(final InputStream stream) throws Exception { long[] byteRange = byteRange(); if (byteRange == null) { rsp.send(stream); } else { stream.skip(byteRange[0]); rsp.send(ByteStreams.limit(stream, byteRange[1])); } }
public void testLimit_markNotSet() { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessageThat().isEqualTo("Mark not set"); } }
public void testLimit_markNotSupported() { InputStream lin = ByteStreams.limit(new UnmarkableInputStream(), 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessageThat().isEqualTo("Mark not supported"); } }
public void testLimit_mark() throws Exception { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); int read = lin.read(); assertEquals(big[0], read); lin.mark(2); read = lin.read(); assertEquals(big[1], read); read = lin.read(); assertEquals(-1, read); lin.reset(); read = lin.read(); assertEquals(big[1], read); read = lin.read(); assertEquals(-1, read); }
public void testLimit() throws Exception { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); // also test available lin.mark(2); assertEquals(2, lin.available()); int read = lin.read(); assertEquals(big[0], read); assertEquals(1, lin.available()); read = lin.read(); assertEquals(big[1], read); assertEquals(0, lin.available()); read = lin.read(); assertEquals(-1, read); lin.reset(); byte[] small = new byte[5]; read = lin.read(small); assertEquals(2, read); assertEquals(big[0], small[0]); assertEquals(big[1], small[1]); lin.reset(); read = lin.read(small, 2, 3); assertEquals(2, read); assertEquals(big[0], small[2]); assertEquals(big[1], small[3]); }
@Override public Map<String, String[]> getParameterMap() { InputStream body = request.getBody(); if (body == null) { return ImmutableMap.of(); } String bodyStr; try { body.reset(); bodyStr = CharStreams.toString(new InputStreamReader(ByteStreams.limit(body, 1000), Charsets.UTF_8)); } catch (IOException e) { return ImmutableMap.of(); } return ImmutableMap.of("raw", new String[]{bodyStr}); }
payload = ByteStreams.toByteArray(ByteStreams.limit( is, v4MaxNonChunkedRequestSize + 1)); if (payload.length == v4MaxNonChunkedRequestSize + 1) {
@Override public void readBytes(OutputStream out, int length) throws IOException { ByteStreams.copy(ByteStreams.limit(countingInputStream, length), out); } }
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { byte[] data = ByteStreams.toByteArray(ByteStreams.limit(input, length)); out.write(ByteBuffer.wrap(data)); return data.length; }
protected Payload doSlice(InputStream content, long offset, long length) { try { ByteStreams.skipFully(content, offset); } catch (IOException ioe) { throw Throwables.propagate(ioe); } return new InputStreamPayload(ByteStreams.limit(content, length)); }
protected Payload doSlice(InputStream content, long offset, long length) { try { ByteStreams.skipFully(content, offset); } catch (IOException ioe) { throw Throwables.propagate(ioe); } return new InputStreamPayload(ByteStreams.limit(content, length)); }
protected Payload doSlice(InputStream content, long offset, long length) { try { ByteStreams.skipFully(content, offset); } catch (IOException ioe) { throw Throwables.propagate(ioe); } return new InputStreamPayload(ByteStreams.limit(content, length)); }
public void testLimit_markNotSet() { byte[] big = newPreFilledByteArray(5); InputStream bin = new ByteArrayInputStream(big); InputStream lin = ByteStreams.limit(bin, 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessage("Mark not set"); } }
public void testLimit_markNotSupported() { InputStream lin = ByteStreams.limit(new UnmarkableInputStream(), 2); try { lin.reset(); fail(); } catch (IOException expected) { assertThat(expected).hasMessage("Mark not supported"); } }