public override object Deserialize(Type type, System.IO.Stream stream) { using (var gzip = new LZ4Stream(stream, System.IO.Compression.CompressionMode.Decompress, highCompression: false, blockSize: 1024 * 64)) using (var sr = new StreamReader(gzip, new UTF8Encoding(false))) { return JSON.Deserialize(sr, type, options); } }
public override void Serialize(System.IO.Stream stream, object obj) { // default LZ4Stream buffer size is 1MB but it's too large on WebService serialization using (var lz4 = new LZ4Stream(stream, System.IO.Compression.CompressionMode.Compress, highCompression: false, blockSize: 1024 * 64)) using (var sw = new StreamWriter(lz4, Encoding ?? new UTF8Encoding(false))) { JSON.Serialize(obj, sw, options); } }
/// <summary> /// Decompress a byte array. /// </summary> /// <param name="input">Compressed data.</param> /// <returns>Uncompressed data.</returns> public static byte[] Uncompress(byte[] input) { using (MemoryStream output = new MemoryStream()) { MemoryStream memstream = new MemoryStream(input); LZ4Stream LZStream = new LZ4Stream(memstream, LZ4StreamMode.Decompress); LZStream.CopyTo(output); LZStream.Close(); memstream.Close(); return output.ToArray(); } }
/// <summary> /// Compresses a byte array. /// </summary> /// <param name="input">Uncompressed data.</param> /// <returns>Compressed data.</returns> public static byte[] Compress(byte[] input) { MemoryStream memstream = new MemoryStream(); LZ4Stream lzstream = new LZ4Stream(memstream, LZ4StreamMode.Compress); lzstream.Write(input, 0, input.Length); lzstream.Close(); byte[] finaldata = memstream.ToArray(); memstream.Close(); return finaldata; }