private void WriterMain() { Message("Starting Write"); try { var f = new BinaryFormatter(); using (var compressor = new LZ4.LZ4Stream(netStream, LZ4.LZ4StreamMode.Compress)) { while (!closed) { var obj = compressQueue.Take(); //Message("sending "+obj); f.Serialize(compressor, obj); //Message("flushing"); //compressor.Flush(); //netStream.Flush(); } } } catch (ObjectDisposedException) { } catch (SocketException) { } catch (Exception ex) { Error(ex); } Close(); }
public static NetMessage Deserialize(byte[] data) { using (var stream = new LZ4.LZ4Stream(new MemoryStream(data), LZ4.LZ4StreamMode.Decompress)) { return(Serializer.Deserialize <NetMessage>(stream)); } }
public T ReadCompressSharpLZ4(string file) { int sz = 0; using (FileStream fs = new FileStream(file, FileMode.Open)) { using (BinaryReader binaryReader = new BinaryReader(fs)) { int length = binaryReader.ReadInt32(); //read the length first //if (length < 0) { length = 0; } byte[] bytesUncompressed = new byte[length]; //you can convert this back to the object using a MemoryStream ;) using (var stream = new LZ4.LZ4Stream(fs, LZ4.LZ4StreamMode.Decompress, true, length)) { try { while ((sz = stream.Read(bytesUncompressed, 0, bytesUncompressed.Length)) > 0) { // ... } } catch (System.IO.EndOfStreamException ex) { } catch (System.NotSupportedException ex) { } var serializer = new DataContractSerializer(typeof(T)); using (var ms = new MemoryStream(bytesUncompressed)) { //if (length == 0) { return default(T); } return((T)serializer.ReadObject(ms)); } } } } }
public static NetMessage Deserialize(byte[] data) { using (var stream = new LZ4.LZ4Stream(new MemoryStream(data), LZ4.LZ4StreamMode.Decompress)) { return Serializer.Deserialize<NetMessage>(stream); } }
public static byte[] Compress(object serializable) { using (MemoryStream ms = new MemoryStream()) using (var stream = new LZ4.LZ4Stream(Helper.Serialize(serializable), LZ4.LZ4StreamMode.Compress)) { stream.CopyTo(ms); return(ms.ToArray()); } }
protected override object DeserializeCore(Type type, byte[] value) { using (var ms = new MemoryStream(value)) using (var lz4 = new LZ4Stream(ms, System.IO.Compression.CompressionMode.Decompress, blockSize: blockSize)) using (var sr = new StreamReader(lz4, Encoding.UTF8)) { var result = Jil.JSON.Deserialize(sr, type); return(result); } }
protected override object DeserializeCore(Type type, byte[] value) { using (var ms = new MemoryStream(value)) using (var lz4 = new LZ4Stream(ms, System.IO.Compression.CompressionMode.Decompress, blockSize: blockSize)) using (var sr = new StreamReader(lz4, Encoding.UTF8)) { var result = Jil.JSON.Deserialize(sr, type); return result; } }
internal static System.IO.FileInfo CompressLZ4(System.IO.FileInfo OriginFile) { string destiFullName = string.Format("{0}{1}.l4z", System.IO.Path.GetTempPath(), OriginFile.Name); using (var istream = OriginFile.OpenRead()) using (var ostream = new System.IO.FileStream(destiFullName, System.IO.FileMode.Create)) using (var lzStream = new LZ4.LZ4Stream(ostream, LZ4.LZ4StreamMode.Compress)) { istream.CopyTo(lzStream); } return(new System.IO.FileInfo(destiFullName)); }
internal static System.IO.FileInfo DecompressLZ4(System.IO.FileInfo CompressedFile) { string destinFile = CompressedFile.FullName.Replace(CompressedFile.Extension, ""); using (var istream = new System.IO.FileStream(CompressedFile.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read)) using (var ostream = new System.IO.FileStream(destinFile, System.IO.FileMode.Create)) using (var lzStream = new LZ4.LZ4Stream(istream, LZ4.LZ4StreamMode.Decompress)) { lzStream.CopyTo(ostream); } return(new System.IO.FileInfo(destinFile)); }
public byte[] Serialize() { if (m_serializedBuffer == null) { using (var stream = new MemoryStream()) { using (var lzStream = new LZ4.LZ4Stream(stream, LZ4.LZ4StreamMode.Compress)) Serializer.Serialize(lzStream, this); m_serializedBuffer = stream.ToArray(); } } return m_serializedBuffer; }
public byte[] Serialize() { if (m_serializedBuffer == null) { using (var stream = new MemoryStream()) { using (var lzStream = new LZ4.LZ4Stream(stream, LZ4.LZ4StreamMode.Compress)) Serializer.Serialize(lzStream, this); m_serializedBuffer = stream.ToArray(); } } return(m_serializedBuffer); }
protected override byte[] SerializeCore(object value, out long resultSize) { using (var ms = new MemoryStream()) { using (var lz4 = new LZ4Stream(ms, System.IO.Compression.CompressionMode.Compress, highCompression, blockSize)) using (var sw = new StreamWriter(lz4)) { Jil.JSON.Serialize(value, sw); } var result = ms.ToArray(); resultSize = result.Length; return(result); } }
protected override byte[] SerializeCore(object value, out long resultSize) { using (var ms = new MemoryStream()) { using (var lz4 = new LZ4Stream(ms, System.IO.Compression.CompressionMode.Compress, highCompression, blockSize)) using (var sw = new StreamWriter(lz4)) { Jil.JSON.Serialize(value, sw); } var result = ms.ToArray(); resultSize = result.Length; return result; } }
public static void Decode(string encoded, string decoded, int chunk) { using (var source = new RefStream(File.OpenRead(encoded), RefStreamMode.Decompress)) using (var target = File.Create(decoded)) { var buffer = new byte[chunk]; while (true) { var read = source.Read(buffer, 0, buffer.Length); if (read == 0) { break; } target.Write(buffer, 0, read); } } }
//Json End public void CompressAndSaveLZ4(T obj, string fileName) { using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate)) { using (MemoryStream objectSerialization = new MemoryStream()) { var byt = GetBytes(obj); using (BinaryWriter binaryWriter = new BinaryWriter(fs)) { binaryWriter.Write(byt.Length); //write the length first using (var stream = new LZ4.LZ4Stream(fs, LZ4.LZ4StreamMode.Compress, true, byt.Count())) { stream.Write(byt, 0, byt.Length); } } } } }
public static void Encode(string original, string encoded, bool high, int block, int chunk) { var flags = high ? RefStreamFlags.HighCompression : RefStreamFlags.None; using (var input = File.OpenRead(original)) using (var encode = new RefStream(File.Create(encoded), RefStreamMode.Compress, flags)) { var buffer = new byte[chunk]; while (true) { var read = input.Read(buffer, 0, buffer.Length); if (read == 0) { break; } encode.Write(buffer, 0, read); } } }
public static byte[] Compress(SDS sds) { var serial = new Newtonsoft.Json.JsonSerializer(); StringBuilder str = new StringBuilder(); TextWriter w = new StringWriter(str); foreach (var e in sds.FinalEntities) { serial.Serialize(w, e); } using (MemoryStream ms = new MemoryStream()) using (var stream = new LZ4.LZ4Stream(Helper.Serialize(sds), LZ4.LZ4StreamMode.Compress)) { stream.CopyTo(ms); return(ms.ToArray()); } }
public async Task GetSingle(RequestContext <IScenePeerClient> ctx) { var userId = ctx.RemotePeer.GetUserData <string>(); var profileId = ctx.ReadObject <string>(); if (string.IsNullOrEmpty(profileId)) { throw new ArgumentNullException($"profileId is null. userId={userId}"); } var profile = await this._profileService.GetProfile(profileId); if (profile == null) { profile = await CreatePlayerProfile(ctx.RemotePeer); } if (profile != null) { EnsureUserOwnsProfile(userId, profile, "User {0} cannot retrieve a profile owned by user {1}."); } //_logger.Log(LogLevel.Debug, "profiles", "Get single ", new { metadata = ctx.RemotePeer.Metadata }); if (!ctx.RemotePeer.Metadata.ContainsKey("bfg.profiles.compression")) { ctx.SendValue(profile); } else { ctx.SendValue(s => { using (var lzStream = new LZ4.LZ4Stream(s, LZ4.LZ4StreamMode.Compress, LZ4.LZ4StreamFlags.HighCompression)) { ctx.RemotePeer.Serializer().Serialize(profile, lzStream); } }); } }
static void Main(string[] args) { var testCount = 1; var testFile = "test.txt"; GenerateFile(testFile); var testData = File.ReadAllBytes(testFile); int size = 0; ExecuteTest(testCount, "Copy", testData, (data) => { var copy = new byte[data.Length]; data.CopyTo(copy, 0); return(copy.Length); }); ExecuteTest(testCount, "ft.lz4", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var lz4 = FT.LZ4.LZ4Stream.Encode(memoryStream)) { lz4.Write(data, 0, data.Length); lz4.Flush(); return((int)memoryStream.Length); } } }); ExecuteTest(testCount, "Gzip", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var gz = new GZipStream(memoryStream, CompressionMode.Compress)) { gz.Write(data, 0, data.Length); gz.Flush(); return((int)memoryStream.Length); } } }); ExecuteTest(testCount, "deflate", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var deflate = new DeflateStream(memoryStream, CompressionMode.Compress)) { deflate.Write(data, 0, data.Length); deflate.Flush(); return((int)memoryStream.Length); } } }); /* * ExecuteTest(testCount, "k40s", testData, (data) => * { * using (var memoryStream = new MemoryStream(64 * 1024)) * { * using (var lz4 = K4os.Compression.LZ4.Streams.LZ4Stream.Encode(memoryStream)) * { * lz4.Write(data, 0, data.Length); * lz4.Flush(); * return (int)memoryStream.Length; * } * } * }); */ ExecuteTest(testCount, "lz4net", testData, (data) => { using (var memoryStream = new MemoryStream(64 * 1024)) { using (var lz4 = new LZ4.LZ4Stream(memoryStream, CompressionMode.Compress)) { lz4.Write(data, 0, data.Length); lz4.Flush(); return((int)memoryStream.Length); } } }); Console.ReadKey(); }
private void ThreadMain() { if (lastHost.IsEmpty) { return; } Debug.Log("Sector: Thread active"); try { while (!stop) { try { Debug.Log("Attempting to connect to " + lastHost); client = new TcpClient(lastHost.Host, lastHost.Port); var stream = new LZ4.LZ4Stream(client.GetStream(), LZ4.LZ4StreamMode.Decompress); var f = new BinaryFormatter(); CSLogicProvider.AsyncFactory = ResolveProvider; Debug.Log("Sector: Connected to " + lastHost); while (client.Connected) { Debug.Log("Sector: Deserializing next object"); var obj = f.UnsafeDeserialize(stream, null); Debug.Log("Sector: Deserialized object " + obj); if (obj is ShardID) { privateID = (ShardID)obj; Debug.Log("Sector: ID updated to " + privateID); OnNewNeighbor(privateID, lastHost); } else if (obj is ObserverTimingInfo) { secondsPerTLG = (float)((ObserverTimingInfo)obj).msPerTLG / 1000f; } else if (obj is CSLogicProvider) { var prov = (CSLogicProvider)obj; TaskCompletionSource <CSLogicProvider> entry; while (!providerMap.TryGetValue(prov.AssemblyName, out entry)) { providerMap.TryAdd(prov.AssemblyName, new TaskCompletionSource <CSLogicProvider>()); } if (!entry.Task.IsCompleted) { entry.SetResult(prov); } Debug.Log("Sector: Added new provider " + prov.AssemblyName); } else if (obj is FullShardAddress) { var h = (FullShardAddress)obj; newNeighbors.Add(h); } else if (obj is SDS) { SDS sds = (SDS)obj; //Debug.Log("Sector: Got new SDS. Deserializing entities..."); foreach (var e in sds.FinalEntities) { var logic = e.MyLogic as DynamicCSLogic; if (logic != null) { try { logic.FinishLoading(e.ID, TimeSpan.FromSeconds(1)); } catch (ExecutionException ex) { Debug.LogException(ex); } } } //Debug.Log("Sector: SDS processed. Signalling change"); SDS = sds; sdsChanged = true; } } } catch (SocketException ex) { Debug.LogException(ex); } catch (IOException ex) { Debug.LogException(ex); } catch (Exception ex) { Debug.LogException(ex); Debug.Log("Weird type: " + ex.GetType()); } try { client.Close(); } catch { }; { var naddr = BaseDB.TryGetAddress(ExpectedID); if (!naddr.IsEmpty) { lastHost = naddr.ObserverAddress; } } Debug.Log("Waiting, then retrying"); Thread.Sleep(2000); } } catch (Exception ex) { Debug.LogError("Encountered terminal exception"); Debug.LogException(ex); } }