Пример #1
0
        private void LoadFromStreamInternal([NotNull] Stream stream, [CanBeNull] TagSelector tagSelector)
        {
            var reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector  = tagSelector,
                UseVarInt = UseVarInt
            };

            RootTag = NbtTag.ReadUnknownTag(reader);
            //else {
            //    // Make sure the first byte in this file is the tag for a TAG_Compound
            //    int firstByte = stream.ReadByte();
            //    if (firstByte < 0)
            //    {
            //        throw new EndOfStreamException();
            //    }
            //    if (firstByte != (int)NbtTagType.Compound)
            //    {
            //        throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            //    }

            //    var rootCompound = new NbtCompound(reader.ReadString());
            //    rootCompound.ReadTag(reader);
            //    RootTag = rootCompound;
            //}
        }
Пример #2
0
        void LoadFromStreamInternal([NotNull] Stream stream, [CanBeNull] TagSelector tagSelector)
        {
            // Make sure the first byte in this file is the tag for a TAG_Compound
            if (FileVersion != NbtVersion.Legacy)
            {
                stream.Read(new byte[8], 0, 8);
            }
            int firstByte = stream.ReadByte();

            if (firstByte < 0)
            {
                throw new EndOfStreamException();
            }
            if (firstByte != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector  = tagSelector,
                UseVarInt = UseVarInt
            };

            var rootCompound = new NbtCompound(reader.ReadString());

            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Пример #3
0
        /// <summary> Loads NBT data from a byte array. Existing <c>RootTag</c> will be replaced. <c>FileName</c> will be set to null. </summary>
        /// <param name="buffer"> Stream from which data will be loaded. If <paramref name="compression"/> is set to AutoDetect, this stream must support seeking. </param>
        /// <param name="index"> The index into <paramref name="buffer"/> at which the stream begins. Must not be negative. </param>
        /// <param name="length"> Maximum number of bytes to read from the given buffer. Must not be negative.
        /// An <see cref="EndOfStreamException"/> is thrown if NBT stream is longer than the given length. </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 buffer. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="buffer"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>;
        /// if <paramref name="index"/> or <paramref name="length"/> is less than zero;
        /// if the sum of <paramref name="index"/> and <paramref name="length"/> is greater than the length of <paramref name="buffer"/>. </exception>
        /// <exception cref="EndOfStreamException"> If NBT stream extends beyond the given <paramref name="length"/>. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected or decompressing failed. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        public long LoadFromBuffer([NotNull] byte[] buffer, int index, int length, NbtCompression compression,
                                   [CanBeNull] TagSelector selector)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            using (var ms = new MemoryStream(buffer, index, length)) {
                LoadFromStream(ms, compression, selector);
                FileName = null;
                return(ms.Position);
            }
        }
Пример #4
0
        /// <summary> Loads NBT data from a file. Existing <c>RootTag</c> will be replaced. </summary>
        /// <param name="fileName"> Name of the file from which data will be loaded. </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 file. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="fileName"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="FileNotFoundException"> If given file was not found. </exception>
        /// <exception cref="EndOfStreamException"> If file ended earlier than expected. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected, or decompressing failed. </exception>
        /// <exception cref="NbtFormatException"> If an error occured while parsing data in NBT format. </exception>
        /// <exception cref="IOException"> If an I/O error occurred while reading the file. </exception>
        public int LoadFromFile([NotNull] string fileName, NbtCompression compression,
                                [CanBeNull] TagSelector selector)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            using (FileStream readFileStream = File.OpenRead(fileName)) {
                LoadFromStream(readFileStream, compression, selector);
                FileName = fileName;
                return((int)readFileStream.Position);
            }
        }
        /// <summary>
        /// タグ選択ダイアログ表示
        /// </summary>
        /// <param name="relativeControl"></param>
        public void ShowTagSelector(FrameworkElement relativeControl)
        {
            switch (this.Client.SelectedPage.Value)
            {
            case PageType.Catalog:
                if (this.SelectedItems.Count <= 0 && this.SelectedRecord.Value == null)
                {
                    return;
                }
                break;

            case PageType.Viewer:
                if (this.SelectedRecord.Value == null)
                {
                    return;
                }
                break;

            default:
                return;
            }

            var left
                = (relativeControl != null) ? 10.0
                : this.IsPaneOpen.Value ? (openPaneWidth + 10.0)
                : 10.0;

            var content = new TagSelector();

            if (this.Client.SelectedPage.Value == PageType.Catalog && this.SelectedItems.Count > 1)
            {
                content.TagSelectedCallBack += x => this.SelectedItems.AddTag(x);
            }
            else if (this.SelectedRecord.Value != null)
            {
                content.Target = this.SelectedRecord.Value;
            }
            else
            {
                return;
            }

            this.PopupOwner.PopupDialog.Show(content,
                                             new Thickness(left, double.NaN, double.NaN, double.NaN),
                                             relativeControl == null ? HorizontalAlignment.Left : HorizontalAlignment.Right,
                                             VerticalAlignment.Center, relativeControl);
        }
Пример #6
0
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="tagid"></param>
        /// <returns></returns>
        static List <TagSelector> GetTags(string accessToken, List <int> tagid)
        {
            if (tagid == null || tagid.Count == 0)
            {
                return(null);
            }

            Tags tags = new Tags();

            Tags.Result result = tags.FindTagsCollection(accessToken);
            if (result != null && result.errcode == 0)
            {
                var items = result.taglist.Where(e => tagid.Any(a => a == e.tagid)).ToList();
                return(TagSelector.ConvertTo(items).ToList());
            }
            return(null);
        }
Пример #7
0
        public CityWeatherService(
            ICityWeatherRepository repository,
            IMapper mapper,
            ILogger <CityWeatherService> logger,
            ISettingService settingService)
        {
            _repository = repository;
            _mapper     = mapper;
            _logger     = logger;

            _web = new HtmlWeb();

            _parentNodeSelector       = settingService.GetTagSelectorForCityWeatherParentNode();
            _childTimeSelector        = settingService.GetTagSelectorsForCityWeatherTime(_parentNodeSelector);
            _childTemperatureSelector = settingService.GetTagSelectorsForCityWeatherDegree(_parentNodeSelector);
            _tomorrowUrlPostfix       = settingService.GetTomorrowUrlPostfix();
            _minusControlChar         = settingService.GetMinusControlChar();
        }
        public void GetSingleNodeTest()
        {
            var selector = new TagSelector
            {
                Tag        = HtmlElementTag.Div,
                Properties = new List <TagProperty>()
                {
                    new TagProperty
                    {
                        Name  = "class",
                        Value = "div4"
                    }
                }
            };
            var result = HtmlParser.GetSingleNode(_htmlDocument.DocumentNode, selector);

            Assert.NotNull(result);
            Assert.Equal("<a href=\"http://url\"></a>", result.InnerHtml.Trim());
        }
 public IEnumerable <TagSelector> GetTagSelectorsForCityWeatherDegree(TagSelector parentNodeSelector)
 {
     return(new List <TagSelector>
     {
         parentNodeSelector,
         new TagSelector
         {
             Tag = HtmlElementTag.Span,
             Properties = new List <TagProperty>
             {
                 new TagProperty
                 {
                     Name = "class",
                     Value = "unit unit_temperature_c"
                 }
             }
         }
     });
 }
Пример #10
0
        /// <summary> Loads NBT data from a file. Existing <c>RootTag</c> will be replaced. </summary>
        /// <param name="fileName"> Name of the file from which data will be loaded. </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 file. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="fileName"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="FileNotFoundException"> If given file was not found. </exception>
        /// <exception cref="EndOfStreamException"> If file ended earlier than expected. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected, or decompressing failed. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        /// <exception cref="IOException"> If an I/O error occurred while reading the file. </exception>
        public long LoadFromFile(string fileName, NbtCompression compression, TagSelector selector)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            using (
                var readFileStream = new FileStream(fileName,
                                                    FileMode.Open,
                                                    FileAccess.Read,
                                                    FileShare.Read,
                                                    FileStreamBufferSize,
                                                    FileOptions.SequentialScan)) {
                LoadFromStream(readFileStream, compression, selector);
                FileName = fileName;
                return(readFileStream.Position);
            }
        }
Пример #11
0
        void LoadFromStreamInternal([NotNull] Stream stream, [CanBeNull] TagSelector tagSelector)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            // Make sure the first byte in this file is the tag for a TAG_Compound
            if (stream.ReadByte() != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            NbtBinaryReader reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());

            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Пример #12
0
        void LoadFromStreamInternal(Stream stream, TagSelector tagSelector)
        {
            // Make sure the first byte in this file is the tag for a TAG_Compound
            int firstByte = stream.ReadByte();

            if (firstByte < 0)
            {
                throw new EndOfStreamException();
            }
            if (firstByte != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());

            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
 public IEnumerable <TagSelector> GetTagSelectorsForCityWeatherTime(TagSelector parentNodeSelector)
 {
     return(new List <TagSelector>
     {
         parentNodeSelector,
         new TagSelector
         {
             Tag = HtmlElementTag.Div,
             Properties = new List <TagProperty>
             {
                 new TagProperty
                 {
                     Name = "class",
                     Value = "w_time"
                 }
             }
         },
         new TagSelector
         {
             Tag = HtmlElementTag.Span
         }
     });
 }
Пример #14
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 occured while parsing data in NBT format. </exception>
        public int 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;
            }

            long startPosition = stream.Position;

            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("Incorrect ZLib header. Expected 0x78 0x9C");
                }
                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");
            }

            return((int)(stream.Position - startPosition));
        }
Пример #15
0
        public static HtmlNode GetSingleNode(HtmlNode htmlNode, TagSelector selector)
        {
            var expression = XpathExpressionBuilder.GetExpressionByTag(selector);

            return(htmlNode.SelectSingleNode(expression));
        }
Пример #16
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;
            }
        }
Пример #17
0
        void LoadFromStreamInternal(Stream stream, TagSelector tagSelector)
        {
            // Make sure the first byte in this file is the tag for a TAG_Compound
            int firstByte = stream.ReadByte();
            if (firstByte < 0)
            {
                throw new EndOfStreamException();
            }
            if (firstByte != (int)NbtTagType.Compound)
            {
                throw new NbtFormatException("Given NBT stream does not start with a TAG_Compound");
            }
            var reader = new NbtBinaryReader(stream, BigEndian)
            {
                Selector = tagSelector
            };

            var rootCompound = new NbtCompound(reader.ReadString());
            rootCompound.ReadTag(reader);
            RootTag = rootCompound;
        }
Пример #18
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 or file version 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"/> or <paramref name="fileVersion"/> 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)
 {
     return(LoadFromStream(stream, compression, NbtVersion.AutoDetect, selector));
 }
Пример #19
0
        /// <summary> Loads NBT data from a file. Existing <c>RootTag</c> will be replaced. </summary>
        /// <param name="fileName"> Name of the file from which data will be loaded. </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 file. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="fileName"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>. </exception>
        /// <exception cref="FileNotFoundException"> If given file was not found. </exception>
        /// <exception cref="EndOfStreamException"> If file ended earlier than expected. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected, or decompressing failed. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        /// <exception cref="IOException"> If an I/O error occurred while reading the file. </exception>
        public long LoadFromFile(string fileName, NbtCompression compression, TagSelector selector)
        {
            if (fileName == null) throw new ArgumentNullException("fileName");

            using (
                var readFileStream = new FileStream(fileName,
                                                    FileMode.Open,
                                                    FileAccess.Read,
                                                    FileShare.Read,
                                                    FileStreamBufferSize,
                                                    FileOptions.SequentialScan))
            {
                LoadFromStream(readFileStream, compression, selector);
                FileName = fileName;
                return readFileStream.Position;
            }
        }
 public static string GetExpressionByTag(TagSelector elementTag)
 {
     return($"//{elementTag.Tag.ToString().ToLower()}{GetExpression(elementTag.Properties)}");
 }
Пример #21
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);
            }
        }
Пример #22
0
        /// <summary> Loads NBT data from a byte array. Existing <c>RootTag</c> will be replaced. <c>FileName</c> will be set to null. </summary>
        /// <param name="buffer"> Stream from which data will be loaded. If <paramref name="compression"/> is set to AutoDetect, this stream must support seeking. </param>
        /// <param name="index"> The index into <paramref name="buffer"/> at which the stream begins. Must not be negative. </param>
        /// <param name="length"> Maximum number of bytes to read from the given buffer. Must not be negative.
        /// An <see cref="EndOfStreamException"/> is thrown if NBT stream is longer than the given length. </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 buffer. </returns>
        /// <exception cref="ArgumentNullException"> <paramref name="buffer"/> is <c>null</c>. </exception>
        /// <exception cref="ArgumentOutOfRangeException"> If an unrecognized/unsupported value was given for <paramref name="compression"/>;
        /// if <paramref name="index"/> or <paramref name="length"/> is less than zero;
        /// if the sum of <paramref name="index"/> and <paramref name="length"/> is greater than the length of <paramref name="buffer"/>. </exception>
        /// <exception cref="EndOfStreamException"> If NBT stream extends beyond the given <paramref name="length"/>. </exception>
        /// <exception cref="InvalidDataException"> If file compression could not be detected or decompressing failed. </exception>
        /// <exception cref="NbtFormatException"> If an error occurred while parsing data in NBT format. </exception>
        public long LoadFromBuffer(byte[] buffer, int index, int length, NbtCompression compression,
                                   TagSelector selector)
        {
            if (buffer == null) throw new ArgumentNullException("buffer");

            using (var ms = new MemoryStream(buffer, index, length))
            {
                LoadFromStream(ms, compression, selector);
                FileName = null;
                return ms.Position;
            }
        }