点击或拖拽改变大小

SoftBuffer 类

一个线程安全的缓存数据块,支持批量动态修改,添加,并获取快照
A thread-safe cache data block that supports batch dynamic modification, addition, and snapshot acquisition
继承层次
SystemObject
  HslCommunication.BasicFrameworkSoftBuffer

命名空间:  HslCommunication.BasicFramework
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.0.0.0 (12.0.0.0)
语法
public class SoftBuffer : IDisposable

SoftBuffer 类型公开以下成员。

构造函数
  名称说明
公共方法SoftBuffer
使用默认的大小初始化缓存空间
Initialize cache space with default size
公共方法SoftBuffer(Int32)
使用指定的容量初始化缓存数据块
Initialize the cache data block with the specified capacity
Top
属性
  名称说明
公共属性代码示例ByteTransform
当前的数据变换机制,当你需要从字节数据转换类型数据的时候需要。
The current data transformation mechanism is required when you need to convert type data from byte data.
公共属性IsBoolReverseByWord
获取或设置当前的bool操作是否按照字节反转
Gets or sets whether the current bool operation is reversed by bytes
Top
方法
  名称说明
公共方法Dispose
释放被 SoftBuffer 使用的所有资源
受保护的方法Dispose(Boolean)
释放当前的对象
公共方法Equals (继承自 Object。)
受保护的方法Finalize (继承自 Object。)
公共方法GetBool(Int32)
获取指定的位置的bool值,如果超出,则引发异常
Get the bool value at the specified position, if it exceeds, an exception is thrown
公共方法GetBool(Int32, Int32)
获取指定位置的bool数组值,如果超过,则引发异常
Get the bool array value at the specified position, if it exceeds, an exception is thrown
公共方法GetByte
获取byte类型的数据
Get byte data
公共方法GetBytes
获取内存所有的数据信息
Get all data information in memory
公共方法GetBytes(Int32, Int32)
获取内存指定长度的数据信息
Get data information of specified length in memory
公共方法GetCustomerT
读取自定义类型的数据,需要规定解析规则
Read custom types of data, need to specify the parsing rules
公共方法GetDouble(Int32)
获取double类型的数据到缓存区
Get double type data to cache
公共方法GetDouble(Int32, Int32)
获取double类型的数组到缓存区
Get double type array to cache
公共方法GetHashCode (继承自 Object。)
公共方法GetInt16(Int32)
获取short类型的数据到缓存区
Get short data to the cache
公共方法GetInt16(Int32, Int32)
获取short类型的数组到缓存区
Get short type array to cache
公共方法GetInt32(Int32)
获取int类型的数据到缓存区
Get int type data to cache
公共方法GetInt32(Int32, Int32)
获取int类型的数组到缓存区
Get int type array to cache
公共方法GetInt64(Int32)
获取long类型的数据到缓存区
Get long type data to cache
公共方法GetInt64(Int32, Int32)
获取long类型的数组到缓存区
Get long type array to cache
公共方法GetSingle(Int32)
获取float类型的数据到缓存区
Get float type data to cache
公共方法GetSingle(Int32, Int32)
获取float类型的数组到缓存区
Get float type array to cache
公共方法GetType (继承自 Object。)
公共方法GetUInt16(Int32)
获取ushort类型的数据到缓存区
Get ushort type data to cache
公共方法GetUInt16(Int32, Int32)
获取ushort类型的数组到缓存区
Get ushort type array to cache
公共方法GetUInt32(Int32)
获取uint类型的数据到缓存区
Get uint type data to cache
公共方法GetUInt32(Int32, Int32)
获取uint类型的数组到缓存区
Get uint type array to cache
公共方法GetUInt64(Int32)
获取ulong类型的数据到缓存区
Get ulong type data to cache
公共方法GetUInt64(Int32, Int32)
获取ulong类型的数组到缓存区
Get ulong type array to cache
受保护的方法MemberwiseClone (继承自 Object。)
公共方法SetBool(Boolean, Int32)
设置指定的位置bool值,如果超出,则丢弃数据,该位置是指按照位为单位排序的
Set the bool value at the specified position, if it is exceeded, the data is discarded, the position refers to sorting in units of bits
公共方法SetBool(Boolean, Int32)
设置指定的位置的bool数组,如果超出,则丢弃数据,该位置是指按照位为单位排序的
Set the bool array at the specified position, if it is exceeded, the data is discarded, the position refers to sorting in units of bits
公共方法SetBytes(Byte, Int32)
设置指定的位置的数据块,如果超出,则丢弃数据
Set the data block at the specified position, if it is exceeded, the data is discarded
公共方法SetBytes(Byte, Int32, Int32)
设置指定的位置的数据块,如果超出,则丢弃数据 Set the data block at the specified position, if it is exceeded, the data is discarded
公共方法SetBytes(Byte, Int32, Int32, Int32)
设置指定的位置的数据块,如果超出,则丢弃数据
Set the data block at the specified position, if it is exceeded, the data is discarded
公共方法SetCustomerT
写入自定义类型的数据到缓存中去,需要规定生成字节的方法
Write custom type data to the cache, need to specify the method of generating bytes
公共方法SetValue(Byte, Int32)
设置byte类型的数据到缓存区
Set byte type data to the cache area
公共方法SetValue(Double, Int32)
设置double类型的数据到缓存区
Set double type data to the cache area
公共方法SetValue(Double, Int32)
设置double数组的数据到缓存区
Set double array data to the cache area
公共方法SetValue(Int16, Int32)
设置short类型的数据到缓存区
Set short type data to the cache area
公共方法SetValue(Int16, Int32)
设置short数组的数据到缓存区
Set short array data to the cache area
公共方法SetValue(Int32, Int32)
设置int类型的数据到缓存区
Set int type data to the cache area
公共方法SetValue(Int32, Int32)
设置int数组的数据到缓存区
Set int array data to the cache area
公共方法SetValue(Int64, Int32)
设置long类型的数据到缓存区
Set long type data to the cache area
公共方法SetValue(Int64, Int32)
设置long数组的数据到缓存区
Set long array data to the cache area
公共方法SetValue(Single, Int32)
设置float类型的数据到缓存区
Set float type data to the cache area
公共方法SetValue(Single, Int32)
设置float数组的数据到缓存区
Set float array data to the cache area
公共方法SetValue(UInt16, Int32)
设置ushort类型的数据到缓存区
Set ushort type data to the cache area
公共方法SetValue(UInt16, Int32)
设置ushort数组的数据到缓存区
Set ushort array data to the cache area
公共方法SetValue(UInt32, Int32)
设置uint类型的数据到缓存区
Set uint byte data to the cache area
公共方法SetValue(UInt32, Int32)
设置uint数组的数据到缓存区
Set uint array data to the cache area
公共方法SetValue(UInt64, Int32)
设置ulong类型的数据到缓存区
Set ulong byte data to the cache area
公共方法SetValue(UInt64, Int32)
设置ulong数组的数据到缓存区
Set long array data to the cache area
公共方法ToString (重写 ObjectToString.)
Top
扩展方法
  名称说明
公共扩展器方法ToJsonString
获取当前对象的JSON格式表示的字符串。
Gets the string represented by the JSON format of the current object.
(由 HslExtension 定义。)
Top
备注
这个类可以实现什么功能呢,就是你有一个大的数组,作为你的应用程序的中间数据池,允许你往byte[]数组里存放指定长度的子byte[]数组,也允许从里面拿数据, 这些操作都是线程安全的,当然,本类扩展了一些额外的方法支持,也可以直接赋值或获取基本的数据类型对象。
示例
此处举例一些数据的读写说明,可以此处的数据示例。
SoftBuffer示例
public class SoftBufferExample
{
    private SoftBuffer softBuffer = new SoftBuffer( 1000 );    // 实例化个1000个byte长度的缓冲区
    private Random random = new Random( );


    public void SoftBufferExample1( )
    {
        // 举例设置100-199长度的数据值
        byte[] buffer = new byte[100];
        random.NextBytes( buffer );

        // 本语句是线程安全的,可以在任意的线程进行操作
        softBuffer.SetBytes( buffer, 100 );

        // 然后我们把数据进行读取出来,和上述的buffer是一致的
        byte[] data = softBuffer.GetBytes( 100, 100 );
    }

    public void SoftBufferExample2( )
    {
        // 举例设置读取指定的数据类型的方法
        int[] buffer = new int[10];
        for (int i = 0; i < buffer.Length; i++)
        {
            buffer[i] = random.Next( 10000 );
        }

        // 本语句是线程安全的,可以在任意的线程进行操作
        softBuffer.SetValue( buffer, 100 );

        // 然后我们把数据进行读取出来,和上述的buffer是一致的
        int[] data = softBuffer.GetInt32( 100, 10 );
    }

    // 其他的类型读写也是类似的,如果是自定义类型
    public class UserData : IDataTransfer
    {
        public float temp1 = 0;
        public float temp2 = 0;
        public ushort temp3 = 0;


        public ushort ReadCount => 10;

        public void ParseSource( byte[] Content )
        {
            temp1 = BitConverter.ToSingle( Content, 0 );
            temp2 = BitConverter.ToSingle( Content, 4 );
            temp3 = BitConverter.ToUInt16( Content, 8 );
        }

        public byte[] ToSource( )
        {
            byte[] buffer = new byte[10];
            BitConverter.GetBytes( temp1 ).CopyTo( buffer, 0 );
            BitConverter.GetBytes( temp2 ).CopyTo( buffer, 4 );
            BitConverter.GetBytes( temp3 ).CopyTo( buffer, 8 );
            return buffer;
        }
    }

    public void SoftBufferExample3( )
    {
        // 举例设置读取自定义的数据类型
        UserData userData = new UserData( )
        {
            temp1 = 123.456f,
            temp2 = 1.23f,
            temp3 = 12345,
        };

        // 本语句是线程安全的,可以在任意的线程进行操作
        softBuffer.SetCustomer( userData, 100 );

        // 然后我们把数据进行读取出来,和上述的buffer是一致的
        UserData data = softBuffer.GetCustomer<UserData>( 100 );
    }

}
参见