예제 #1
0
        private static NbtDocument Parse(
            ReadOnlyMemory <byte> data,
            NbtOptions options,
            byte[]?extraRentedBytes,
            out int bytesConsumed)
        {
            ReadOnlySpan <byte> dataSpan = data.Span;
            var database = new MetadataDb(data.Length);
            var stack    = new ByteStack <ContainerFrame>(NbtOptions.DefaultMaxDepth, clearOnReturn: false);

            var readerState = new NbtReaderState(options);
            var reader      = new NbtReader(dataSpan, isFinalBlock: true, readerState);

            try
            {
                Parse(ref reader, ref database, ref stack);
                bytesConsumed = (int)reader.BytesConsumed;
            }
            catch
            {
                database.Dispose();
                throw;
            }
            finally
            {
                readerState.Dispose();
                stack.Dispose();
            }

            return(new NbtDocument(data, options, database, extraRentedBytes, isDisposable: true));
        }
        private static NbtDocument Parse(
            ReadOnlyMemory <byte> data,
            NbtOptions?options,
            byte[]?extraRentedBytes,
            ArrayPool <byte>?pool,
            out int bytesConsumed)
        {
            pool ??= ArrayPool <byte> .Shared;
            NbtOptions nbtOptions = options ?? NbtOptions.JavaDefault;

            ReadOnlySpan <byte> dataSpan = data.Span;
            MetadataDb          database = new(pool, data.Length);

            ByteStack <NbtReader.ContainerFrame> readerStack;

            unsafe
            {
                NbtReader.ContainerFrame *      stackBuffer = stackalloc NbtReader.ContainerFrame[NbtOptions.DefaultMaxDepth];
                Span <NbtReader.ContainerFrame> stackSpan   = new(stackBuffer, NbtOptions.DefaultMaxDepth);
                readerStack = new(stackSpan, clearOnReturn : false);
            }
            NbtReaderState readerState = new(readerStack, nbtOptions);
            NbtReader      reader      = new(dataSpan, isFinalBlock : true, readerState);

            ByteStack <ContainerFrame> docStack;

            unsafe
            {
                ContainerFrame *      stackBuffer = stackalloc ContainerFrame[NbtOptions.DefaultMaxDepth];
                Span <ContainerFrame> stackSpan   = new(stackBuffer, NbtOptions.DefaultMaxDepth);
                docStack = new(stackSpan, clearOnReturn : false);
            }

            try
            {
                Parse(ref reader, ref database, ref docStack);
                bytesConsumed = (int)reader.BytesConsumed;
            }
            catch
            {
                database.Dispose();
                throw;
            }
            finally
            {
                readerState.Dispose();
                readerStack.Dispose();
                docStack.Dispose();
            }

            return(new NbtDocument(data, nbtOptions, database, extraRentedBytes, pool));
        }
예제 #3
0
        public NbtReaderState(ByteStack <ContainerFrame> stack, NbtOptions?options = null) : this()
        {
            Options = options ?? NbtOptions.JavaDefault;

            _containerInfoStack = stack;
        }
        public NbtReaderState(NbtOptions?options = null) : this()
        {
            Options = options ?? NbtOptions.JavaDefault;

            _containerInfoStack = new ByteStack <ContainerFrame>(NbtOptions.DefaultMaxDepth, clearOnReturn: false);
        }