/** * Sets the preset dictionary. This should only be called, if needsDictionary() returns true and * it should set the same dictionary, that was used for deflating. The getAdler() function returns * the checksum of the dictionary needed. * * @param buffer the dictionary. * @param off the offset into buffer where the dictionary starts. * @param len the length of the dictionary. * @exception IllegalStateException if no dictionary is needed. * @exception IllegalArgumentException if the dictionary checksum is wrong. * @exception IndexOutOfBoundsException if the off and/or len are wrong. */ public void setDictionary(byte[] buffer, int off, int len) { if (!needsDictionary()) throw new IllegalStateException(); adler.update(buffer, off, len); if ((int) adler.getValue() != readAdler) throw new IllegalArgumentException("Wrong adler checksum"); adler.reset(); outputWindow.copyDict(buffer, off, len); mode = DECODE_BLOCKS; }
/** * Inflates the compressed stream to the output buffer. If this returns 0, you should check, * whether needsDictionary(), needsInput() or finished() returns true, to determine why no further * output is produced. * * @param buffer the output buffer. * @param off the offset into buffer where the output should start. * @param len the maximum length of the output. * @return the number of bytes written to the buffer, 0 if no further output can be produced. * @exception DataFormatException if deflated stream is invalid. * @exception IndexOutOfBoundsException if the off and/or len are wrong. */ public int inflate(byte[] buf, int off, int len) throws DataFormatException { /* Special case: len may be zero */ if (len == 0) return 0; /* Check for correct buff, off, len triple */ if (0 > off || off > off + len || off + len > buf.length) throw new ArrayIndexOutOfBoundsException(); int count = 0; int more; do { if (mode != DECODE_CHKSUM) { /* Don't give away any output, if we are waiting for the * checksum in the input stream. * * With this trick we have always: * needsInput() and not finished() * implies more output can be produced. */ more = outputWindow.copyOutput(buf, off, len); adler.update(buf, off, more); off += more; count += more; totalOut += more; len -= more; if (len == 0) return count; } } while (decode() || (outputWindow.getAvailable() > 0 && mode != DECODE_CHKSUM)); return count; }
public static String calculateCrc32(String source) { CRC32 crc32 = new CRC32(); Adler32 adler32 = new Adler32(); adler32.update(source.getBytes()); System.out.println(adler32.getValue()); crc32.update(source.getBytes()); StringBuilder builder = new StringBuilder(8); System.out.println(crc32.getValue()); System.out.println(Long.toHexString(crc32.getValue())); builder.append(Long.toHexString(crc32.getValue())); if (builder.length() < 8) { builder.append(CHAR_TEMPLATE, 0, 8 - builder.length()); } System.out.println(builder.toString()); return builder.toString(); }
/** * Decodes the adler checksum after the deflate stream. * * @return false if more input is needed. * @exception DataFormatException if checksum doesn't match. */ private boolean decodeChksum() throws DataFormatException { while (neededBits > 0) { int chkByte = input.peekBits(8); if (chkByte < 0) return false; input.dropBits(8); readAdler = (readAdler << 8) | chkByte; neededBits -= 8; } if ((int) adler.getValue() != readAdler) throw new DataFormatException( "Adler chksum doesn't match: " + Integer.toHexString((int) adler.getValue()) + " vs. " + Integer.toHexString(readAdler)); mode = FINISHED; return false; }
/** * Resets the inflater so that a new stream can be decompressed. All pending input and output will * be discarded. */ public void reset() { mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER; totalIn = totalOut = 0; input.reset(); outputWindow.reset(); dynHeader = null; litlenTree = null; distTree = null; isLastBlock = false; adler.reset(); }
void reset(ZStream z, long[] c) { if (c != null) { c[0] = check; } mode = TYPE; bitk = 0; bitb = 0; read = write = 0; if (checkfn != null) { z.adler = check = Adler32.adler32(0L, null, 0, 0); } }
/** * Gets the adler checksum. This is either the checksum of all uncompressed bytes returned by * inflate(), or if needsDictionary() returns true (and thus no output was yet produced) this is * the adler checksum of the expected dictionary. * * @returns the adler checksum. */ public int getAdler() { return needsDictionary() ? readAdler : (int) adler.getValue(); }
// copy as much as possible from the sliding window to the output area int inflate_flush(ZStream z, int r) { int n; int p; int q; // local copies of source and destination pointers p = z.next_out_index; q = read; // compute number of bytes to copy as far as end of window n = (q <= write ? write : end) - q; if (n > z.avail_out) { n = z.avail_out; } if (n != 0 && r == JZlib.Z_BUF_ERROR) { r = JZlib.Z_OK; } // update counters z.avail_out -= n; z.total_out += n; // update check information if (checkfn != null) { z.adler = check = Adler32.adler32(check, window, q, n); } // copy as far as end of window System.arraycopy(window, q, z.next_out, p, n); p += n; q += n; // see if more to copy at beginning of window if (q == end) { // wrap pointers q = 0; if (write == end) { write = 0; } // compute bytes to copy n = write - q; if (n > z.avail_out) { n = z.avail_out; } if (n != 0 && r == JZlib.Z_BUF_ERROR) { r = JZlib.Z_OK; } // update counters z.avail_out -= n; z.total_out += n; // update check information if (checkfn != null) { z.adler = check = Adler32.adler32(check, window, q, n); } // copy System.arraycopy(window, q, z.next_out, p, n); p += n; q += n; } // update pointers z.next_out_index = p; read = q; // done return r; }