private static InputStream getInputStream(StreamConnection sc, String urlAddress) throws IOException { InputStream stream = sc.getInputStream(); String encoding = sc.getEncoding(); if ("gzip".equals(encoding) || urlAddress.endsWith(".gz")) { return new GZIPInputStream(stream); } if ("deflate".equals(encoding)) { return new DeflaterInputStream(stream); } return stream; }
/** * Returns 0 when when this stream has exhausted its input; and 1 otherwise. * A result of 1 does not guarantee that further bytes can be returned, * with or without blocking. * * <p>Although consistent with the RI, this behavior is inconsistent with * {@link InputStream#available()}, and violates the <a * href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov * Substitution Principle</a>. This method should not be used. * * @return 0 if no further bytes are available. Otherwise returns 1, * which suggests (but does not guarantee) that additional bytes are * available. * @throws IOException if this stream is closed or an error occurs */ @Override public int available() throws IOException { checkClosed(); return available ? 1 : 0; }
private static byte[] zipText(byte[] licenseText) throws IOException { int len; byte[] buff = new byte[64]; ByteArrayInputStream in = new ByteArrayInputStream(licenseText); DeflaterInputStream deflater = new DeflaterInputStream(in, new Deflater()); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { while ((len = deflater.read(buff)) > 0) { out.write(buff, 0, len); } return out.toByteArray(); } finally { out.close(); deflater.close(); in.close(); } }
... out.println("Content-Encoding: deflate"); // NOTICE deflate encoding out.println("Content-Type: text/html"); out.println("Connection: close"); out.println(); String x = "jsonp1330xxxxx462022184([["; DeflaterInputStream dis = new DeflaterInputStream(out); dis.write(x.getBytes("utf-8")); // JSON is UTF-8 dis.close(); server.close(); // this a bad idea, the client may not have read the data yet
final int r = super.read(b, off + count, i); if (r < i) {
@Override public void close() throws IOException { if (LOG.isDebugEnabled()) { final StringBuilder sb = new StringBuilder(); long bytesWritten = this.def.getBytesWritten(); long bytesRead = this.def.getBytesRead(); sb.append("POST body length compressed / uncompressed / compression ratio: "); sb.append(bytesWritten); sb.append(" / "); sb.append(bytesRead); sb.append(" / "); sb.append(String.format( "%.2f", bytesRead / (double)bytesWritten)); LOG.debug(sb.toString()); } super.close(); } };
private static byte[] deflate(byte[] orig) throws ItlException { try { DeflaterInputStream isDeflater = new DeflaterInputStream(new ByteArrayInputStream(orig), new Deflater()); ByteArrayOutputStream osCompressed = new ByteArrayOutputStream(orig.length); byte[] deflated = new byte[orig.length]; int iCompressed; while(true) { iCompressed = isDeflater.read(deflated, 0, orig.length); if (iCompressed == -1) break; osCompressed.write(deflated, 0, iCompressed); } deflated = osCompressed.toByteArray(); osCompressed.close(); isDeflater.close(); return deflated; } catch (IOException ioe) { throw new ItlException("Error when zipping the file contents", ioe); } }
public int read(byte b[], int off, int len) throws IOException { // Check if there are bytes left to be read from the internal buffer. This is used to provide the header // or trailer, and always takes precedence. int count; if (buffer.getByteCountRemainder() > 0) { // Write data from the internal buffer into b. count = Math.min(len, buffer.getByteCountRemainder()); System.arraycopy(buffer.data, buffer.position, b, off, count); // Advance the internal buffer position as "count" bytes have already been read. buffer.position += count; return count; } // Attempt to read compressed input data. count = super.read(b, off, len); if (count > 0) { return count; } /* * If the stream has reached completion, write out the GZIP trailer and re-attempt the read */ if (count <= 0 && !trailerWritten) { buffer.position = 0; buffer.length = writeTrailer(buffer.data, buffer.position); trailerWritten = true; return read(b, off, len); } else { return count; } }
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);
checkClosed(); Arrays.checkOffsetAndCount(buffer.length, byteOffset, byteCount); if (byteCount == 0) {
count = super.read(b, off, len); if (count > 0)
InputStreamBody data = new InputStreamBody(new DeflaterInputStream(inputStream), filename);
/** * Returns 0 when when this stream has exhausted its input; and 1 otherwise. * A result of 1 does not guarantee that further bytes can be returned, * with or without blocking. * * <p>Although consistent with the RI, this behavior is inconsistent with * {@link InputStream#available()}, and violates the <a * href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov * Substitution Principle</a>. This method should not be used. * * @return 0 if no further bytes are available. Otherwise returns 1, * which suggests (but does not guarantee) that additional bytes are * available. * @throws IOException if this stream is closed or an error occurs */ @Override public int available() throws IOException { checkClosed(); return available ? 1 : 0; }
@Override public CompressedInput getCompressedStream(final InputStream inputStream) { return () -> new DeflaterInputStream(inputStream); }
/** * Returns 0 when when this stream has exhausted its input; and 1 otherwise. * A result of 1 does not guarantee that further bytes can be returned, * with or without blocking. * * <p>Although consistent with the RI, this behavior is inconsistent with * {@link InputStream#available()}, and violates the <a * href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov * Substitution Principle</a>. This method should not be used. * * @return 0 if no further bytes are available. Otherwise returns 1, * which suggests (but does not guarantee) that additional bytes are * available. * @throws IOException if this stream is closed or an error occurs */ @Override public int available() throws IOException { checkClosed(); return available ? 1 : 0; }
@Override public InputStream compress(InputStream in) { return new DeflaterInputStream(in); }
/** * Returns 0 when when this stream has exhausted its input; and 1 otherwise. * A result of 1 does not guarantee that further bytes can be returned, * with or without blocking. * * <p>Although consistent with the RI, this behavior is inconsistent with * {@link InputStream#available()}, and violates the <a * href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov * Substitution Principle</a>. This method should not be used. * * @return 0 if no further bytes are available. Otherwise returns 1, * which suggests (but does not guarantee) that additional bytes are * available. * @throws IOException if this stream is closed or an error occurs */ @Override public int available() throws IOException { checkClosed(); return available ? 1 : 0; }
public InputStream getCompressedStream(InputStream unCompressedStream) { return new DeflaterInputStream(unCompressedStream); }
/** * Returns 0 when when this stream has exhausted its input; and 1 otherwise. * A result of 1 does not guarantee that further bytes can be returned, * with or without blocking. * * <p>Although consistent with the RI, this behavior is inconsistent with * {@link InputStream#available()}, and violates the <a * href="http://en.wikipedia.org/wiki/Liskov_substitution_principle">Liskov * Substitution Principle</a>. This method should not be used. * * @return 0 if no further bytes are available. Otherwise returns 1, * which suggests (but does not guarantee) that additional bytes are * available. * @throws IOException if this stream is closed or an error occurs */ @Override public int available() throws IOException { checkClosed(); return available ? 1 : 0; }
@Override protected InputStream getCompressionStream(InputStream input) { return new DeflaterInputStream(input, new Deflater(compressionLevel)); } }