如何在不创建新数组的情况下用字符串填充字节数组?
How can i fill a byte array with a string without creating a new array?
我正在尝试打开多个 websocket,我需要以某种方式为每个套接字使用相同的缓冲区或在 sending/receiving 新消息之前清除它们。
接收方法很好,因为我可以传递字节数组的参数,它会在不创建新的字节数组实例的情况下填充该参数。
我可以用 BitConverter.GetBytes
方法做什么?我需要开始使用不安全的上下文并使用带指针参数的重载 GetBytes
吗?还有其他方法吗?
我需要它来填充我将在构造函数中定义的 outBytes
变量。
public class Client:IDisposable
{
//Fields
public char[] innerData { get; private set; }
private byte[] inBytes;
private byte[] outBytes;
private ArraySegment<byte> inSegment;
private ArraySegment<byte> outSegment;
private WebSocket webSocket;
public WebSocket Socket => this.webSocket;
public readonly string clientID;
//Auxiliary
private const int BufferSize = 1024;
public static Client CreateClient(WebSocket socket, string id)
{
Client client = new Client(socket, id);
return client;
}
public Client(WebSocket socket, string id)
{
this.inBytes = new byte[BufferSize];
this.inSegment = new ArraySegment<byte>(inBytes);
this.outBytes = new byte[BufferSize];
this.outSegment = new ArraySegment<byte>(outBytes);
this.webSocket = socket;
this.clientID = id;
this.innerData = new char[BufferSize];
}
public async Task<WebSocketReceiveResult> ReceiveResult()
{
if(this.webSocket.State!=WebSocketState.Open)
{
return null;
}
WebSocketReceiveResult result = await this.webSocket.ReceiveAsync(this.inSegment, CancellationToken.None);
Encoding.UTF8.GetChars(this.inSegment.Array, 0, BufferSize, this.innerData, 0);
return result;
}
public async Task SendMessage(string message)
{
if(this.webSocket.State==WebSocketState.Open)
{
this.outBytes = Encoding.UTF8.GetBytes(message, 0, message.Length); //How can i fill the already existing outBytes?
await this.webSocket.SendAsync(this.outSegment, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
public void Dispose()
{
if(this.webSocket.State!=WebSocketState.Closed)
{
this.webSocket.Dispose();
this.webSocket = null;
}
}
}
我需要以某种方式使用已经存在的outBytes
,当我转换我将send.At的消息时,outBytes
的行为就像一个指针,每次SendMessage方法的迭代GetBytes
将产生一个新的字节数组。
你显然对 GetBytes 的工作原理有错误的误解,它不会每次都生成一个新数组,这个重载:
Encoding.GetBytes Method (String, Int32, Int32, Byte[], Int32)
将
encodes a set of characters from the specified string into the specified byte array (From MSDN)
所以你的台词应该是
Encoding.UTF8.GetBytes(message, 0, message.Length, this.outBytes, 0);
该函数将使用 UTF8 编码将此字符串转换为字节来填充您的数组...
您可以使用整数 return 值来检查已写入数组的字节数。
我正在尝试打开多个 websocket,我需要以某种方式为每个套接字使用相同的缓冲区或在 sending/receiving 新消息之前清除它们。 接收方法很好,因为我可以传递字节数组的参数,它会在不创建新的字节数组实例的情况下填充该参数。
我可以用 BitConverter.GetBytes
方法做什么?我需要开始使用不安全的上下文并使用带指针参数的重载 GetBytes
吗?还有其他方法吗?
我需要它来填充我将在构造函数中定义的 outBytes
变量。
public class Client:IDisposable
{
//Fields
public char[] innerData { get; private set; }
private byte[] inBytes;
private byte[] outBytes;
private ArraySegment<byte> inSegment;
private ArraySegment<byte> outSegment;
private WebSocket webSocket;
public WebSocket Socket => this.webSocket;
public readonly string clientID;
//Auxiliary
private const int BufferSize = 1024;
public static Client CreateClient(WebSocket socket, string id)
{
Client client = new Client(socket, id);
return client;
}
public Client(WebSocket socket, string id)
{
this.inBytes = new byte[BufferSize];
this.inSegment = new ArraySegment<byte>(inBytes);
this.outBytes = new byte[BufferSize];
this.outSegment = new ArraySegment<byte>(outBytes);
this.webSocket = socket;
this.clientID = id;
this.innerData = new char[BufferSize];
}
public async Task<WebSocketReceiveResult> ReceiveResult()
{
if(this.webSocket.State!=WebSocketState.Open)
{
return null;
}
WebSocketReceiveResult result = await this.webSocket.ReceiveAsync(this.inSegment, CancellationToken.None);
Encoding.UTF8.GetChars(this.inSegment.Array, 0, BufferSize, this.innerData, 0);
return result;
}
public async Task SendMessage(string message)
{
if(this.webSocket.State==WebSocketState.Open)
{
this.outBytes = Encoding.UTF8.GetBytes(message, 0, message.Length); //How can i fill the already existing outBytes?
await this.webSocket.SendAsync(this.outSegment, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
public void Dispose()
{
if(this.webSocket.State!=WebSocketState.Closed)
{
this.webSocket.Dispose();
this.webSocket = null;
}
}
}
我需要以某种方式使用已经存在的outBytes
,当我转换我将send.At的消息时,outBytes
的行为就像一个指针,每次SendMessage方法的迭代GetBytes
将产生一个新的字节数组。
你显然对 GetBytes 的工作原理有错误的误解,它不会每次都生成一个新数组,这个重载:
Encoding.GetBytes Method (String, Int32, Int32, Byte[], Int32)
将
encodes a set of characters from the specified string into the specified byte array (From MSDN)
所以你的台词应该是
Encoding.UTF8.GetBytes(message, 0, message.Length, this.outBytes, 0);
该函数将使用 UTF8 编码将此字符串转换为字节来填充您的数组... 您可以使用整数 return 值来检查已写入数组的字节数。