[英]C# equivalent for ByteArrayOutputStream in java
I have java
code as 我有java
代码
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(tokenBytes);
baos.write(signedData);
byte[] finalout = baos.toByteArray();
where tokenBytes and signedData are byte arrays.In c#
I have written as 其中tokenBytes和signedData是字节数组。在c#
我写的是
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(tokenBytes);
writer.Write(signature);
}
finalBytesToSend = stream.ToArray();
}
where tokenBytes , signature and finaleBytesToSend are byte arrays. 其中tokenBytes,signature和finaleBytesToSend是字节数组。
Is it correct? 这是正确的吗? OR is there any other way to do it? 或者还有其他方法吗?
Presumably I assume you are writing a Java-C# serialization scheme. 据推测,我假设您正在编写Java-C#序列化方案。 I think there are 3 things you might want to be careful: 我认为有三件事你可能要小心:
In my opinion if you are transferring data between Java and C#, you'd better use simply ByteBuffer in Java side and MemoryStream/BinaryReader/BinaryWrite in C# side. 在我看来,如果你在Java和C#之间传输数据,你最好使用Java端的ByteBuffer和C#端的MemoryStream / BinaryReader / BinaryWrite。 Setting UTF-16 Big Endian correctly in C# side, and write your own de-serializer for stuff like int/long/double primitives. 在C#端正确设置UTF-16 Big Endian,并为int / long / double primitives之类的东西编写自己的反序列化器。 That makes it work. 这使它工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.