/** * Compress the data and return a new GZIP compressed byte array. * The compressed data conforms to RFC 1952, * with a 10-byte gzip header and a 8-byte gzip checksum footer. * * Prior to 0.9.29, this would return a zero-length output * for a zero-length input. As of 0.9.29, output is valid for * a zero-length input also. * * @throws IllegalArgumentException if size is over 40KB * @throws IllegalStateException on compression failure, as of 0.9.29 * @return null if orig is null */ public static byte[] compress(byte orig[], int offset, int size) { return compress(orig, offset, size, MAX_COMPRESSION); }
/** * Compress the data and return a new GZIP compressed byte array. * The compressed data conforms to RFC 1952, * with a 10-byte gzip header and a 8-byte gzip checksum footer. * * Prior to 0.9.29, this would return a zero-length output * for a zero-length input. As of 0.9.29, output is valid for * a zero-length input also. * * @throws IllegalArgumentException if input size is over 40KB * @throws IllegalStateException on compression failure, as of 0.9.29 * @return null if orig is null */ public static byte[] compress(byte orig[]) { return compress(orig, 0, orig.length); }
/** * calculate the message body's length (not including the header and footer) * * @throws IllegalStateException */ protected int calculateWrittenLength() { // TODO if _byteCache is non-null, don't check _dbEntry if (_dbEntry == null) throw new IllegalStateException("Missing entry"); int len = Hash.HASH_LENGTH + 1 + 4; // key+type+replyToken if (_replyToken > 0) len += 4 + Hash.HASH_LENGTH; // replyTunnel+replyGateway int type = _dbEntry.getType(); if (_dbEntry.isLeaseSet()) { if (_byteCache == null) { _byteCache = _dbEntry.toByteArray(); } } else if (type == DatabaseEntry.KEY_TYPE_ROUTERINFO) { // only decompress once if (_byteCache == null) { byte uncompressed[] = _dbEntry.toByteArray(); _byteCache = DataHelper.compress(uncompressed); } len += 2; } else { throw new IllegalStateException("Invalid key type " + type); } len += _byteCache.length; return len; }
@Test public void testCompress() throws Exception{ Random r = new Random(); for (int size = 0; size < 32*1024; size+=32){ // Original had size++, changed value because // speed was a problem. -Comwiz byte data[] = new byte[size]; r.nextBytes(data); byte compressed[] = DataHelper.compress(data); byte decompressed[] = DataHelper.decompress(compressed); assertTrue(DataHelper.eq(data, decompressed)); } }
/** * @return gzip compressed payload, ready to send * @since 0.9.14 */ private byte[] prepPayload(byte[] payload, int offset, int size, int proto, int fromPort, int toPort, SendMessageOptions.GzipOption gzo) throws I2PSessionException { verifyOpen(); updateActivity(); boolean docompress; if (gzo == SendMessageOptions.GzipOption.DEFAULT) docompress = shouldCompress(size); else docompress = gzo == SendMessageOptions.GzipOption.GZIP_ON; if (docompress) payload = DataHelper.compress(payload, offset, size); else payload = DataHelper.compress(payload, offset, size, DataHelper.NO_COMPRESSION); setProto(payload, proto); setFromPort(payload, fromPort); setToPort(payload, toPort); _context.statManager().addRateData("i2cp.tx.msgCompressed", payload.length); _context.statManager().addRateData("i2cp.tx.msgExpanded", size); return payload; }