Beispiel #1
0
        public void ByteArrayFromStream()
        {
            var data = new byte[64*1024];
            for (int i = 0; i < data.Length; i++) {
                data[i] = unchecked((byte)i);
            }

            using (var ms = new MemoryStream()) {
                var writer = new NbtWriter(ms, "root");
                {
                    byte[] buffer = new byte[1024];
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray1", dataStream, data.Length);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray2", dataStream, data.Length, buffer);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray3", dataStream, 1);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray4", dataStream, 1, buffer);
                    }

                    writer.BeginList("innerLists", NbtTagType.ByteArray, 4);
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, data.Length);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, data.Length, buffer);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, 1);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, 1, buffer);
                    }
                    writer.EndList();
                }
                writer.EndCompound();
                writer.Finish();

                ms.Position = 0;
                var file = new NbtFile();
                file.LoadFromStream(ms, NbtCompression.None);
                CollectionAssert.AreEqual(data, file.RootTag["byteArray1"].ByteArrayValue);
                CollectionAssert.AreEqual(data, file.RootTag["byteArray2"].ByteArrayValue);
                Assert.AreEqual(1, file.RootTag["byteArray3"].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["byteArray3"].ByteArrayValue[0]);
                Assert.AreEqual(1, file.RootTag["byteArray4"].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["byteArray4"].ByteArrayValue[0]);

                CollectionAssert.AreEqual(data, file.RootTag["innerLists"][0].ByteArrayValue);
                CollectionAssert.AreEqual(data, file.RootTag["innerLists"][1].ByteArrayValue);
                Assert.AreEqual(1, file.RootTag["innerLists"][2].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["innerLists"][2].ByteArrayValue[0]);
                Assert.AreEqual(1, file.RootTag["innerLists"][3].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["innerLists"][3].ByteArrayValue[0]);
            }
        }
Beispiel #2
0
 public void LoadingBigFileStream()
 {
     byte[] fileBytes = File.ReadAllBytes( "TestFiles/bigtest.nbt" );
     using( MemoryStream ms = new MemoryStream( fileBytes ) ) {
         using( NonSeekableStream nss = new NonSeekableStream( ms ) ) {
             var file = new NbtFile();
             int length = file.LoadFromStream( nss, NbtCompression.None, null );
             AssertNbtBigFile( file );
             Assert.AreEqual( length, new FileInfo( "TestFiles/bigtest.nbt" ).Length );
         }
     }
 }
Beispiel #3
0
 public void LoadingBigFileStream()
 {
     byte[] fileBytes = File.ReadAllBytes(TestFiles.Big);
     using (var ms = new MemoryStream(fileBytes)) {
         using (var nss = new NonSeekableStream(ms)) {
             var file = new NbtFile();
             long length = file.LoadFromStream(nss, NbtCompression.None, null);
             TestFiles.AssertNbtBigFile(file);
             Assert.AreEqual(length, new FileInfo(TestFiles.Big).Length);
         }
     }
 }
Beispiel #4
0
        public void ByteArrayFromStream()
        {
            var data = new byte[64*1024];
            for (int i = 0; i < data.Length; i++) {
                data[i] = unchecked((byte)i);
            }

            using (var ms = new MemoryStream()) {
                var writer = new NbtWriter(ms, "root");
                {
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray", dataStream, data.Length);
                    }
                }
                writer.EndCompound();
                writer.Finish();

                ms.Position = 0;
                var file = new NbtFile();
                file.LoadFromStream(ms, NbtCompression.None);
                CollectionAssert.AreEqual(file.RootTag["byteArray"].ByteArrayValue, data);
            }
        }
Beispiel #5
0
        public void ByteArrayFromStream()
        {
            var data = new byte[64 * 1024];

            for (int i = 0; i < data.Length; i++)
            {
                data[i] = unchecked ((byte)i);
            }

            using (var ms = new MemoryStream()) {
                var writer = new NbtWriter(ms, "root");
                {
                    byte[] buffer = new byte[1024];
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray1", dataStream, data.Length);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray2", dataStream, data.Length, buffer);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray3", dataStream, 1);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray("byteArray4", dataStream, 1, buffer);
                    }

                    writer.BeginList("innerLists", NbtTagType.ByteArray, 4);
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, data.Length);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, data.Length, buffer);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, 1);
                    }
                    using (var dataStream = new NonSeekableStream(new MemoryStream(data))) {
                        writer.WriteByteArray(dataStream, 1, buffer);
                    }
                    writer.EndList();
                }
                writer.EndCompound();
                writer.Finish();

                ms.Position = 0;
                var file = new NbtFile();
                file.LoadFromStream(ms, NbtCompression.None);
                CollectionAssert.AreEqual(data, file.RootTag["byteArray1"].ByteArrayValue);
                CollectionAssert.AreEqual(data, file.RootTag["byteArray2"].ByteArrayValue);
                Assert.AreEqual(1, file.RootTag["byteArray3"].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["byteArray3"].ByteArrayValue[0]);
                Assert.AreEqual(1, file.RootTag["byteArray4"].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["byteArray4"].ByteArrayValue[0]);

                CollectionAssert.AreEqual(data, file.RootTag["innerLists"][0].ByteArrayValue);
                CollectionAssert.AreEqual(data, file.RootTag["innerLists"][1].ByteArrayValue);
                Assert.AreEqual(1, file.RootTag["innerLists"][2].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["innerLists"][2].ByteArrayValue[0]);
                Assert.AreEqual(1, file.RootTag["innerLists"][3].ByteArrayValue.Length);
                Assert.AreEqual(data[0], file.RootTag["innerLists"][3].ByteArrayValue[0]);
            }
        }
Beispiel #6
0
 public void ReloadNonSeekableStream()
 {
     NbtFile loadedFile = new NbtFile( "TestFiles/bigtest.nbt" );
     using( MemoryStream ms = new MemoryStream() ) {
         using( NonSeekableStream nss = new NonSeekableStream( ms ) ) {
             int bytesWritten = loadedFile.SaveToStream( nss, NbtCompression.None );
             ms.Position = 0;
             int bytesRead = loadedFile.LoadFromStream( nss, NbtCompression.None, null );
             Assert.AreEqual( bytesWritten, bytesRead );
             AssertNbtBigFile( loadedFile );
         }
     }
 }
Beispiel #7
0
 public void NonSeekableStreamSkip()
 {
     byte[] fileBytes = File.ReadAllBytes("TestFiles/bigtest.nbt");
     using (var ms = new MemoryStream(fileBytes)) {
         using (var nss = new NonSeekableStream(ms)) {
             var reader = new NbtReader(nss);
             reader.ReadToFollowing();
             reader.Skip();
         }
     }
 }
Beispiel #8
0
        void ReadRootTagInternal(String fileName, NbtCompression compression)
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => NbtFile.ReadRootTagName(fileName, compression, true, -1));

            Assert.AreEqual("Level", NbtFile.ReadRootTagName(fileName));
            Assert.AreEqual("Level", NbtFile.ReadRootTagName(fileName, compression, true, 0));

            byte[] fileBytes = File.ReadAllBytes(fileName);
            using (var ms = new MemoryStream(fileBytes)) {
                using (var nss = new NonSeekableStream(ms)) {
                    Assert.Throws<ArgumentOutOfRangeException>(
                        () => NbtFile.ReadRootTagName(nss, compression, true, -1));
                    NbtFile.ReadRootTagName(nss, compression, true, 0);
                }
            }
        }
Beispiel #9
0
 public void ReloadNonSeekableStream()
 {
     var loadedFile = new NbtFile(TestFiles.Big);
     using (var ms = new MemoryStream()) {
         using (var nss = new NonSeekableStream(ms)) {
             long bytesWritten = loadedFile.SaveToStream(nss, NbtCompression.None);
             ms.Position = 0;
             Assert.Throws<NotSupportedException>(() => loadedFile.LoadFromStream(nss, NbtCompression.AutoDetect));
             ms.Position = 0;
             Assert.Throws<InvalidDataException>(() => loadedFile.LoadFromStream(nss, NbtCompression.ZLib));
             ms.Position = 0;
             long bytesRead = loadedFile.LoadFromStream(nss, NbtCompression.None);
             Assert.AreEqual(bytesWritten, bytesRead);
             TestFiles.AssertNbtBigFile(loadedFile);
         }
     }
 }