Java-ByteArrayOutputStream替换,但仅用于1个值

Java-ByteArrayOutputStream替换,但仅用于1个值,java,client-server,outputstream,Java,Client Server,Outputstream,在第二种情况下,是否有可能因为我不需要数组而截断它。 这是我的代码: public static byte[] createHandshakeMessage(String host, int port, int protocol) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DataOutputStream output = new DataOutput

在第二种情况下,是否有可能因为我不需要数组而截断它。 这是我的代码:

    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)