Пример #1
0
        public void Write_throws_when_buffer_offset_and_count_exceed_length()
        {
            var target = new ByteCountingStream();
            var buffer = new byte[100];

            target.Write(buffer, 50, 51);
        }
Пример #2
0
    private void SerializeTestObject <T>(Action <IEnumerable <T>, Stream> serializeFn) where T : ITestObject, new()
    {
        var output = new ByteCountingStream();
        var value  = new T();

        value.Fill();
        var sw = Stopwatch.StartNew();

        // warmup
        //UnityEngine.Debug.Log(string.Format("[{0}] Warming-up {1} Serializer", typeof(T).Name, serializeFn.Method.DeclaringType.Name));
        serializeFn(InfiniteEnumerable(value).Take(ItemsToSerialize / 100), output);

        //UnityEngine.Debug.Log(string.Format("[{0}] Running {1} Serializer", typeof(T).Name, serializeFn.Method.DeclaringType.Name));
        // reset
        GC.Collect();
        output.SetLength(0);
        sw.Start();
        serializeFn(InfiniteEnumerable(value).Take(ItemsToSerialize), output);
        sw.Stop();
        //UnityEngine.Debug.Log(string.Format("[{0}] {1} Serializer finished in {2:F2}ms, {3} bytes are written.", typeof(T).Name, serializeFn.Method.DeclaringType.Name, sw.ElapsedMilliseconds, output.Length));
        UnityEngine.Debug.Log(string.Format("[{0}] {1} | size(bytes) {2} | object/s {3:F0} | bandwidth {4:F2} Mb/s", typeof(T).Name, serializeFn.Method.DeclaringType.Name,
                                            output.Length / ItemsToSerialize,
                                            ItemsToSerialize * (1 / sw.Elapsed.TotalSeconds),
                                            output.Length * (1 / sw.Elapsed.TotalSeconds) / 1024 / 1024));
    }
Пример #3
0
        public void Write_throws_when_buffer_offset_equals_length()
        {
            var target = new ByteCountingStream();
            var buffer = new byte[100];

            target.Write(buffer, 100, 1);
        }
Пример #4
0
        public static long SizeOf(this IFormatter formatter, object graph)
        {
            Ensure.NotNull(formatter, "formatter");
            Stream stream = new ByteCountingStream();

            formatter.Serialize(stream, graph);
            return(stream.Length);
        }
Пример #5
0
        public void Write_accepts_last_byte_of_buffer()
        {
            var target = new ByteCountingStream();
            var buffer = new byte[100];

            target.Write(buffer, 99, 1);
            Assert.AreEqual(1, target.Length);
        }
Пример #6
0
        public void ByteCountingStream_count_tests()
        {
            var data      = Enumerable.Range(1, 1000).ToList();
            var formatter = new BinaryFormatter();
            var memStream = new MemoryStream();
            var target    = new ByteCountingStream(memStream);

            formatter.Serialize(target, data);
            Assert.AreEqual(target.Length, memStream.Length);
            memStream.Position = 0;
            var clone = (List <int>)formatter.Deserialize(memStream);

            CollectionAssert.AreEqual(data, clone);
        }
 public InflateSinkAdapter(Stream stream, bool leaveOpen)
 {
     _byteCounter   = new ByteCountingStream(stream);
     _deflateInput  = new MemoryStream();
     _deflateStream = new DeflateStream(_deflateInput, CompressionMode.Decompress, leaveOpen);
 }
Пример #8
0
        public void Write_throws_when_buffer_is_null()
        {
            var target = new ByteCountingStream();

            target.Write(null, 0, 1);
        }
Пример #9
0
        /// <summary> Saves this NBT file to a stream. Nothing is written to stream if RootTag is <c>null</c>. </summary>
        /// <param name="stream"> Stream to write data to. May not be <c>null</c>. </param>
        /// <param name="compression"> Compression mode to use for saving. May not be AutoDetect. </param>
        /// <returns> Number of bytes written to the stream. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="stream"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentException"> If AutoDetect was given as the <paramref name="compression"/> mode. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="InvalidDataException"> If given stream does not support writing. </exception>
        /// <exception cref="NbtFormatException"> If RootTag is null;
        /// or if RootTag is unnamed;
        /// or if one of the NbtCompound tags contained unnamed tags;
        /// or if an NbtList tag had Unknown list type and no elements. </exception>
        public long SaveToStream([NotNull] Stream stream, NbtCompression compression)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            switch (compression)
            {
            case NbtCompression.AutoDetect:
                throw new ArgumentException("AutoDetect is not a valid NbtCompression value for saving.");

            case NbtCompression.ZLib:
            case NbtCompression.GZip:
            case NbtCompression.None:
                break;

            default:
                throw new ArgumentOutOfRangeException("compression");
            }

            if (rootTag.Name == null)
            {
                // This may trigger if root tag has been renamed
                throw new NbtFormatException(
                          "Cannot save NbtFile: Root tag is not named. Its name may be an empty string, but not null.");
            }

            long startOffset = 0;

            if (stream.CanSeek)
            {
                startOffset = stream.Position;
            }
            else
            {
                stream = new ByteCountingStream(stream);
            }

            switch (compression)
            {
            case NbtCompression.ZLib:
                stream.WriteByte(0x78);
                stream.WriteByte(0x01);
                int checksum;
                using (var compressStream = new ZLibStream(stream, CompressionMode.Compress, true)) {
                    var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                    RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                    bufferedStream.Flush();
                    checksum = compressStream.Checksum;
                }
                byte[] checksumBytes = BitConverter.GetBytes(checksum);
                if (BitConverter.IsLittleEndian)
                {
                    // Adler32 checksum is big-endian
                    Array.Reverse(checksumBytes);
                }
                stream.Write(checksumBytes, 0, checksumBytes.Length);
                break;

            case NbtCompression.GZip:
                using (var compressStream = new GZipStream(stream, CompressionMode.Compress, true)) {
                    // use a buffered stream to avoid GZipping in small increments (which has a lot of overhead)
                    var bufferedStream = new BufferedStream(compressStream, WriteBufferSize);
                    RootTag.WriteTag(new NbtBinaryWriter(bufferedStream, BigEndian));
                    bufferedStream.Flush();
                }
                break;

            case NbtCompression.None:
                var writer = new NbtBinaryWriter(stream, BigEndian);
                RootTag.WriteTag(writer);
                break;

            default:
                throw new ArgumentOutOfRangeException("compression");
            }

            if (stream.CanSeek)
            {
                return(stream.Position - startOffset);
            }
            else
            {
                return(((ByteCountingStream)stream).BytesWritten);
            }
        }
Пример #10
0
        /// <summary> Loads NBT data from a stream. Existing <c>RootTag</c> will be replaced </summary>
        /// <param name="stream"> Stream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking. </param>
        /// <param name="compression"> Compression method to use for loading/saving this file. </param>
        /// <param name="selector"> Optional callback to select which tags to load into memory. Root may not be skipped.
        /// No reference is stored to this callback after loading (don't worry about implicitly captured closures). May be <c>null</c>. </param>
        /// <returns> Number of bytes read from the stream. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="stream"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="NotSupportedException"> If <paramref name="compression"/> is set to AutoDetect, but the stream is not seekable. </exception>
        /// <exception cref="EndOfStreamException"> If file ended earlier than expected. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected, decompressing failed, or given stream does not support reading. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        public long LoadFromStream([NotNull] Stream stream, NbtCompression compression, [CanBeNull] TagSelector selector)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            FileName = null;

            // detect compression, based on the first byte
            if (compression == NbtCompression.AutoDetect)
            {
                FileCompression = DetectCompression(stream);
            }
            else
            {
                FileCompression = compression;
            }

            // prepare to count bytes read
            long startOffset = 0;

            if (stream.CanSeek)
            {
                startOffset = stream.Position;
            }
            else
            {
                stream = new ByteCountingStream(stream);
            }

            switch (FileCompression)
            {
            case NbtCompression.GZip:
                using (var decStream = new GZipStream(stream, CompressionMode.Decompress, true)) {
                    if (bufferSize > 0)
                    {
                        LoadFromStreamInternal(new BufferedStream(decStream, bufferSize), selector);
                    }
                    else
                    {
                        LoadFromStreamInternal(decStream, selector);
                    }
                }
                break;

            case NbtCompression.None:
                LoadFromStreamInternal(stream, selector);
                break;

            case NbtCompression.ZLib:
                if (stream.ReadByte() != 0x78)
                {
                    throw new InvalidDataException(WrongZLibHeaderMessage);
                }
                stream.ReadByte();
                using (var decStream = new DeflateStream(stream, CompressionMode.Decompress, true)) {
                    if (bufferSize > 0)
                    {
                        LoadFromStreamInternal(new BufferedStream(decStream, bufferSize), selector);
                    }
                    else
                    {
                        LoadFromStreamInternal(decStream, selector);
                    }
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("compression");
            }

            // report bytes read
            if (stream.CanSeek)
            {
                return(stream.Position - startOffset);
            }
            else
            {
                return(((ByteCountingStream)stream).BytesRead);
            }
        }
Пример #11
0
        /// <summary> Loads NBT data from a stream. Existing <c>RootTag</c> will be replaced </summary>
        /// <param name="stream"> Stream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking. </param>
        /// <param name="compression"> Compression method to use for loading/saving this file. </param>
        /// <param name="selector"> Optional callback to select which tags to load into memory. Root may not be skipped.
        /// No reference is stored to this callback after loading (don't worry about implicitly captured closures). May be <c>null</c>. </param>
        /// <returns> Number of bytes read from the stream. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="stream"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="NotSupportedException"> If <paramref name="compression"/> is set to AutoDetect, but the stream is not seekable. </exception>
        /// <exception cref="EndOfStreamException"> If file ended earlier than expected. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected, decompressing failed, or given stream does not support reading. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        public long LoadFromStream(Stream stream, NbtCompression compression, TagSelector selector)
        {
            if (stream == null) throw new ArgumentNullException("stream");

            FileName = null;

            // detect compression, based on the first byte
            if (compression == NbtCompression.AutoDetect)
            {
                FileCompression = DetectCompression(stream);
            }
            else
            {
                FileCompression = compression;
            }

            // prepare to count bytes read
            long startOffset = 0;
            if (stream.CanSeek)
            {
                startOffset = stream.Position;
            }
            else
            {
                stream = new ByteCountingStream(stream);
            }

            switch (FileCompression)
            {
                case NbtCompression.GZip:
                    using (var decStream = new GZipStream(stream, CompressionMode.Decompress, true))
                    {
                        if (bufferSize > 0)
                        {
                            LoadFromStreamInternal(new BufferedStream(decStream, bufferSize), selector);
                        }
                        else
                        {
                            LoadFromStreamInternal(decStream, selector);
                        }
                    }
                    break;

                case NbtCompression.None:
                    LoadFromStreamInternal(stream, selector);
                    break;

                case NbtCompression.ZLib:
                    if (stream.ReadByte() != 0x78)
                    {
                        throw new InvalidDataException(WrongZLibHeaderMessage);
                    }
                    stream.ReadByte();
                    using (var decStream = new DeflateStream(stream, CompressionMode.Decompress, true))
                    {
                        if (bufferSize > 0)
                        {
                            LoadFromStreamInternal(new BufferedStream(decStream, bufferSize), selector);
                        }
                        else
                        {
                            LoadFromStreamInternal(decStream, selector);
                        }
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException("compression");
            }

            // report bytes read
            if (stream.CanSeek)
            {
                return stream.Position - startOffset;
            }
            else
            {
                return ((ByteCountingStream)stream).BytesRead;
            }
        }