public static Buffer compress(Buffer buf, int headerSize, int compressionLevel) { Debug.Assert(supported()); // // Compress the message body, but not the header. // int uncompressedLen = buf.size() - headerSize; byte[] data = buf.b.rawBytes(headerSize, uncompressedLen); int compressedLen = (int)(uncompressedLen * 1.01 + 600); byte[] compressed = new byte[compressedLen]; int rc = _compressBuffer(compressed, ref compressedLen, data, uncompressedLen, compressionLevel, 0, 0); if (rc == BZ_OUTBUFF_FULL) { return(null); } else if (rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffCompress failed"); ex.reason = getBZ2Error(rc); throw ex; } // // Don't bother if the compressed data is larger than the // uncompressed data. // if (compressedLen >= uncompressedLen) { return(null); } Buffer r = new Buffer(); r.resize(headerSize + 4 + compressedLen, false); r.b.position(0); // // Copy the header from the uncompressed stream to the // compressed one. // r.b.put(buf.b.rawBytes(0, headerSize)); // // Add the size of the uncompressed stream before the // message body. // r.b.putInt(buf.size()); // // Add the compressed message body. // r.b.put(compressed, 0, compressedLen); return(r); }
public static Buffer uncompress(Buffer buf, int headerSize, int messageSizeMax) { Debug.Assert(supported()); #if MANAGED return(null); #else buf.b.position(headerSize); int uncompressedSize = buf.b.getInt(); if (uncompressedSize <= headerSize) { throw new Ice.IllegalMessageSizeException("compressed size <= header size"); } if (uncompressedSize > messageSizeMax) { IceInternal.Ex.throwMemoryLimitException(uncompressedSize, messageSizeMax); } int compressedLen = buf.size() - headerSize - 4; byte[] compressed = buf.b.rawBytes(headerSize + 4, compressedLen); int uncompressedLen = uncompressedSize - headerSize; byte[] uncompressed = new byte[uncompressedLen]; int rc = NativeMethods.BZ2_bzBuffToBuffDecompress(uncompressed, ref uncompressedLen, compressed, compressedLen, 0, 0); if (rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffDecompress failed"); ex.reason = getBZ2Error(rc); throw ex; } Buffer r = new Buffer(); r.resize(uncompressedSize, false); // // Copy the header from the compressed buffer to the uncompressed one. // r.b.position(0); r.b.put(buf.b.rawBytes(), 0, headerSize); r.b.put(uncompressed); return(r); #endif }
public static Buffer uncompress(Buffer buf, int headerSize, int messageSizeMax) { Debug.Assert(supported()); #if MANAGED return null; #else buf.b.position(headerSize); int uncompressedSize = buf.b.getInt(); if(uncompressedSize <= headerSize) { throw new Ice.IllegalMessageSizeException("compressed size <= header size"); } if(uncompressedSize > messageSizeMax) { IceInternal.Ex.throwMemoryLimitException(uncompressedSize, messageSizeMax); } int compressedLen = buf.size() - headerSize - 4; byte[] compressed = buf.b.rawBytes(headerSize + 4, compressedLen); int uncompressedLen = uncompressedSize - headerSize; byte[] uncompressed = new byte[uncompressedLen]; int rc = NativeMethods.BZ2_bzBuffToBuffDecompress(uncompressed, ref uncompressedLen, compressed, compressedLen, 0, 0); if(rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffDecompress failed"); ex.reason = getBZ2Error(rc); throw ex; } Buffer r = new Buffer(); r.resize(uncompressedSize, false); // // Copy the header from the compressed buffer to the uncompressed one. // r.b.position(0); r.b.put(buf.b.rawBytes(), 0, headerSize); r.b.put(uncompressed); return r; #endif }
public static Buffer compress(Buffer buf, int headerSize, int compressionLevel) { Debug.Assert(supported()); #if MANAGED return null; #else // // Compress the message body, but not the header. // int uncompressedLen = buf.size() - headerSize; byte[] data = buf.b.rawBytes(headerSize, uncompressedLen); int compressedLen = (int)(uncompressedLen * 1.01 + 600); byte[] compressed = new byte[compressedLen]; int rc = NativeMethods.BZ2_bzBuffToBuffCompress(compressed, ref compressedLen, data, uncompressedLen, compressionLevel, 0, 0); if(rc == BZ_OUTBUFF_FULL) { return null; } else if(rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffCompress failed"); ex.reason = getBZ2Error(rc); throw ex; } // // Don't bother if the compressed data is larger than the // uncompressed data. // if(compressedLen >= uncompressedLen) { return null; } Buffer r = new Buffer(); r.resize(headerSize + 4 + compressedLen, false); r.b.position(0); // // Copy the header from the uncompressed stream to the // compressed one. // r.b.put(buf.b.rawBytes(0, headerSize)); // // Add the size of the uncompressed stream before the // message body. // r.b.putInt(buf.size()); // // Add the compressed message body. // r.b.put(compressed, 0, compressedLen); return r; #endif }
public bool compress(ref BasicStream cstream, int headerSize, int compressionLevel) { #if MANAGED || COMPACT cstream = this; return false; #else if(!_bzlibInstalled) { cstream = this; return false; } // // Compress the message body, but not the header. // int uncompressedLen = size() - headerSize; byte[] uncompressed = _buf.b.rawBytes(headerSize, uncompressedLen); int compressedLen = (int)(uncompressedLen * 1.01 + 600); byte[] compressed = new byte[compressedLen]; int rc = NativeMethods.BZ2_bzBuffToBuffCompress(compressed, ref compressedLen, uncompressed, uncompressedLen, compressionLevel, 0, 0); if(rc == BZ_OUTBUFF_FULL) { cstream = null; return false; } else if(rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffCompress failed"); ex.reason = getBZ2Error(rc); throw ex; } // // Don't bother if the compressed data is larger than the // uncompressed data. // if(compressedLen >= uncompressedLen) { return false; } cstream = new BasicStream(instance_); cstream.resize(headerSize + 4 + compressedLen, false); cstream.pos(0); // // Copy the header from the uncompressed stream to the // compressed one. // cstream._buf.b.put(_buf.b.rawBytes(0, headerSize)); // // Add the size of the uncompressed stream before the // message body. // cstream.writeInt(size()); // // Add the compressed message body. // cstream._buf.b.put(compressed, 0, compressedLen); return true; #endif }
public BasicStream uncompress(int headerSize) { #if MANAGED || COMPACT return this; #else if(!_bzlibInstalled) { return this; } pos(headerSize); int uncompressedSize = readInt(); if(uncompressedSize <= headerSize) { throw new Ice.IllegalMessageSizeException("compressed size <= header size"); } int compressedLen = size() - headerSize - 4; byte[] compressed = _buf.b.rawBytes(headerSize + 4, compressedLen); int uncompressedLen = uncompressedSize - headerSize; byte[] uncompressed = new byte[uncompressedLen]; int rc = NativeMethods.BZ2_bzBuffToBuffDecompress(uncompressed, ref uncompressedLen, compressed, compressedLen, 0, 0); if(rc < 0) { Ice.CompressionException ex = new Ice.CompressionException("BZ2_bzBuffToBuffDecompress failed"); ex.reason = getBZ2Error(rc); throw ex; } BasicStream ucStream = new BasicStream(instance_); ucStream.resize(uncompressedSize, false); ucStream.pos(0); ucStream._buf.b.put(_buf.b.rawBytes(0, headerSize)); ucStream._buf.b.put(uncompressed, 0, uncompressedLen); return ucStream; #endif }