private long bytesToInt(byte[] b, int offset) { int low = bytesToShort(b, offset); int high = bytesToShort(b, offset + 2); return ((long)high << 16) | low; }
public void write(int value) throws IOException { byte[] single = new byte[] { (byte) (value & 0xFF) }; write(single, 0, 1); }
public void write(byte[] b, int off, int len) throws IOException { if (len <= 0) { return; } if (!finishedHeader) { // Keep parsing header data until the header is finished. parseHeader(b, off, len); } else { inflater.setInput(b, off, len); } if (!inflater.finished()) { inflateData(); if (inflater.finished() && inflater.getRemaining() > 0) { // Copy bytes left-over from inflater into trailer buffer. int trailerOffsetInInput = len - inflater.getRemaining(); while (trailerOffset < inflater.getRemaining()) { trailerBytes[trailerOffset++] = b[trailerOffsetInInput++]; } } } else { // Inflator has all the input bytes it needs, anything left over is for the tailer fields. if (trailerOffset + len > trailerBytes.length) { outputStream.close(); throw new IOException("Corrupt GZIP trailer, too many trailer bytes (only 8 expected)"); } System.arraycopy(b, off, trailerBytes, trailerOffset, len); trailerOffset += len; } }
public void close() throws IOException { outputStream.close(); if (inflater != null) { // Check that the data stream has been correctly unzipped. if (trailerOffset < trailerBytes.length) { throw new IOException("Corrupt GZIP trailer, trailer is incomplete. Expected 8 bytes, only have " + trailerOffset); } // Check CRC from trail long trailerCrc = bytesToInt(trailerBytes, 0); if (trailerCrc != crc.getValue()) { throw new IOException("Corrupt GZIP trailer, CRC values mismatch"); } long trailerByteCount = bytesToInt(trailerBytes, 4); if (trailerByteCount != inflater.getTotalOut()) { throw new IOException("Corrupt GZIP trailer, actual size of inflated data mismatch"); } inflater.end(); inflater = null; } }
outputStream = new GZipInflatingOutputStream(outputStream);
public void write(byte[] b, int off, int len) throws IOException { if (len <= 0) { return; } if (!finishedHeader) { // Keep parsing header data until the header is finished. parseHeader(b, off, len); } else { inflater.setInput(b, off, len); } if (!inflater.finished()) { inflateData(); if (inflater.finished() && inflater.getRemaining() > 0) { // Copy bytes left-over from inflater into trailer buffer. int trailerOffsetInInput = len - inflater.getRemaining(); while (trailerOffset < inflater.getRemaining()) { trailerBytes[trailerOffset++] = b[trailerOffsetInInput++]; } } } else { // Inflator has all the input bytes it needs, anything left over is for the tailer fields. if (trailerOffset + len > trailerBytes.length) { outputStream.close(); throw new IOException("Corrupt GZIP trailer, too many trailer bytes (only 8 expected)"); } System.arraycopy(b, off, trailerBytes, trailerOffset, len); trailerOffset += len; } }
public void close() throws IOException { outputStream.close(); if (inflater != null) { // Check that the data stream has been correctly unzipped. if (trailerOffset < trailerBytes.length) { throw new IOException("Corrupt GZIP trailer, trailer is incomplete. Expected 8 bytes, only have " + trailerOffset); } // Check CRC from trail long trailerCrc = bytesToInt(trailerBytes, 0); if (trailerCrc != crc.getValue()) { throw new IOException("Corrupt GZIP trailer, CRC values mismatch"); } long trailerByteCount = bytesToInt(trailerBytes, 4); if (trailerByteCount != inflater.getTotalOut()) { throw new IOException("Corrupt GZIP trailer, actual size of inflated data mismatch"); } inflater.end(); inflater = null; } }
outputStream = new GZipInflatingOutputStream(outputStream);
private long bytesToInt(byte[] b, int offset) { int low = bytesToShort(b, offset); int high = bytesToShort(b, offset + 2); return ((long)high << 16) | low; }
public void write(int value) throws IOException { byte[] single = new byte[] { (byte) (value & 0xFF) }; write(single, 0, 1); }
if (bytesToShort(headerBytes, 0) != GZIP_MAGIC) { outputStream.close(); throw new IOException("Not in GZIP format"); int fieldLength = bytesToShort(headerBytes, headerOffset); expectedHeaderLength += fieldLength; headerOffset += 2 + fieldLength; crc.update(headerBytes, 0, headerOffset); int v = (int)crc.getValue() & 0xffff; if (bytesToShort(headerBytes, headerOffset) != v) { outputStream.close(); throw new IOException("Corrupt GZIP header");
if (bytesToShort(headerBytes, 0) != GZIP_MAGIC) { outputStream.close(); throw new IOException("Not in GZIP format"); int fieldLength = bytesToShort(headerBytes, headerOffset); expectedHeaderLength += fieldLength; headerOffset += 2 + fieldLength; crc.update(headerBytes, 0, headerOffset); int v = (int)crc.getValue() & 0xffff; if (bytesToShort(headerBytes, headerOffset) != v) { outputStream.close(); throw new IOException("Corrupt GZIP header");