public static T Decompress <T>(byte[] bytes) { var ba = MiniLZO.Decompress(bytes); var s = Encoding.UTF8.GetString(ba); return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(s)); }
public static T Decompress <T>(Stream msi) { using (var mso = new MemoryStream()) { msi.CopyTo(mso); var ba = mso.ToArray(); var s = Encoding.UTF8.GetString(MiniLZO.Decompress(ba)); return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(s)); } }
private void ReadBlock(Stream s, int index) { var compSize = _compressedBlockSizes[index]; byte[] compressedData = new byte[compSize]; s.Position = _blockStarts[index]; s.Read(compressedData, 0, compSize); _curBlockData = new byte[_uncompressedBlockSizes[index]]; _curBlockIndex = index; MiniLZO.Decompress(compressedData, _curBlockData); }
public byte[] Decode(byte[] input, int decodedSize) { if (decodedSize < 0) { throw new LoadDataException("Decoded size must be specified for MiniLZO encoding"); } var result = new byte[decodedSize]; MiniLZO.Decompress(input, result); return(result); }
public object Send(object data) { try { CheckConnection(); byte[] hdr = new byte[5]; hdr[0] = (UseBJSON ? (byte)3 : (byte)0); byte[] dat = fastBinaryJSON.BJSON.ToBJSON(data); bool compressed = false; if (dat.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + dat.Length.ToString("#,#")); compressed = true; dat = MiniLZO.Compress(dat); log.Debug("new size : " + dat.Length.ToString("#,#")); } byte[] len = Helper.GetBytes(dat.Length, false); hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(len, 0, hdr, 1, 4); _client.Client.Send(hdr); _client.Client.Send(dat); byte[] rechdr = new byte[5]; using (NetworkStream n = new NetworkStream(_client.Client)) { n.Read(rechdr, 0, 5); int c = Helper.ToInt32(rechdr, 1); byte[] recd = new byte[c]; int bytesRead = 0; int chunksize = 1; while (bytesRead < c && chunksize > 0) { bytesRead += chunksize = n.Read (recd, bytesRead, c - bytesRead); } if ((rechdr[0] & (byte)4) == (byte)4) { recd = MiniLZO.Decompress(recd); } if ((rechdr[0] & (byte)3) == (byte)3) { return(fastBinaryJSON.BJSON.ToObject(recd)); } } } catch { } return(null); }
public static bool MiniLzoDecompress(Stream fsIn, Stream fsOut, out int decompressedSize) { //_checkSum = 0; var decompressedData = new byte[LooseBufferSize]; decompressedSize = 0; //byte b; try { while (true) { var count = (int)fsIn.ReadByte(); count += (fsIn.ReadByte() << 8); var buf = new byte[count]; count = fsIn.Read(buf, 0, count); if (count != 0) { decompressedSize = MiniLZO.Decompress(buf, decompressedData); //_checkSum += AcedUtils.Adler32(_decompData, 0, _decompData.Length); fsOut.Write(decompressedData ?? throw new Exception("Null decompressedData"), 0, decompressedSize); decompressedData = null; } buf = null; if (count != LooseBufferSize) { break; } if (count == 0) { break; } } return(true); } catch (Exception ex) { Debug.WriteLine(ex); } return(false); }
public static bool MiniLzoCompress(Stream fsIn, Stream fsOut) { var buf = new byte[LooseBufferSize]; try { while (true) { var count = fsIn.Read(buf, 0, LooseBufferSize); if (count != 0) { //compressing.Write(buf, 0, count); if (count < LooseBufferSize) { var x = new byte[count]; Array.Copy(buf, x, count); // buf = null; buf = x; } MiniLZO.Compress(buf, out var compData); var b = (byte)(compData.Length & 0xff); fsOut.WriteByte(b); b = (byte)((compData.Length >> 8) & 0xff); fsOut.WriteByte(b); fsOut.Write(compData, 0, compData.Length); // compData = null; } if (count != LooseBufferSize) { break; } } return(true); } catch (Exception ex) { Debug.WriteLine(ex); } return(false); }
private Stream MiniLZODecompressStream(Stream input) { MemoryStream memoryStream = new MemoryStream(); const int BUFFERSIZE = 10000; byte[] buffer = new byte[BUFFERSIZE]; while (true) { int bytesRead = input.Read(buffer, 0, BUFFERSIZE); if (bytesRead == 0) { break; } memoryStream.Write(buffer, 0, bytesRead); } memoryStream.Seek(0, SeekOrigin.Begin); return(new MemoryStream(MiniLZO.Decompress(memoryStream.ToArray()))); }
public void Write(GameBoxWriter w, ClassIDRemap remap) { if (GBX.BodyCompression == 'C') { using (var msBody = new MemoryStream()) using (var gbxwBody = new GameBoxWriter(msBody, this)) { GBX.MainNode.Write(gbxwBody, remap); MiniLZO.Compress(msBody.ToArray(), out byte[] output); w.Write((int)msBody.Length); // Uncompressed w.Write(output.Length); // Compressed w.Write(output, 0, output.Length); // Compressed body data } } else { GBX.MainNode.Write(w); } // ... }
public object Send(object data) { CheckConnection(); byte[] hdr = new byte[5]; hdr[0] = (UseBJSON ? (byte)3 : (byte)0); byte[] dat = fastBinaryJSON.BJSON.ToBJSON(data); byte[] len = Helper.GetBytes(dat.Length, false); Array.Copy(len, 0, hdr, 1, 4); _client.Client.Send(hdr); _client.Client.Send(dat); byte[] rechdr = new byte[5]; using (NetworkStream n = new NetworkStream(_client.Client)) { n.Read(rechdr, 0, 5); int c = Helper.ToInt32(rechdr, 1); byte[] recd = new byte[c]; int bytesRead = 0; int chunksize = 1; while (bytesRead < c && chunksize > 0) { bytesRead += chunksize = n.Read (recd, bytesRead, c - bytesRead); } if ((rechdr[0] & (byte)4) == (byte)4) { recd = MiniLZO.Decompress(recd); } if ((rechdr[0] & (byte)3) == (byte)3) { return(fastBinaryJSON.BJSON.ToObject(recd)); } } return(null); }
void Accept(TcpClient client) { using (NetworkStream n = client.GetStream()) { while (client.Connected) { this.count++; byte[] c = new byte[5]; n.Read(c, 0, 5); int count = BitConverter.ToInt32(c, 1); byte[] data = new byte[count]; int bytesRead = 0; int chunksize = 1; while (bytesRead < count && chunksize > 0) { bytesRead += chunksize = n.Read (data, bytesRead, count - bytesRead); } object o = fastBinaryJSON.BJSON.ToObject(data); object r = _handler(o); bool compressed = false; data = fastBinaryJSON.BJSON.ToBJSON(r); if (data.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + data.Length.ToString("#,#")); compressed = true; data = MiniLZO.Compress(data); log.Debug("new size : " + data.Length.ToString("#,#")); } if (data.Length > NetworkClient.Config.LogDataSizesOver) { log.Debug("data size (bytes) = " + data.Length.ToString("#,#")); } byte[] b = BitConverter.GetBytes(data.Length); byte[] hdr = new byte[5]; hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(b, 0, hdr, 1, 4); n.Write(hdr, 0, 5); n.Write(data, 0, data.Length); int wait = 0; while (n.DataAvailable == false) { wait++; if (wait < 10000) // kludge : for insert performance { Thread.Sleep(0); } else { Thread.Sleep(1); } // FEATURE : if wait > 10 min -> close connection } } } }
void Accept(TcpClient client) { using (NetworkStream n = client.GetStream()) { while (client.Connected) { this.count++; byte[] c = new byte[5]; n.Read(c, 0, 5); int count = BitConverter.ToInt32(c, 1); byte[] data = new byte[count]; int bytesRead = 0; int chunksize = 1; while (bytesRead < count && chunksize > 0) { bytesRead += chunksize = n.Read (data, bytesRead, count - bytesRead); } object o = fastBinaryJSON.BJSON.ToObject(data); if ((c[0] & (byte)4) == (byte)4) { data = MiniLZO.Decompress(data); } object r = _handler(o); bool compressed = false; var dataret = fastBinaryJSON.BJSON.ToBJSON(r); r = null; if (dataret.Length > NetworkClient.Config.CompressDataOver) { log.Debug("compressing data over limit : " + dataret.Length.ToString("#,#")); compressed = true; dataret = MiniLZO.Compress(dataret); log.Debug("new size : " + dataret.Length.ToString("#,#")); } if (dataret.Length > NetworkClient.Config.LogDataSizesOver) { log.Debug("data size (bytes) = " + dataret.Length.ToString("#,#")); } byte[] b = BitConverter.GetBytes(dataret.Length); byte[] hdr = new byte[5]; hdr[0] = (byte)(3 + (compressed ? 4 : 0)); Array.Copy(b, 0, hdr, 1, 4); n.Write(hdr, 0, 5); n.Write(dataret, 0, dataret.Length); //n.Flush(); //return; int wait = 0; bool close = false; var dt = FastDateTime.Now; while (n.DataAvailable == false && close == false) { wait++; if (wait < 10000) // kludge : for insert performance { Thread.Sleep(0); } else { Thread.Sleep(1); // wait done -> close connection if (FastDateTime.Now.Subtract(dt).TotalSeconds > NetworkClient.Config.KillConnectionSeconds) { close = true; } } } if (close) { break; } } n.Close(); } client.Close(); }
public static byte[] Compress(string str) { var bytes = Encoding.UTF8.GetBytes(str); return(MiniLZO.Decompress(bytes)); }
private MemoryStream MiniLZOCompressMemoryStream(MemoryStream input) { return(new MemoryStream(MiniLZO.Compress(input))); }
public static string Decompress(byte[] bytes) { var ba = MiniLZO.Decompress(bytes); return(Encoding.UTF8.GetString(ba)); }
public void Read(byte[] data, int uncompressedSize, IProgress <GameBoxReadProgress> progress = null) { byte[] buffer = new byte[uncompressedSize]; MiniLZO.Decompress(data, buffer); Read(buffer, progress); }
private MemoryStream MiniLZODecompressMemoryStream(MemoryStream input) { return(new MemoryStream(MiniLZO.Decompress(input.ToArray()))); }