Exemplo n.º 1
0
        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);
                        }
                }
        }
Exemplo n.º 2
0
        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);
                }
        }
Exemplo n.º 3
0
        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);
                }
        }
Exemplo n.º 4
0
        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);
                }
            }
        }