I'm writing some simple networking code which involves working with ByteBuffers. For some reason, the following code throws an UnsopportedOperationException on two of my test devices:
int send = Integer.parseInt(edtxt.getText().toString());
OutputStream out = sock.getOutputStream();
ByteBuffer buf = ByteBuffer.allocateDirect(1);
buf.order(ByteOrder.BIG_ENDIAN);
buf.put((byte)send);
buf.rewind();
byte[] outa = buf.array(); //Exception thrown here
out.write(outa);
The two devices it fails on are:
Sony Ericsson Xperia Play running Android 2.3.3
Motorla Droid X2 running Android 2.3.5
The two it works on are:
LG G3 running Android 4.4.2
Nexus 4 running Android 4.4.4
The docs say that UnsupportedOperationException will be thrown if the byte buffer isn't based on an array. Is this a difference between Gingerbread and KitKat that I just need to deal with, or is it just a case of bad practice?