0

How can I do something like ByteBuffer.putBoolean and ByteBuffer.getBoolean?

As with DataInputStream there is readBoolean and with DataOutputStream there is writeBoolean; I don't find the ability to write/read booleans with ByteBuffer. Is there a way to achieve the same behavior?

Jire
  • 9,680
  • 14
  • 52
  • 87

2 Answers2

3

See the Javadoc. writeBoolean() writes one byte which is 1 or 0 according as the argument is true or false. So do that.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

You could do something like

    boolean b = buf.get() == 1;
    buf.put((byte) (b ? 1 : 0));
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275