简体   繁体   English

套接字上下文中的Java和二进制数据

[英]Java and Binary data in the context of sockets

Java newbie here. Java新手在这里。 Are there any helper functions to serialize data in and out of byte arrays? 是否有任何辅助函数可以在字节数组中对数据进行序列化? I am writing a Java package that implements a network protocol. 我正在编写一个实现网络协议的Java包。 So I have to write some typical variables like a version (1byte), sequence Number (long) and binary data (bytes) in a loop. 因此,我必须在循环中编写一些典型变量,例如版本(1字节),序列号(长)和二进制数据(字节)。 How do I do this in Java? 我如何用Java做到这一点? Coming from CI am thinking of creating a byte array of the required size and then since there is no memcpy() I am converting the long into a temporary byte array and then copying it into the actual byte array. 来自CI的人正在考虑创建所需大小的字节数组,然后由于没有memcpy(),我将long转换为临时字节数组,然后将其复制到实际的字节数组中。 It seems so inefficient and also really error prone. 看起来效率很低,而且也很容易出错。 Is there a class I could use to marshall and unmarshall parameters to a byte array? 有没有可以用于将参数编组和解组到字节数组的类?

Also why does all the Socket classes only deals with char[] and not byte[]? 同样为什么所有的Socket类都只处理char []而不处理byte []? A socket by definition has to deal with binary data also. 根据定义,套接字还必须处理二进制数据。 How is this done in Java? 这是如何在Java中完成的?

I am sure what I am missing is the Java mindset. 我确定我缺少的是Java思维方式。 Appreciate it if some one can point it to me. 如果有人可以指向我,请感激它。

EDIT: I did look at DataOutputStream and DataInputStream but I cannot convert the bytes to a String not to a byte[] which means the information might be lost in the conversion to write to a socket. 编辑:我确实看过DataOutputStream和DataInputStream,但是我无法将字节转换为String而不是byte [],这意味着信息可能会在写入套接字的转换中丢失。

  • Pav PAV

Have a look at DataInputStream , DataOutputStream , ObjectInputStream and ObjectOutputStream . 看一下DataInputStreamDataOutputStreamObjectInputStreamObjectOutputStream Check first if the layout of the data is acceptable to you. 首先检查数据的布局是否可以接受。 Also, Serialization. 另外,序列化。

套接字既不处理char[]也不处理byte[]而是处理用于读取和写入字节的InputStreamOutputStream

If you are sending the data over a socket, then you don't need a temporary byte array at all; 如果要通过套接字发送数据,则根本不需要临时字节数组。 you can wrap the socket's OutputStream with DataOutputStream or ObjectOutputStream and just write what you want to write. 您可以使用DataOutputStream或ObjectOutputStream包装套接字的OutputStream并只写要写的内容。

There might be an aspect I've missed that means you do actually need temporary byte arrays. 我可能错过了一个方面,这意味着您实际上确实需要临时字节数组。 If so, look at ByteArrayOutputStream. 如果是这样,请查看ByteArrayOutputStream。 Also, there's no memcpy(), sure, but there is System.arraycopy. 另外,当然没有memcpy(),但是有System.arraycopy。

As above, DataInputStream and DataOutputStream are exactly what you are looking for. 如上所述,DataInputStream和DataOutputStream正是您想要的。 Re your comment about String, if you're planning to use Java Strings over the wire, you're not designing a network protocol, youre designing a Java protocol. 关于String的评论,如果您打算通过网络使用Java String,则您不是在设计网络协议,而是在设计Java协议。 There are readUTF() and writeUTF() if you're sure the other end is Java or if you can code the other end to understand these formats. 如果您确定另一端是Java或可以编写另一端以理解这些格式,则有readUTF()和writeUTF()。 Or you can send as bytes along with the appropriate charset, or predefine the charset for the entire protocol if that makes sense. 或者,您可以将字节和适当的字符集一起发送,或者在有意义的情况下为整个协议预定义字符集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM