/** * 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; }
/** * 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; }
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(); }