public void BzzWriterTest003() { UTF8Encoding encoding = new UTF8Encoding(false); string filePath = Path.Combine(Util.RepoRoot, "artifacts", "data", "testhello.obz"); filePath = filePath.Replace(".obz", ".bz3"); const string testText = "Hello bzz! \r\n"; long bytesWritten = 0; using (Stream stream = File.Create(filePath, 8192)) using (BzzWriter writer = new BzzWriter(stream)) { // TODO track BzzWriter bug causing side effects during write writer.Write(testText); writer.Flush(); bytesWritten = stream.Position; } byte[] testBuffer = Util.ReadFileToEnd(filePath); //byte[] readBuffer = new byte[bytesWritten + 32]; //Buffer.BlockCopy(testBuffer, 0, readBuffer, 0, (int)bytesWritten); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { string testResult = reader.ReadUTF8String(testText.Length); Assert.False(String.IsNullOrWhiteSpace(testResult)); // Assert.Equal(testText, testResult); } }
public void ReadTest001() { const int expectedLength = 4558626; string filePath = Path.Combine(Util.ArtifactsDataPath, "test042C.djvu.2048.bzz"); string testFilePath = Path.Combine(Util.ArtifactsPath, "test042C.djvu"); byte[] testBuffer = Util.ReadFileToEnd(filePath); byte[] expectedBuffer = Util.ReadFileToEnd(testFilePath); Assert.Equal(expectedBuffer.Length, expectedLength); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { byte[] result = new byte[expectedBuffer.Length]; int readBytes = reader.Read(result, 0, result.Length); Assert.Equal(readBytes, expectedLength); for (int i = 0; i < expectedLength; i++) { if (expectedBuffer[i] != result[i]) { Assert.True(false); } } } }
public void BzzWriterTest001() { UTF8Encoding encoding = new UTF8Encoding(false); string filePath = Path.Combine(Util.RepoRoot, "artifacts", "data", "testbzznbmont.obz"); string testText = File.ReadAllText(filePath, encoding); using (MemoryStream stream = new MemoryStream()) using (BzzWriter writer = new BzzWriter(stream)) { byte[] buffer = encoding.GetBytes(testText); writer.Write(buffer, 0, buffer.Length); writer.Flush(); long bytesWritten = stream.Position; stream.Position = 0; byte[] testBuffer = stream.GetBuffer(); byte[] readBuffer = new byte[bytesWritten + 32]; Buffer.BlockCopy(testBuffer, 0, readBuffer, 0, (int)bytesWritten); using (MemoryStream readStream = new MemoryStream(readBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { string testResult = reader.ReadUTF8String(buffer.Length); Assert.False(String.IsNullOrWhiteSpace(testResult)); // TODO track bug causing roundtrip errors // Assert.Equal(testText, testResult); } } }
public void BzzReaderTest001() { using (MemoryStream stream = new MemoryStream()) using (BzzReader reader = new BzzReader(stream)) { Assert.NotSame(stream, reader.BaseStream); Assert.IsType <BSInputStream>(reader.BaseStream); } }
public void ReadUTF8String() { string testText = "Hello bzz! \r\n"; string filePath = Path.Combine(Util.ArtifactsDataPath, "testhello.bzz"); byte[] testBuffer = Util.ReadFileToEnd(filePath); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { string testResult = reader.ReadUTF8String(13); Assert.False(String.IsNullOrWhiteSpace(testResult)); Assert.Equal(testText, testResult); } }
public void WriteTest003() { string filePath = Path.Combine(Util.ArtifactsPath, "test042C.djvu"); string outFilePath = Path.Combine(Util.ArtifactsDataPath, Path.GetFileName(filePath) + ".2048.bz3"); string refFilePath = Path.Combine(Util.ArtifactsDataPath, Path.GetFileName(filePath) + ".2048.bzz"); try { byte[] buffer = Util.ReadFileToEnd(filePath); byte[] data = new byte[buffer.Length + 32]; Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length); long bytesWritten = 0; using (Stream stream = new FileStream( outFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) using (BSOutputStream writer = new BSOutputStream(stream, 2048)) { writer.Write(data, 0, buffer.Length); bytesWritten = stream.Position; } byte[] testBuffer = Util.ReadFileToEnd(outFilePath); byte[] refBuffer = Util.ReadFileToEnd(refFilePath); Util.AssertBufferEqal(testBuffer, refBuffer); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { byte[] testResult = reader.ReadBytes(buffer.Length); Assert.NotNull(testResult); Assert.Equal(buffer.Length, testResult.Length); for (int i = 0; i < buffer.Length; i++) { Assert.Equal(buffer[i], testResult[i]); } } } finally { if (File.Exists(outFilePath)) { File.Delete(outFilePath); } } }
public void DecoderNoLearn002() { string filePath = Path.Combine(Util.ArtifactsDataPath, "test043C.json.bzz"); byte[] testBuffer = Util.ReadFileToEnd(filePath); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { byte[] result = new byte[testBuffer.Length]; int readBytes = reader.Read(result, 0, result.Length / 2); BSInputStream bsStream = reader.BaseStream as BSInputStream; Assert.NotNull(bsStream); byte ctx = 0x01; int decoded = bsStream.Coder.DecoderNoLearn(ref ctx); } }
public void ReadNullTerminatedStringTest() { string filePath = Path.Combine(Util.ArtifactsDataPath, "testbzznbmont.bz"); string testPath = Path.Combine(Util.ArtifactsDataPath, "testbzznbmont.obz"); byte[] sourceBuffer = Util.ReadFileToEnd(filePath); byte[] testBuffer = Util.ReadFileToEnd(testPath); using (MemoryStream readStream = new MemoryStream(sourceBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { string expected = new UTF8Encoding(false).GetString(testBuffer, 0, testBuffer.Length - 1); string testResult = reader.ReadNullTerminatedString(); Assert.False(String.IsNullOrWhiteSpace(testResult)); Assert.Equal <int>(expected.Length, testResult.Length); Assert.Equal(expected, testResult); } }
public void WriteTest001() { UTF8Encoding encoding = new UTF8Encoding(false); string filePath = Path.Combine(Util.ArtifactsDataPath, "testhello.obz"); string outFilePath = Path.GetTempFileName(); try { byte[] buffer = Util.ReadFileToEnd(filePath); string testText = "Hello bzz! \r\n"; string sourceText = new UTF8Encoding(false).GetString(buffer); Assert.Equal(testText, sourceText); byte[] data = new byte[buffer.Length + 32]; Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length); long bytesWritten = 0; using (Stream stream = new FileStream( outFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) using (BSOutputStream writer = new BSOutputStream(stream, 4096)) { writer.Write(data, 0, buffer.Length); bytesWritten = stream.Position; } byte[] testBuffer = Util.ReadFileToEnd(outFilePath); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { string testResult = reader.ReadUTF8String(testText.Length); Assert.False(String.IsNullOrWhiteSpace(testResult)); Assert.Equal(testText, testResult); } } finally { if (File.Exists(outFilePath)) { File.Delete(outFilePath); } } }
public void WriteTest002() { string filePath = Path.Combine(Util.ArtifactsDataPath, "DjvuNet.pdb"); string outFilePath = Path.GetTempFileName(); string refFilePath = filePath + ".bzz"; try { byte[] buffer = Util.ReadFileToEnd(filePath); byte[] data = new byte[buffer.Length + 32]; Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length); long bytesWritten = 0; using (Stream stream = new FileStream( outFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) using (BSOutputStream writer = new BSOutputStream(stream, 4096)) { writer.Write(data, 0, buffer.Length); bytesWritten = stream.Position; } byte[] testBuffer = Util.ReadFileToEnd(outFilePath); byte[] refBuffer = Util.ReadFileToEnd(refFilePath); Util.AssertBufferEqal(testBuffer, refBuffer); using (MemoryStream readStream = new MemoryStream(testBuffer)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { byte[] testResult = reader.ReadBytes(buffer.Length); Assert.NotNull(testResult); Util.AssertBufferEqal(testResult, buffer); } } finally { if (File.Exists(outFilePath)) { File.Delete(outFilePath); } } }
public void WriteTest004() { string filePath = Path.Combine(Util.ArtifactsDataPath, "LLVMMCJIT.lib"); string outFilePath = Path.Combine(Util.ArtifactsDataPath, Path.GetFileName(filePath) + ".2048.bz3"); string refFilePath = Path.Combine(Util.ArtifactsDataPath, Path.GetFileName(filePath) + ".2048.bzz"); try { byte[] buffer = Util.ReadFileToEnd(filePath); long bytesWritten = 0; using (Stream stream = new FileStream( outFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) using (BSOutputStream writer = new BSOutputStream(stream, 2048)) { writer.Write(buffer, 0, buffer.Length); bytesWritten = stream.Position; } byte[] outputBuffer = Util.ReadFileToEnd(outFilePath); byte[] refBuffer = Util.ReadFileToEnd(refFilePath); Util.AssertBufferEqal(outputBuffer, refBuffer); using (FileStream readStream = new FileStream(outFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (BzzReader reader = new BzzReader(new BSInputStream(readStream))) { byte[] testResult = reader.ReadBytes(buffer.Length); Assert.NotNull(testResult); Util.AssertBufferEqal(testResult, buffer); } } finally { if (File.Exists(outFilePath)) { File.Delete(outFilePath); } } }
/// <summary> /// Reads the compressed data from the djvu file /// </summary> /// <param name="reader"></param> /// <param name="count"></param> /// <param name="compressedSectionLength"></param> internal void ReadCompressedData(IDjvuReader reader, int count, int compressedSectionLength) { long prevPos = reader.Position; BzzReader bzReader = reader.GetBZZEncodedReader(compressedSectionLength); // Read the component sizes for (int x = 0; x < count; x++) { _components[x].Size = bzReader.ReadInt24BigEndian(); } // Read the component flag information for (int x = 0; x < count; x++) { _components[x].DecodeFlags(bzReader.ReadByte()); } // Read the component strings for (int x = 0; x < count; x++) { _components[x].ID = bzReader.ReadNullTerminatedString(); if (_components[x].HasName == true) { _components[x].Name = bzReader.ReadNullTerminatedString(); } if (_components[x].HasTitle == true) { _components[x].Title = bzReader.ReadNullTerminatedString(); } } _isInitialized = true; reader.Position = prevPos; }