Reading/Writing 来自字节[]

Reading/Writing from byte[]

在java中有什么方法可以从byte[]到read/write整数、布尔值和字符串吗?

Like:
write(50550);
write(true);
write("bla bla");

And then:
int i = readInt();
int j = readBoolean();
String res = readUTF();

Walter 和 fge 回答了两种不同的方法来实现这一点。唯一的问题仍然存在:"Which way is faster?"

您要查找的是 DataInputStreamDataOutputStream

两者都覆盖 resp.an InputStreamOutputStream 并且可以 read/write 各种基本类型,"transformed UTF" 数据等

至于写入字节数组本身,您想使用 ByteArrayOutputStream,然后将 DataOutputStream 包裹在它上面:

final ByteArrayOutputStream out = new ByteArrayOutputStream();
final DataOutputStream dataOut = new DataOutputStream(out);

// use dataOut; then:

final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
final DataInputStream dataIn = new DataInputStream(in);

// read back

无论如何,在您的一条评论中也不清楚您要做什么;如果这只是二进制数据,则改为使用 ByteBuffer ;你可以在使用它之前随意改变它的字节顺序(默认情况下它是大端),在其中包装字节数组等......不同之处在于 Java 没有无符号原始整数类型(好吧,保存为 char 但这个是一只奇特的野兽)。

总而言之,这看起来像是一个 XY 问题,所以我建议您编辑您的问题并充分解释您要尝试做的事情。

您似乎是从 DataInputStream 和 DataOutputStream 中搜索。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// Like:
dos.writeInt(50550);
dos.writeBoolean(true);
dos.writeUTF("bla bla");
byte[] bytes = baos.toByteArray();

DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
// And then:
int i = dis.readInt();
boolean flag = dis.readBoolean();
String res = dis.readUTF();

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'");

打印

i: 50550, flag: true, res: 'bla bla'

stdext::writeULE16, stdext::writeULE32, stdext::writeULE64 and stdext::readULE16, stdext::readULE32, stdext::readULE64

这意味着您需要一种文本格式,在这种情况下我建议使用 PrintWriter

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, "UTF-16LE"));
// Like:
pw.println(50550);
pw.println(true);
pw.println("bla bla");
pw.close();
byte[] bytes = baos.toByteArray();

Scanner in = new Scanner(new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-16LE"));
// And then:
int i = in.nextInt();
boolean flag = in.nextBoolean(); in.nextLine(); // discard the rest of the line
String res = in.nextLine();

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'");

除了底层字节将采用 UTF-16LE 编码外,打印结果相同。这种方法还支持 UTF-16BE、UTF-32LE、UTF-32BE(和 UTF-8)

    ByteBuffer buffer = ByteBuffer.allocate(0x1000);
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.putInt(100);
    //store booleans as 1 or 0 in a byte
    buffer.put((byte) (true ? 1 : 0));
    buffer.put((byte) (false ? 1 : 0));

    //store string as [int length, byte array]
    String str = "Hello World!";
    byte[] strBytes = str.getBytes(StandardCharsets.UTF_8); //use any charset you want here
    buffer.putInt(strBytes.length);
    buffer.put(strBytes);


    buffer.flip();
    byte[] data = new byte[buffer.remaining()];
    buffer.get(data);
    System.out.println("Total Bytes: " + data.length);


    ByteBuffer bb = ByteBuffer.wrap(data);

    System.out.println(bb.getInt());
    System.out.println(bb.get() != 0);
    System.out.println(bb.get() != 0);
    byte[] inStr = new byte[bb.getInt()];
    bb.get(inStr);
    String myStr = new String(inStr, StandardCharsets.UTF_8);
    System.out.println(myStr);