在第二种情况下是否可以以某种方式截断它,因为我不需要数组。 这是我的代码:
public static byte[] createHandshakeMessage(String host, int port, int protocol) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
handshake.writeByte(0x00); //packet id for handshake
//Fields->
//some code...
return buffer.toByteArray();
}
public static byte createClient(){
ByteArrayOutputStream buffer = new ByteArrayOutputStream();//is there a replacement for this?
DataOutputStream handshake = new DataOutputStream(buffer);
handshake.writeByte(0x00); //packet id for login start
String offlineSession = "Username";
//Fields->
writeString(handshake, offlineSession, StandardCharsets.UTF_8);
return buffer.toByteArray();
}
最佳答案
只需使用一个简单的OutputStream.write(b)
。
关于Java - ByteArrayOutputStream 替换,但仅适用于 1 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60478544/