【问题标题】:Java - ByteArrayOutputStream replacement but only for 1 valueJava - ByteArrayOutputStream 替换,但仅适用于 1 个值
【发布时间】:2020-03-01 18:45:42
【问题描述】:

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

    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();
}

【问题讨论】:

    标签: java client-server outputstream


    【解决方案1】:

    只需使用简单的OutputStream.write(b)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2012-12-25
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多