Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert nio bytebufer to String newbie  RSS feed

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from a socket into a nio ByteBuffer, buffer, and then I need the data as a string.

This compiles


but fails at runtime with the message: java.lang.UnsupportedOperationException

I'm at a loss as to why it fails as buffer.array() should return the underlying byte[] and how to get the string I need.

TIA
Nate
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the Javadocs? It clearly says
Throws:
ReadOnlyBufferException - If this buffer is backed by an array but is read-only
UnsupportedOperationException - If this buffer is not backed by an accessible array
Apparently hasArray() returns false for the byte buffer. How did you get it?
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read from a SocketChannel into an nio ByteBuffer and then put the ByteBuffer
into a FIFO queue as a String so that the data may be processed;

It looked to me as if buffer.array() returned the address of the underlying array of byte and if I had that I could get a String.

 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a solution via Google. My test class that sends data to the port does not encode but I get a readable string. This code will be used on a captive "LAN" with 3 or 4 computers on an aircraft All using the same OS - I would think that the charactor encoding would be the same.



I combined the last three statements statements after I tested the code above and got what's below. Am I right in assuming that there is only one copy in this code? Insertion into the FIFO buffer makes another copy of course.


Is there a better, that is, faster way?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you were using ByteBuffer.allocateDirect. It's Javadoc says this:
public static ByteBuffer allocateDirect(int capacity)
Allocates a new direct byte buffer.

The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined. Whether or not it has a backing array is unspecified.
 
Nate Lockwood
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new buffer's position will be zero, its limit will be its capacity, and its mark will be undefined. Whether or not it has a backing array is unspecified.


OK, I see why array() doesn't work. Is charset the best way to interpret the data as a String?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!