using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace System.IO.BufferStream
{
    /// <summary>
    /// Buffer读写类
    ///
    /// @Author: Red_angelX
    /// </summary>   
    [Serializable, ComVisible(true)]
    public class ByteBuffer : IDisposable
    {
        private byte[] _buffer;
        private Encoder _encoder;
        private Encoding _encoding;
        /// <summary>
        /// 无后被存储区的ByteBuffer
        /// </summary>
        public static readonly ByteBuffer Null = new ByteBuffer();
        /// <summary>
        /// 获取同后备存储区连接的基础流
        /// </summary>
        protected Stream BaseStream;

        /// <summary>
        /// 构造函数
        /// </summary>
        public ByteBuffer()
        {
            this.BaseStream = new MemoryStream();
            this._buffer = new byte[0x10];
            this._encoding = Encoding.Default;//(false, true);
            this._encoder = this._encoding.GetEncoder();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="encoding">编码</param>
        public ByteBuffer(Encoding encoding)
        {
            this.BaseStream = new MemoryStream();
            this._buffer = new byte[0x10];
            this._encoding = encoding;
            this._encoder = this._encoding.GetEncoder();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buffer">初始数组</param>
        public ByteBuffer(byte[] buffer)
        {
            this.BaseStream = new MemoryStream(buffer);
            this.BaseStream.Position = 0;
            this._buffer = new byte[0x10];
            this._encoding = Encoding.Default;
            this._encoder = this._encoding.GetEncoder();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buffer">初始数组</param>
        /// <param name="encoding">编码</param>
        public ByteBuffer(byte[] buffer, Encoding encoding)
        {
            this.BaseStream = new MemoryStream(buffer);
            this.BaseStream.Position = 0;
            this._buffer = new byte[0x10];
            this._encoding = encoding;
            this._encoder = this._encoding.GetEncoder();
        }

        #region "基础属性方法"
        /// <summary>
        /// 关闭当前流并释放与之关联的相关资源
        /// </summary>
        public virtual void Close()
        {
            this.Dispose(true);           
        }

        /// <summary>
        /// 释放由ByteBuffer使用得所有资源
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                BaseStream.Close();
            }
        }

        /// <summary>
        /// 将清除该流所有缓冲区,并使得所有缓冲数据被写入到基础设备.
        /// </summary>
        public virtual void Flush()
        {
            this.BaseStream.Flush();
        }

        /// <summary>
        /// 设置当前流中的位置
        /// </summary>
        /// <param name="offset">相对于origin参数字节偏移量</param>
        /// <param name="origin">System.IO.SeekOrigin类型值,指示用于获取新位置的参考点</param>
        /// <returns></returns>
        public virtual long Seek(int offset, SeekOrigin origin)
        {
            return this.BaseStream.Seek((long)offset, origin);
        }

        /// <summary>
        /// 设置当前流长度
        /// </summary>
        /// <param name="value"></param>
        public virtual void SetLength(long value)
        {
            this.BaseStream.SetLength(value);
        }

        /// <summary>
        /// 检测是否还有可用字节
        /// </summary>
        /// <returns></returns>
        public bool Peek()
        {
            return BaseStream.Position >= BaseStream.Length ? false : true;
        }

        void IDisposable.Dispose()
        {
            this.Dispose(true);
        }

        /// <summary>
        /// 将整个流内容写入字节数组,而与 Position 属性无关。
        /// </summary>
        /// <returns></returns>
        public byte[] ToArray()
        {
            long org = BaseStream.Position;
            BaseStream.Position = 0;
            byte[] ret = new byte[BaseStream.Length];           
            BaseStream.Read(ret, 0, ret.Length);
            BaseStream.Position = org;
            return ret;
        }
        #endregion

        #region "写流方法"
        /// <summary>
        /// 压入一个布尔值,并将流中当前位置提升1
        /// </summary>
        /// <param name="value"></param>
        public void writeBoolean(bool value)
        {
            this._buffer[0] = value ? (byte)1 : (byte)0;
            this.BaseStream.Write(_buffer, 0, 1);
        }

        /// <summary>
        /// 压入一个Byte,并将流中当前位置提升1
        /// </summary>
        /// <param name="value"></param>
        public void writeByte(Byte value)
        {
            this.BaseStream.WriteByte(value);
        }

        /// <summary>
        /// 压入Byte数组,并将流中当前位置提升数组长度
        /// </summary>
        /// <param name="value">字节数组</param>
        public void writeBytes(Byte[] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            this.BaseStream.Write(value, 0, value.Length);
        }

        /// <summary>
        /// 压入Byte数组,并将流中当前位置提升数组长度
        /// </summary>
        /// <param name="value">字节数组</param>
        /// <param name="index">value中从零开始的字节偏移量</param>
        /// <param name="count">要写入当前流的字节数</param>
        public void writeBytes(Byte[] value, int index, int count)
        {
            this.BaseStream.Write(value, index, count);
        }

        /// <summary>
        /// 压入一个Char,并将流中当前位置提升1
        /// </summary>
        /// <param name="ch"></param>
        public unsafe void writeChar(char ch)
        {
            if (char.IsSurrogate(ch))
            {
                throw new ArgumentException("Arg_SurrogatesNotAllowedAsSingleChar");
            }
            int count = 0;
            fixed (byte* numRef = this._buffer)
            {
                count = this._encoder.GetBytes(&ch, 1, numRef, 0x10, true);
            }
            this.BaseStream.Write(this._buffer, 0, count);
        }

        /// <summary>
        /// 压入Char数组,并将流中当前位置提升数组长度
        /// </summary>
        /// <param name="chars"></param>
        public void writeChars(char[] chars)
        {
            if (chars == null)
            {
                throw new ArgumentNullException("chars");
            }
            byte[] buffer = this._encoding.GetBytes(chars, 0, chars.Length);
            this.BaseStream.Write(buffer, 0, buffer.Length);
        }

        /// <summary>
        /// 压入Char数组,并将流中当前位置提升数组长度
        /// </summary>
        /// <param name="chars"></param>
        /// <param name="index">chars中从零开始的字节偏移量</param>
        /// <param name="count">要写入当前流的字节数</param>
        public void writeChars(char[] chars, int index, int count)
        {
            byte[] buffer = _encoding.GetBytes(chars, index, count);
            this.BaseStream.Write(buffer, 0, buffer.Length);
        }

        /// <summary>
        /// 压入一个Short,并将流中当前位置提升2
        /// </summary>
        /// <param name="value"></param>
        public void writeShort(short value)
        {
            this._buffer[0] = (byte)(value >> 8);
            this._buffer[1] = (byte)value;
            this.BaseStream.Write(this._buffer, 0, 2);
        }

        /// <summary>
        /// 压入一个int,并将流中当前位置提升4
        /// </summary>
        /// <param name="value"></param>
        public void writeInteger(int value)
        {
            this._buffer[0] = (byte)(value >> 0x18);
            this._buffer[1] = (byte)(value >> 0x10);
            this._buffer[2] = (byte)(value >> 8);
            this._buffer[3] = (byte)value;
            this.BaseStream.Write(this._buffer, 0, 4);
        }

        /// <summary>
        /// 压入一个Long,并将流中当前位置提升8
        /// </summary>
        /// <param name="value"></param>
        public void writeLong(int value)
        {
            this._buffer[0] = (byte)(value >> 0x38);
            this._buffer[1] = (byte)(value >> 0x30);
            this._buffer[2] = (byte)(value >> 0x28);
            this._buffer[3] = (byte)(value >> 0x20);
            this._buffer[4] = (byte)(value >> 0x18);
            this._buffer[5] = (byte)(value >> 0x10);
            this._buffer[6] = (byte)(value >> 8);
            this._buffer[7] = (byte)value;           
            this.BaseStream.Write(this._buffer, 0, 8);
        }

        /// <summary>
        /// 压入一个Double,并将流中当前位置提升8
        /// </summary>
        /// <param name="value"></param>
        public unsafe void writeDouble(double value)
        {
            ulong num = *((ulong*)&value);
            this._buffer[0] = (byte)(num >> 0x38);
            this._buffer[1] = (byte)(num >> 0x30);
            this._buffer[2] = (byte)(num >> 0x28);
            this._buffer[3] = (byte)(num >> 0x20);
            this._buffer[4] = (byte)(num >> 0x18);
            this._buffer[5] = (byte)(num >> 0x10);
            this._buffer[6] = (byte)(num >> 8);
            this._buffer[7] = (byte)num;           
            this.BaseStream.Write(this._buffer, 0, 8);
        }

        /// <summary>
        /// 压入String,并将流中当前位置提升String的长度
        /// </summary>
        /// <param name="value"></param>
        public void writeString(string value)
        {
            byte[] bytes = _encoding.GetBytes(value);
            this.BaseStream.Write(bytes, 0, bytes.Length);
        }
        #endregion

        #region "读流方法"
        /// <summary>
        /// 读取布尔值,并将流中当前位置提升1
        /// </summary>
        /// <returns></returns>
        public bool readBoolean()
        {
            return readByte() == 0 ? false : true;
        }

        /// <summary>
        /// 读取Byte值,并将流中当前位置提升1
        /// </summary>
        /// <returns></returns>
        public byte readByte()
        {
            return (byte)BaseStream.ReadByte();
        }

        /// <summary>
        /// 读取count长度的Byte数组,并将流中当前位置提升count
        /// </summary>
        /// <param name="count">要从当前流中最多读取的字节数</param>
        /// <returns></returns>
        public byte[] readBytes(int count)
        {
            if (count < 0)
                throw new ArgumentOutOfRangeException("count");
            byte[] buffer = new byte[count];
            int num = BaseStream.Read(buffer, 0, count);
            return buffer;
        }

        /// <summary>
        /// 读取一个Char值,并将流中当前位置提升1
        /// </summary>
        /// <returns></returns>
        public char readChar()
        {
            return (char)BaseStream.ReadByte();
        }

        /// <summary>
        /// 读取count长度的Char数组,并将流中当前位置提升count
        /// </summary>
        /// <param name="count">要从当前流中最多读取的字节数</param>
        /// <returns></returns>
        public char[] readChars(int count)
        {
            byte[] buffer = readBytes(count);
            return _encoding.GetChars(buffer);
        }

        /// <summary>
        /// 读取一个short值,并将流中当前位置提升2
        /// </summary>
        /// <returns></returns>
        public short readShort()
        {
            short ret = (short)(readByte() << 8 | readByte());
            return ret;
        }

        /// <summary>
        /// 读取一个Int值,并将流中当前位置提升4
        /// </summary>
        /// <returns></returns>
        public int readInteger()
        {
            int ret = (int)(readByte() << 0x18 | readByte() << 0x10 | readByte() << 8 | readByte());
            return ret;
        }

        /// <summary>
        /// 读取一个Long值,并将流中当前位置提升8
        /// </summary>
        /// <returns></returns>
        public long readLong()
        {
            uint num1 = (uint)readInteger();
            uint num2 = (uint)readInteger();
            return (long)((num1 << 0x20) | num2);
        }

        /// <summary>
        /// 读取一个Double值,并将流中当前位置提升8
        /// </summary>
        /// <returns></returns>
        public unsafe double readDouble()
        {
            uint num1 = (uint)readInteger();
            uint num2 = (uint)readInteger();
            ulong num3 = (num1 << 0x20) | num2;
            return *(((double*)&num3));
        }

        /// <summary>
        /// 读取length(Bytes)长度的String,并将流中当前位置提升length
        /// </summary>
        /// <param name="length">要从当前流中最多读取的字节数</param>
        /// <returns></returns>
        public string readString(int length)
        {
            byte[] buffer = readBytes(length);
            return _encoding.GetString(buffer);
        }
        #endregion

        #region 属性
        /// <summary>
        /// 获取用字节表示的流长度
        /// </summary>
        public long Length
        {
            get
            {
                return this.BaseStream.Length;
            }
        }

        /// <summary>
        /// 获取或设置当前流中的位置
        /// </summary>
        public long Position
        {
            get
            {
                return this.BaseStream.Position;
            }
            set
            {
                this.BaseStream.Position = value;
            }
        }

        /// <summary>
        /// 获取可用字节个数
        /// </summary>
        public long Remaining
        {
            get
            {
                return BaseStream.Length - BaseStream.Position;
            }
        }
        #endregion
    }
}


posted on 2007-06-20 11:59  Red_angelX  阅读(2176)  评论(1编辑  收藏  举报