int deflate(byte[] buf, int off, int nbytes, int flushParam) { if (impl == null) { throw new IllegalStateException(); } // avoid int overflow, check null buf if (off > buf.length || nbytes < 0 || off < 0 || buf.length - off < nbytes) { throw new ArrayIndexOutOfBoundsException(); } long sin = impl.total_in; long sout = impl.total_out; impl.setOutput(buf, off, nbytes); int err = impl.deflate(flushParam); switch (err) { case JZlib.Z_OK: break; case JZlib.Z_STREAM_END: finished = true; break; default: throw new RuntimeException("Error: " + err); } inRead += impl.total_in - sin; return (int) (impl.total_out - sout); } public void end() {
public void reset() { if (impl == null) { throw new NullPointerException(); } flushParm = Z_NO_FLUSH; finished = false; impl.init(compressLevel, strategy, nowrap); }
int resultCode = z.deflate(JZlib.Z_FINISH); if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) { promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode)); z.deflateEnd();
public void setInput(byte[] buf, int off, int nbytes) { if (impl == null) { throw new IllegalStateException(); } // avoid int overflow, check null buf if (off <= buf.length && nbytes >= 0 && off >= 0 && buf.length - off >= nbytes) { inLength = nbytes; inRead = 0; if (impl.next_in == null) { impl.init(compressLevel, strategy, nowrap); } impl.setInput(buf, off, nbytes, false); } else { throw new ArrayIndexOutOfBoundsException(); } }
@Override public void compress(Buffer buffer) { deflater.setNextIn(buffer.array()); deflater.setNextInIndex(buffer.rpos()); deflater.setAvailIn(buffer.available()); buffer.wpos(buffer.rpos()); do { deflater.setNextOut(tempBuf); deflater.setNextOutIndex(0); deflater.setAvailOut(BUF_SIZE); final int status = deflater.deflate(JZlib.Z_PARTIAL_FLUSH); if (status == JZlib.Z_OK) { buffer.putRawBytes(tempBuf, 0, BUF_SIZE - deflater.getAvailOut()); } else { throw new SSHRuntimeException("compress: deflate returned " + status); } } while (deflater.getAvailOut() == 0); }
resultCode = z.deflateInit( compressionLevel, windowBits, memLevel, ZlibUtil.fail(z, "initialization failure", resultCode); } else { resultCode = z.deflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
public int read(byte[] b, int off, int len) throws IOException { if(compress){ deflater.setOutput(b, off, len); while(true){ int datalen = in.read(buf, 0, buf.length); if(datalen == -1) return -1; deflater.setInput(buf, 0, datalen, true); int err = deflater.deflate(flush); if(deflater.next_out_index>0) return deflater.next_out_index; if(err == JZlib.Z_STREAM_END) return 0; if(err == JZlib.Z_STREAM_ERROR || err == JZlib.Z_DATA_ERROR){ throw new ZStreamException("deflating: "+deflater.msg); } } } else{ return iis.read(b, off, len); } }
@Override public void end() { if (finished) { return; } finished = true; z.deflateEnd(); z.next_in = null; z.next_out = null; } }
resultCode = z.deflate(JZlib.Z_SYNC_FLUSH); } finally { in.skipBytes(z.next_in_index - oldNextInIndex);
public TDeflater(int level, boolean noHeader) { super(); if (level < DEFAULT_COMPRESSION || level > BEST_COMPRESSION) { throw new IllegalArgumentException(); } compressLevel = level; try { impl = new Deflater(compressLevel, strategy, noHeader); } catch (GZIPException e) { // do nothing } nowrap = noHeader; }
private void init(int level, int windowBits, int memlevel, int strategy) { flush = JZlib.Z_NO_FLUSH; flater = new com.jcraft.jzlib.Deflater(); // TODO: Can we expect JZlib to check level, windowBits, and strategy here? // Then we should remove checkLevel, checkWindowsBits and checkStrategy. int err = flater.init(level, windowBits, memlevel); if (err == com.jcraft.jzlib.JZlib.Z_STREAM_ERROR) { throw RubyZlib.newStreamError(getRuntime(), "stream error"); } err = flater.params(level, strategy); if (err == com.jcraft.jzlib.JZlib.Z_STREAM_ERROR) { throw RubyZlib.newStreamError(getRuntime(), "stream error"); } collected = new byte[BASE_SIZE]; collectedIdx = 0; }
public void write(byte[] b, int off, int len) throws IOException { if (deflater.finished()) { throw new IOException("finished"); } else if (off<0 | len<0 | off+len>b.length) { throw new IndexOutOfBoundsException(); } else if (len == 0) { return; } else { int flush = syncFlush ? JZlib.Z_SYNC_FLUSH : JZlib.Z_NO_FLUSH; deflater.setInput(b, off, len, true); while (deflater.avail_in>0) { int err = deflate(flush); if (err == JZlib.Z_STREAM_END) break; } } }
@JRubyMethod(name = "params", required = 2) public IRubyObject params(ThreadContext context, IRubyObject level, IRubyObject strategy) { int l = RubyNumeric.fix2int(level); checkLevel(getRuntime(), l); int s = RubyNumeric.fix2int(strategy); checkStrategy(getRuntime(), s); if (flater.next_out == null) flater.setOutput(ByteList.NULL_ARRAY); int err = flater.params(l, s); if (err == com.jcraft.jzlib.JZlib.Z_STREAM_ERROR) { throw RubyZlib.newStreamError(getRuntime(), "stream error"); } if (collectedIdx != flater.next_out_index) collectedIdx = flater.next_out_index; run(); return getRuntime().getNil(); }
private IRubyObject flush(int flush) { int last_flush = this.flush; this.flush = flush; if (flush == JZlib.Z_NO_FLUSH) return RubyString.newEmptyString(getRuntime()); run(); this.flush = last_flush; IRubyObject obj = RubyString.newString(getRuntime(), collected, 0, collectedIdx); collectedIdx = 0; flater.setOutput(collected); return obj; }
private void append(ByteList obj) throws IOException { flater.setInput(obj.getUnsafeBytes(), obj.getBegin(), obj.getRealSize(), true); run(); }
SpdyHeaderBlockJZlibEncoder( SpdyVersion version, int compressionLevel, int windowBits, int memLevel) { super(version); if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (windowBits < 9 || windowBits > 15) { throw new IllegalArgumentException( "windowBits: " + windowBits + " (expected: 9-15)"); } if (memLevel < 1 || memLevel > 9) { throw new IllegalArgumentException( "memLevel: " + memLevel + " (expected: 1-9)"); } int resultCode = z.deflateInit( compressionLevel, windowBits, memLevel, JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { throw new CompressionException( "failed to initialize an SPDY header block deflater: " + resultCode); } else { resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length); if (resultCode != JZlib.Z_OK) { throw new CompressionException( "failed to set the SPDY dictionary: " + resultCode); } } }
public int read(byte[] b, int off, int len) throws IOException { if(compress){ deflater.setOutput(b, off, len); while(true){ int datalen = in.read(buf, 0, buf.length); if(datalen == -1) return -1; deflater.setInput(buf, 0, datalen, true); int err = deflater.deflate(flush); if(deflater.next_out_index>0) return deflater.next_out_index; if(err == JZlib.Z_STREAM_END) return 0; if(err == JZlib.Z_STREAM_ERROR || err == JZlib.Z_DATA_ERROR){ throw new ZStreamException("deflating: "+deflater.msg); } } } else{ return iis.read(b, off, len); } }
@Override public void end() { if (finished) { return; } finished = true; z.deflateEnd(); z.next_in = null; z.next_out = null; } }