int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH); int outputLength = z.next_out_index - oldNextOutIndex; if (outputLength > 0) { ZlibUtil.fail(z, "decompression failure", resultCode); } else { resultCode = z.inflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode); case JZlib.Z_STREAM_END: finished = true; // Do not decode anymore. z.inflateEnd(); break loop; case JZlib.Z_OK:
public void reset() { if (impl == null) { throw new NullPointerException(); } finished = false; needsDictionary = false; inLength = 0; inRead = 0; impl.init(nowrap); }
/** * Creates a new instance with the specified preset dictionary. The wrapper * is always {@link ZlibWrapper#ZLIB} because it is the only format that * supports the preset dictionary. * * @throws DecompressionException if failed to initialize zlib */ public JZlibDecoder(byte[] dictionary) { if (dictionary == null) { throw new NullPointerException("dictionary"); } this.dictionary = dictionary; int resultCode; resultCode = z.inflateInit(JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } }
@Override public void uncompress(Buffer from, Buffer to) throws TransportException { inflater.setNextIn(from.array()); inflater.setNextInIndex(from.rpos()); inflater.setAvailIn(from.available()); while (true) { inflater.setNextOut(tempBuf); inflater.setNextOutIndex(0); inflater.setAvailOut(BUF_SIZE); final int status = inflater.inflate(JZlib.Z_PARTIAL_FLUSH); switch (status) { case JZlib.Z_OK: to.putRawBytes(tempBuf, 0, BUF_SIZE - inflater.getAvailOut()); break; case JZlib.Z_BUF_ERROR: return; default: throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + status); } } }
public void readHeader() throws IOException { byte[] empty = "".getBytes(); inflater.setInput(empty, 0, 0, false); inflater.setOutput(empty, 0, 0); int err = inflater.inflate(JZlib.Z_NO_FLUSH); if(!inflater.istate.inParsingHeader()){ return; } byte[] b1 = new byte[1]; do{ int i = in.read(b1); if(i<=0) throw new IOException("no input"); inflater.setInput(b1); err = inflater.inflate(JZlib.Z_NO_FLUSH); if(err!=0/*Z_OK*/) throw new IOException(inflater.msg); } while(inflater.istate.inParsingHeader()); }
flater.setOutput(collected, collectedIdx, collected.length - collectedIdx); int ret = flater.inflate(com.jcraft.jzlib.JZlib.Z_NO_FLUSH); resultLength = flater.next_out_index - collectedIdx; collectedIdx = flater.next_out_index; throw RubyZlib.newDataError(runtime, flater.getMessage()); case com.jcraft.jzlib.JZlib.Z_NEED_DICT: throw RubyZlib.newDictError(runtime, "need dictionary"); flater.setInput("".getBytes()); int err = flater.inflate(com.jcraft.jzlib.JZlib.Z_FINISH); if (err != com.jcraft.jzlib.JZlib.Z_OK) { throw RubyZlib.newBufError(getRuntime(), "buffer error");
public TInflater(boolean noHeader) { nowrap = noHeader; try { impl = new Inflater(noHeader); } catch (GZIPException e) { // Do nothing } }
protected void fill() throws IOException { if (closed) { throw new IOException("Stream closed"); } int len = in.read(buf, 0, buf.length); if (len == -1) { if(inflater.istate.wrap == 0 && !inflater.finished()){ buf[0]=0; len=1; } else if(inflater.istate.was != -1){ // in reading trailer throw new IOException("footer is not found"); } else{ throw new EOFException("Unexpected end of ZLIB input stream"); } } inflater.setInput(buf, 0, len, true); }
/** * Get position within this stream including that has been read by users * calling read + what jzlib may have speculatively read in because of * buffering. * * @return number of bytes */ private long internalPosition() { Inflater inflater = io.getInflater(); return inflater.getTotalIn() + inflater.getAvailIn(); }
@JRubyMethod(name = "sync", required = 1) public IRubyObject sync(ThreadContext context, IRubyObject string) { if (flater.avail_in > 0) { switch (flater.sync()) { case com.jcraft.jzlib.JZlib.Z_OK: flater.setInput(string.convertToString().getByteList().bytes(), true); return getRuntime().getTrue(); case com.jcraft.jzlib.JZlib.Z_DATA_ERROR: break; default: throw RubyZlib.newStreamError(getRuntime(), "stream error"); } } if (string.convertToString().getByteList().length() <= 0) { return getRuntime().getFalse(); } flater.setInput(string.convertToString().getByteList().bytes(), true); switch (flater.sync()) { case com.jcraft.jzlib.JZlib.Z_OK: return getRuntime().getTrue(); case com.jcraft.jzlib.JZlib.Z_DATA_ERROR: return getRuntime().getFalse(); default: throw RubyZlib.newStreamError(getRuntime(), "stream error"); } }
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) { inRead = 0; inLength = nbytes; impl.setInput(buf, off, nbytes, false); } else { throw new ArrayIndexOutOfBoundsException(); } } }
public long getBytesRead() { if (impl == null) { throw new IllegalStateException(); } return impl.getTotalIn(); }
public void readHeader() throws IOException { byte[] empty = "".getBytes(); inflater.setInput(empty, 0, 0, false); inflater.setOutput(empty, 0, 0); int err = inflater.inflate(JZlib.Z_NO_FLUSH); if(!inflater.istate.inParsingHeader()){ return; } byte[] b1 = new byte[1]; do{ int i = in.read(b1); if(i<=0) throw new IOException("no input"); inflater.setInput(b1); err = inflater.inflate(JZlib.Z_NO_FLUSH); if(err!=0/*Z_OK*/) throw new IOException(inflater.msg); } while(inflater.istate.inParsingHeader()); }
@Override public void uncompress(Buffer from, Buffer to) throws TransportException { inflater.setNextIn(from.array()); inflater.setNextInIndex(from.rpos()); inflater.setAvailIn(from.available()); while (true) { inflater.setNextOut(tempBuf); inflater.setNextOutIndex(0); inflater.setAvailOut(BUF_SIZE); final int status = inflater.inflate(JZlib.Z_PARTIAL_FLUSH); switch (status) { case JZlib.Z_OK: to.putRawBytes(tempBuf, 0, BUF_SIZE - inflater.getAvailOut()); break; case JZlib.Z_BUF_ERROR: return; default: throw new TransportException(DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + status); } } }
@Override public void init(Mode mode) { try { switch (mode) { case DEFLATE: deflater = new Deflater(JZlib.Z_DEFAULT_COMPRESSION); break; case INFLATE: inflater = new Inflater(); break; default: assert false; } } catch (GZIPException gze) { } }
protected void fill() throws IOException { if (closed) { throw new IOException("Stream closed"); } int len = in.read(buf, 0, buf.length); if (len == -1) { if(inflater.istate.wrap == 0 && !inflater.finished()){ buf[0]=0; len=1; } else if(inflater.istate.was != -1){ // in reading trailer throw new IOException("footer is not found"); } else{ throw new EOFException("Unexpected end of ZLIB input stream"); } } inflater.setInput(buf, 0, len, true); }