从套接字的 byte[] 开头解析 int

Parsing int from the start of a byte[] from a socket

我有一个 Java 应用程序正在从接收 XML 不同大小的 TCP 套接字读取数据。给定数据包的前 5 个字节应该指示剩余消息的大小。如果我手动创建一个大字节 [] 并读取数据,我可以成功读取消息和 xml。

以下是生成数据的应用程序手册中的说明:

Each message is preceded by the message size indicator which is a 32-bit unsinged integer using the network bytes order method. For example: \x05\x00\x00\x00\x30\x31\x30\x32\x00 indicates the message size of an ack which is 5 bytes included the fifth message byte '[=11=]'. The size indicator specifies everything following the size indicator itself.

但是我不知道如何将前 5 个字节解码为一个整数,我可以使用该整数来正确调整 byte[] 的大小以读取消息的其余部分。我得到随机结果:

这是我用来解析消息的代码:

DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
BufferedInputStream inFromServer = new BufferedInputStream(clientSocket.getInputStream());

byte[] data = new byte[10];
inFromServer.read(data);
String result = new String(data, "ISO-8859-1");

Logger.info(data+"");

//PROBLEM AREA: Tried reading different byte lengths but no joy
//This should be a number but it never is. Often strange symbols
byte[] numeric = Arrays.copyOfRange(data,1,5);
String numericString = new String(numeric, "ISO-8859-1");

//Create a huge array to make sure everything gets captured. 
//Want to use the parsed value from the start here
byte[] message = new byte[1000000];
inFromServer.read(message);

//This works as expected and returns correctly formatted XML
String fullMessage = new String(message, "ISO-8859-1");

Logger.info("Result "+result+ " Full message "+fullMessage);

长度看起来像小端。您仍然可以使用 DataInputStream,但必须交换字节。如果您使用 NIO 的 SocketChannel 和 ByteBuffer,您可以设置字节顺序,但这可能更难使用。

// only do this once per socket.
DataInputStream in = new DataInputStream(
                                  new BufferedInputStream(clientSocket.getInputStream()));

// for each message.
int len0 = in.readInt();
int len = Integer.reverseBytes(len0);
assert len < 1 << 24;

byte[] bytes = new byte[len];
in.readFully(bytes);

String text = new String(bytes, "ISO-8859-1").trim();
int number = Integer.parseInt(text);

网络字节顺序又名 big-endian。但是看到您的数据似乎实际上使用了 little-endian 。至少 5 看起来像 little-endian 中的前 4 个字节,但不是 big-endian 中的那些。所以你需要读取那些字节,考虑 little-endian 并转换为 long 以考虑 "unsigned-ness".

public static void main(String[] args) throws IOException {
    DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(null));

    int iSize = inFromServer.readInt();
    iSize = Integer.reverseBytes(iSize); //read as little-endian

    long count = Integer.toUnsignedLong(iSize); //unsigned int
}