public static T Load <T>(string directoryPath, string name, Func <T> defaultValueFactory) where T : RocketPackMessageBase <T> { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } foreach (string extension in new string[] { ".rpk.gz", ".rpk.gz.bak" }) { string configPath = Path.Combine(directoryPath, name + extension); var hub = new Hub(); try { if (!File.Exists(configPath)) { continue; } using (var fileStream = new UnbufferedFileStream(configPath, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.None, BufferPool.Shared)) using (var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress)) { for (; ;) { var readLength = gzipStream.Read(hub.Writer.GetSpan(1024 * 4)); if (readLength < 0) { break; } hub.Writer.Advance(readLength); } } hub.Writer.Complete(); var result = RocketPackMessageBase <T> .Import(hub.Reader.GetSequence(), BufferPool.Shared); hub.Reader.Complete(); return(result); } catch (Exception e) { _logger.Error(e); } finally { hub.Reset(); } } return(defaultValueFactory()); }
public static T StreamToMessage <T>(Stream inStream) where T : RocketPackMessageBase <T> { using var hub = new Hub(); const int bufferSize = 4096; while (inStream.Position < inStream.Length) { var readLength = inStream.Read(hub.Writer.GetSpan(bufferSize)); if (readLength < 0) { break; } hub.Writer.Advance(readLength); } hub.Writer.Complete(); return(RocketPackMessageBase <T> .Import(hub.Reader.GetSequence(), BufferPool.Shared)); }