/// <summary> /// Creates a new file, converts the value to binary, writes the contents to the file, and then closes the file. /// If the target file already exists, it is overwritten. /// </summary> /// <param name="fileLocation"></param> /// <param name="value">The object to convert to binary.</param> /// <param name="compressionType">The type of compression to use.</param> public static async Task WriteAsync <T>(string fileLocation, T value, CompressionType compressionType) { Utility.UpdateExtension(ref fileLocation, FileType.Binary); var bytes = ZeroFormatterSerializer.Serialize(value); await Text.WriteAsync(fileLocation, bytes, compressionType).ConfigureAwait(false); }
/// <summary> /// Opens a binary file, parses the text in the file into a single object specified by a generic type parameter, and then closes the file. /// </summary> /// <param name="fileLocation"></param> /// <param name="compressionType">The type of decompression to use. /// Must match the type of compression used to write the file.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the specified generic type parameter.</returns> public static async Task <T> ReadAsync <T>(string fileLocation, CompressionType compressionType) { Utility.UpdateExtension(ref fileLocation, FileType.Binary); var bytes = await Text.ReadAsBytesAsync(fileLocation, compressionType).ConfigureAwait(false); return(ZeroFormatterSerializer.Deserialize <T>(bytes)); }
/// <summary> /// Opens a binary file, parses the text in the file into a single object specified by a generic type parameter, and then closes the file. /// </summary> /// <param name="fileLocation"></param> /// <param name="compressionType">The type of decompression to use. /// Must match the type of compression used to write the file.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the specified generic type parameter.</returns> public static T Read <T>(string fileLocation, CompressionType compressionType) { Utility.UpdateExtension(ref fileLocation, FileType.Binary); var bytes = Text.ReadAsBytes(fileLocation, compressionType); return(ZeroFormatterSerializer.Deserialize <T>(bytes)); }
/// <summary> /// Attempts to create a new file, convert the value to binary, write the contents to the file, and then close the file. /// If the target file already exists, it is overwritten. /// Catches all exceptions. Returns true if successful. /// </summary> /// <param name="fileLocation"></param> /// <param name="value">The object to parse to binary.</param> /// <param name="compressionType">The type of compression to use.</param> /// <returns>Returns true if successful.</returns> public static bool TryWrite(string fileLocation, object value, CompressionType compressionType) { try { Write(fileLocation, value, compressionType); return(true); } catch (Exception e) { return(false); } }
/// <summary> /// Attempts to open a binary file, /// parse the text in the file into a single object specified by a generic type parameter, /// and then close the file. Catches all exceptions. Returns true if successful. /// </summary> /// <param name="fileLocation"></param> /// <param name="compressionType">The type of decompression to use. /// Must match the type of compression used to write the file.</param> /// <param name="result">The object of the specified generic type parameter parsed from JSON.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>Returns true if successful.</returns> public static bool TryRead <T>(string fileLocation, CompressionType compressionType, out T result) { try { result = Read <T>(fileLocation, compressionType); return(true); } catch (Exception e) { result = default; return(false); } }
/// <summary> /// Parses the encoded bytes representing a binary-formatted value into an instance of the type specified by /// a generic type parameter. /// </summary> /// <param name="bytes">The bytes to parse.</param> /// <param name="compressionType">The type of decompression to use. /// Must match the type of compression used to write the file.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the specified generic type parameter.</returns> public static async Task <T> FromBytesAsync <T>(byte[] bytes, CompressionType compressionType) { if (compressionType == CompressionType.None) { return(await FromBytesAsync <T>(bytes).ConfigureAwait(false)); } using (var decompressedStream = new MemoryStream()) { using (var compressedStream = new MemoryStream(bytes)) { await Compressor.Decompress(compressedStream, decompressedStream, compressionType) .ConfigureAwait(false); } return(ZeroFormatterSerializer.Deserialize <T>(decompressedStream)); } }
/// <summary> /// Parses the encoded bytes representing a binary-formatted value into an instance of the type specified by /// a generic type parameter. /// </summary> /// <param name="bytes">The bytes to parse.</param> /// <param name="compressionType">The type of decompression to use. /// Must match the type of compression used to write the file.</param> /// <typeparam name="T">The type of object to create an instance of.</typeparam> /// <returns>An instance of the specified generic type parameter.</returns> public static T FromBytes <T>(byte[] bytes, CompressionType compressionType) { if (compressionType == CompressionType.None) { return(FromBytes <T>(bytes)); } using (var decompressedStream = new MemoryStream()) { using (var compressedStream = new MemoryStream(bytes)) { Task.Run(() => Compressor.Decompress(compressedStream, decompressedStream, compressionType)).GetAwaiter().GetResult(); } return(ZeroFormatterSerializer.Deserialize <T>(decompressedStream)); } }
/// <summary> /// Converts the value of a type specified by a generic type parameter into a binary-formatted array of bytes. /// </summary> /// <param name="value">The object to parse to binary.</param> /// <param name="compressionType">The type of compression to use.</param> /// <returns>A binary-formatted array of bytes, parsed from the given object.</returns> public static async Task <byte[]> GetBytesAsync(object value, CompressionType compressionType) { var bytes = await GetBytesAsync(value).ConfigureAwait(false); if (compressionType == CompressionType.None) { return(bytes); } using (var compressedStream = new MemoryStream()) { using (var decompressedStream = new MemoryStream(bytes)) { await Compressor.Compress(decompressedStream, compressedStream, compressionType) .ConfigureAwait(false); } return(compressedStream.ToArray()); } }
/// <summary> /// Converts the value of a type specified by a generic type parameter into a binary-formatted array of bytes. /// </summary> /// <param name="value">The object to parse to binary.</param> /// <param name="compressionType">The type of compression to use.</param> /// <returns>A binary-formatted array of bytes, parsed from the given object.</returns> public static byte[] GetBytes(object value, CompressionType compressionType) { var bytes = GetBytes(value); if (compressionType == CompressionType.None) { return(bytes); } using (var compressedStream = new MemoryStream()) { using (var decompressedStream = new MemoryStream(bytes)) { Task.Run(() => Compressor.Compress(decompressedStream, compressedStream, compressionType)).GetAwaiter() .GetResult(); } return(compressedStream.ToArray()); } }
/// <summary> /// Creates a new file, converts the value to binary, writes the contents to the file, and then closes the file. /// If the target file already exists, it is overwritten. /// </summary> /// <param name="fileLocation"></param> /// <param name="value">The object to convert to binary.</param> /// <param name="compressionType">The type of compression to use.</param> public static void Write <T>(string fileLocation, T value, CompressionType compressionType) { Utility.UpdateExtension(ref fileLocation, FileType.Binary); Text.Write(fileLocation, ZeroFormatterSerializer.Serialize(value), compressionType); }