一、简介
缓存Socket发送的数据,放到队列中,然后固定地从队列总读取字节。
关键点1:List<byte>列表
List<byte> VideoList = new List<byte>();
List数组可以实现快速地增删。如果用byte[] VideoList = new byte[1000],来缓存,那么久需要进行移位等等操作,增加了复杂度。
List数组本身移位的功能、插入的动能。
关键点2:VideoList 不但可以插入单个数据,还可以插入 byte[]字节数组
byte[] byteD = new byte[4] { 4, 4, 4, 4 };
byte[] byteB = new byte[2] { 2, 2 };
//插入:一个一个字节插入
VideoList.Add(2);
VideoList.Add(3);
VideoList.Add(10);
VideoList.Add(32);
VideoList.Add(12);
//插入:将字节数组插入到末尾
VideoList.AddRange(byteD);
VideoList.AddRange(btVedioRecByte);
关键点3:List<byte>列表类型的内容可以拷贝到byte[]字节数组中
//拷贝
byte[] btBitmapImage = new byte[IMAGE_LENGHT];
Array.Copy(VideoList.ToArray(), btBitmapImage, IMAGE_LENGHT); //将List<byte>的内容拷贝到byte[]:VideoList.ToArray()
二、Codes
#region List<byte> 列表 队列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QueueSample
{
class Program
{
static List<byte> VideoList = new List<byte>();
const int IMAGE_LENGHT = 3;
byte[] btBitmapImage = new byte[IMAGE_LENGHT];
static void Main(string[] args)
{
byte[] byteC = new byte[3] { 3, 3,3};
VideoQuene(byteC);
}
private static byte[] VideoQuene(byte[] btVedioRecByte)
{
byte[] byteD = new byte[4] { 4, 4, 4, 4 };
byte[] byteB = new byte[2] { 2, 2 };
//插入:一个一个字节插入
VideoList.Add(2);
VideoList.Add(3);
VideoList.Add(10);
VideoList.Add(32);
VideoList.Add(12);
//插入:将字节数组插入到末尾
VideoList.AddRange(byteD);
VideoList.AddRange(btVedioRecByte);
//拷贝
byte[] btBitmapImage = new byte[IMAGE_LENGHT];
Array.Copy(VideoList.ToArray(), btBitmapImage, IMAGE_LENGHT); //将List<byte>的内容拷贝到byte[]:VideoList.ToArray()
//删除
VideoList.RemoveRange(0, IMAGE_LENGHT);
return btBitmapImage;
}
}
}
#endregion