/// <remarks> /// Auto-detection only supports up to 32MB files to avoid hogging all the ram. /// </remarks> public async Task <bool> IsOfType(GenericFile file) { if (file.Length > 2 && await file.ReadAsync(0) == 0x78 && new byte[] { 0x1, 0x9C, 0xDA }.Contains(await file.ReadAsync(1)) && file.Length < 32 * 1024 * 1024) { try { using (var compressed = new MemoryStream(await file.ReadAsync())) { compressed.Seek(2, SeekOrigin.Begin); using (var decompressed = new MemoryStream()) { using (var zlib = new DeflateStream(compressed, CompressionMode.Decompress)) { zlib.CopyTo(decompressed); } var rawData = decompressed.ToArray(); } } } catch (Exception) { return(false); } return(true); } else { return(false); } }
public async Task WriteByte_Sequence_Async_Stream() { var testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var f = new GenericFile()) { f.EnableInMemoryLoad = false; f.CreateFile(new byte[8]); await f.WriteByteAsync(testData[0]); await f.WriteByteAsync(testData[1]); await f.WriteByteAsync(testData[2]); await f.WriteByteAsync(testData[3]); await f.WriteByteAsync(testData[4]); await f.WriteByteAsync(testData[5]); await f.WriteByteAsync(testData[6]); await f.WriteByteAsync(testData[7]); Assert.AreEqual(testData[0], await f.ReadAsync(0)); Assert.AreEqual(testData[1], await f.ReadAsync(1)); Assert.AreEqual(testData[2], await f.ReadAsync(2)); Assert.AreEqual(testData[3], await f.ReadAsync(3)); Assert.AreEqual(testData[4], await f.ReadAsync(4)); Assert.AreEqual(testData[5], await f.ReadAsync(5)); Assert.AreEqual(testData[6], await f.ReadAsync(6)); Assert.AreEqual(testData[7], await f.ReadAsync(7)); } }
public async Task Read_Async_Stream() { var testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var f = new GenericFile()) { f.EnableInMemoryLoad = false; f.CreateFile(testData.Clone() as byte[]); Assert.IsTrue(testData.SequenceEqual(await f.ReadAsync())); } }
public virtual async Task <bool> IsOfType(GenericFile file) { for (int i = 0; i < file.Length; i++) { if (await file.ReadAsync(i) == 0) // Ensure there's no null characters { return(false); } } return(true); }
public async Task Write_Async_Memory() { var testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var f = new GenericFile()) { f.EnableInMemoryLoad = true; f.CreateFile(new byte[8]); f.Write(testData); Assert.IsTrue(testData.SequenceEqual(await f.ReadAsync())); } }
public async Task ReadByte_Async_Memory() { var testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var f = new GenericFile()) { f.EnableInMemoryLoad = true; f.CreateFile(testData.Clone() as byte[]); Assert.AreEqual(testData[0], await f.ReadAsync(0)); Assert.AreEqual(testData[1], await f.ReadAsync(1)); Assert.AreEqual(testData[2], await f.ReadAsync(2)); Assert.AreEqual(testData[3], await f.ReadAsync(3)); Assert.AreEqual(testData[4], await f.ReadAsync(4)); Assert.AreEqual(testData[5], await f.ReadAsync(5)); Assert.AreEqual(testData[6], await f.ReadAsync(6)); Assert.AreEqual(testData[7], await f.ReadAsync(7)); } }
public async Task ReadSequence_Async_Stream() { var testData = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; using (var f = new GenericFile()) { f.EnableInMemoryLoad = true; f.CreateFile(testData.Clone() as byte[]); for (int start = 0; start < 7; start++) { for (int length = 1; length < 7 - start; length++) { var value = await f.ReadAsync(start, length); Assert.IsTrue(testData.Skip(start).Take(length).SequenceEqual(value), "Failed to execute ReadAsync(" + start.ToString() + ", " + length.ToString() + ")."); } } } }
public async Task OpenFile(string filename, IFileSystem provider) { using (var file = new GenericFile()) { await file.OpenFile(filename, provider); using (var compressed = new MemoryStream(await file.ReadAsync())) { compressed.Seek(2, SeekOrigin.Begin); using (var decompressed = new MemoryStream()) { using (var zlib = new DeflateStream(compressed, CompressionMode.Decompress)) { zlib.CopyTo(decompressed); } RawData = decompressed.ToArray(); } } } }
public async Task WriteNullTerminatedString_Async() { using (var f = new GenericFile()) { var original = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x50, 0x6F, 0x6B, 0xC3, 0xA9, 0x6D, 0x6F, 0x01 }; f.CreateFile(original); await f.WriteNullTerminatedStringAsync(0, Encoding.UTF8, "Pokémon"); await f.WriteNullTerminatedStringAsync(9, Encoding.UTF8, "1234567"); Assert.IsTrue(new byte[] { 0x50, 0x6F, 0x6B, 0xC3, 0xA9, 0x6D, 0x6F, 0x6E, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x00 }.SequenceEqual(await f.ReadAsync())); } }
public async Task <bool> IsOfType(GenericFile file) { return(await file.ReadAsync(0) == 0xFF); }