Example #1
0
        /// <summary>
        /// Provide the data for this chunk. (Not all chunks have data.)
        /// </summary>
        /// <param name="data">
        /// The data for this chunk.
        /// </param>
        public void SetData(byte[] data)
        {
            this.m_data = data;
            int length = this.m_data == null ? 0 : this.m_data.Length;

            this.m_length = NetworkOrderBitConverter.GetBytes((uint)length);
            uint crc = Crc32.UpdateCrc(0xffffffff, this.m_chunkType);

            if (this.m_data != null)
            {
                crc = Crc32.UpdateCrc(crc, this.m_data);
            }

            crc            ^= 0xffffffff;
            this.m_crcBytes = NetworkOrderBitConverter.GetBytes(crc);
        }
Example #2
0
        /// <summary>
        /// Read bytes from this chunk.
        /// </summary>
        /// <param name="buffer">
        /// Buffer into which to write bytes.
        /// </param>
        /// <param name="offset">
        /// Offset in buffer at which to start
        ///     writing bytes.
        /// </param>
        /// <param name="count">
        /// Maximum number of bytes to fetch.
        /// </param>
        /// <returns>
        /// The number of bytes placed into the buffer. If the
        ///     buffer is larger than the amount of remaining data, this will
        ///     be less than count. Returns 0 to indicate that no data is left.
        /// </returns>
        public int Read(byte[] buffer, int offset, int count)
        {
            int dataLength = this.m_data == null ? 0 : this.m_data.Length;

            if (this.m_data == null)
            {
                this.m_length   = new byte[4];
                this.m_crcBytes = NetworkOrderBitConverter.GetBytes(Crc32.Crc(this.m_chunkType));
            }

            int written = 0;

            while (written < count)
            {
                int amount = 1;
                if (this.position < 4)
                {
                    amount = CopyUtilities.WriteAsMuchDataAsPossible(
                        buffer, offset + written, offset + count, this.m_length, this.position);
                }
                else if (this.position < 8)
                {
                    amount = CopyUtilities.WriteAsMuchDataAsPossible(
                        buffer, offset + written, offset + count, this.m_chunkType, this.position - 4);
                }
                else if (this.position < (8 + dataLength))
                {
                    amount = CopyUtilities.WriteAsMuchDataAsPossible(
                        buffer, offset + written, offset + count, this.m_data, this.position - 8);
                }
                else if (this.position < (12 + dataLength))
                {
                    amount = CopyUtilities.WriteAsMuchDataAsPossible(
                        buffer, offset + written, offset + count, this.m_crcBytes, this.position - (8 + dataLength));
                }
                else
                {
                    return(written);
                }

                this.position += amount;
                written       += amount;
            }

            return(count);
        }