Exemplo n.º 1
0
        /// <summary>
        /// 指定したストリームからAtomを読み取ります
        /// </summary>
        /// <param name="stream">読み取り元のストリーム</param>
        /// <returns>読み取ったAtomのインスタンス</returns>
        /// <exception cref="EndOfStreamException">ストリームの末尾に達しました</exception>
        static public Atom Read(Stream stream)
        {
            var header = stream.ReadBytes(8);
            var name   = new ID4(header, 0);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(header, 4, 4);
            }
            uint len = BitConverter.ToUInt32(header, 4);

            if ((len & 0x80000000U) != 0)
            {
                if ((len & 0x7FFFFFFF) > 1024)
                {
                    throw new InvalidDataException("Atom has too many children");
                }
                var children = new AtomCollection();
                for (var i = 0; i < (len & 0x7FFFFFFF); i++)
                {
                    children.Add(Read(stream));
                }
                return(new Atom(name, children));
            }
            else
            {
                if (len > 1024 * 1024)
                {
                    throw new InvalidDataException("Atom length too long");
                }
                var value = stream.ReadBytes((int)len);
                return(new Atom(name, value));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// ストリームからAtomを読み取ります
        /// </summary>
        /// <returns>読み取ったAtomのインスタンス</returns>
        /// <exception cref="EndOfStreamException">ストリームの末尾に達しました</exception>
        public Atom Read()
        {
            var header = stream.ReadBytes(8);
            var name   = new ID4(header, 0);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(header, 4, 4);
            }
            uint len = BitConverter.ToUInt32(header, 4);

            if ((len & 0x80000000U) != 0)
            {
                var children = new AtomCollection();
                for (var i = 0; i < (len & 0x7FFFFFFF); i++)
                {
                    children.Add(Read());
                }
                return(new Atom(name, children));
            }
            else
            {
                var value = stream.ReadBytes((int)len);
                return(new Atom(name, value));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 指定したストリームからAtomを読み取ります
        /// </summary>
        /// <param name="stream">読み取り元のストリーム</param>
        /// <returns>読み取ったAtomのインスタンス</returns>
        /// <exception cref="EndOfStreamException">ストリームの末尾に達しました</exception>
        static public Atom Read(Stream stream)
        {
            var header = new byte[8];
            int pos    = 0;

            while (pos < 8)
            {
                var r = stream.Read(header, pos, 8 - pos);
                if (r <= 0)
                {
                    throw new EndOfStreamException();
                }
                pos += r;
            }
            var name = new ID4(header, 0);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(header, 4, 4);
            }
            uint len = BitConverter.ToUInt32(header, 4);

            if ((len & 0x80000000U) != 0)
            {
                if ((len & 0x7FFFFFFF) > 1024)
                {
                    throw new InvalidDataException("Atom has too many children");
                }
                var children = new AtomCollection();
                for (var i = 0; i < (len & 0x7FFFFFFF); i++)
                {
                    children.Add(Read(stream));
                }
                return(new Atom(name, children));
            }
            else
            {
                if (len > 1024 * 1024)
                {
                    throw new InvalidDataException("Atom length too long");
                }
                var value = new byte[len];
                pos = 0;
                while (pos < len)
                {
                    var r = stream.Read(value, pos, (int)len - pos);
                    if (r <= 0)
                    {
                        throw new EndOfStreamException();
                    }
                    pos += r;
                }
                return(new Atom(name, value));
            }
        }
Exemplo n.º 4
0
        static public async Task <Atom> ReadAtomAsync(this Stream stream, CancellationToken cancel_token)
        {
            var header = await stream.ReadBytesAsync(8, cancel_token).ConfigureAwait(false);

            var name = new ID4(header, 0);

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(header, 4, 4);
            }
            uint len = BitConverter.ToUInt32(header, 4);

            if ((len & 0x80000000U) != 0)
            {
                if ((len & 0x7FFFFFFF) > 1024)
                {
                    throw new InvalidDataException("Atom has too many children");
                }
                var children = new AtomCollection();
                for (var i = 0; i < (len & 0x7FFFFFFF); i++)
                {
                    children.Add(await stream.ReadAtomAsync(cancel_token).ConfigureAwait(false));
                }
                return(new Atom(name, children));
            }
            else
            {
                if (len > 1024 * 1024)
                {
                    throw new InvalidDataException("Atom length too long");
                }
                var value = await stream.ReadBytesAsync((int)len, cancel_token).ConfigureAwait(false);

                return(new Atom(name, value));
            }
        }