简体   繁体   English

是否等效于ByteBuffer的DataOutputStream.readBoolean和DataOutputStream.writeBoolean?

[英]Equivalent of DataOutputStream.readBoolean and DataOutputStream.writeBoolean for ByteBuffer?

How can I do something like ByteBuffer.putBoolean and ByteBuffer.getBoolean ? 我该如何做类似ByteBuffer.putBooleanByteBuffer.getBoolean的操作

As with DataInputStream there is readBoolean and with DataOutputStream there is writeBoolean ; DataInputStream一样,readBoolean ,而DataOutputStream里有writeBoolean I don't find the ability to write/read booleans with ByteBuffer. 我找不到使用ByteBuffer写入/读取布尔值的功能。 Is there a way to achieve the same behavior? 有没有办法实现相同的行为?

See the Javadoc. 请参阅Javadoc。 writeBoolean() writes one byte which is 1 or 0 according as the argument is true or false. writeBoolean()根据参数为true或false写入一个1或0的字节。 So do that. 这样吧。

You could do something like 你可以做类似的事情

    boolean b = buf.get() == 1;
    buf.put((byte) (b ? 1 : 0));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM