N
nooneinparticular314159
I'm trying to create a bytebuffer of a specific size, create a
charbuffer view of that bytebuffer, and write a string into that
charbuffer. My bytebuffer always needs to be 4 bytes long (represent a
4 digit number), but no matter how I try to place some chars into the
buffer, I always get an overflow. For example:
//Test fixed length header strings
String ZeroString = "0000";
byte[] ByteSizeArray = ZeroString.getBytes();
int MessageHeaderSize = ByteSizeArray.length;
// int MessageHeaderSize = 4;
System.out.println("Length in bytes of 0000 = "+
MessageHeaderSize);
ByteBuffer HeaderBuffer =
ByteBuffer.allocateDirect(MessageHeaderSize);
System.out.println("HeaderBuffer.capacity() = "+
HeaderBuffer.capacity());
HeaderBuffer.put(ByteSizeArray);
StringBuffer HeaderStringBuffer = new StringBuffer(ZeroString);
CharBuffer HeaderCharBuffer = HeaderBuffer.asCharBuffer();
// Character ZeroChar = new Character("0");
for (int counter = 0; counter < HeaderBuffer.capacity();
counter++){
HeaderCharBuffer.put(counter, (char) 0);
}
Can someone tell me what I'm doing wrong here?
Thanks!
charbuffer view of that bytebuffer, and write a string into that
charbuffer. My bytebuffer always needs to be 4 bytes long (represent a
4 digit number), but no matter how I try to place some chars into the
buffer, I always get an overflow. For example:
//Test fixed length header strings
String ZeroString = "0000";
byte[] ByteSizeArray = ZeroString.getBytes();
int MessageHeaderSize = ByteSizeArray.length;
// int MessageHeaderSize = 4;
System.out.println("Length in bytes of 0000 = "+
MessageHeaderSize);
ByteBuffer HeaderBuffer =
ByteBuffer.allocateDirect(MessageHeaderSize);
System.out.println("HeaderBuffer.capacity() = "+
HeaderBuffer.capacity());
HeaderBuffer.put(ByteSizeArray);
StringBuffer HeaderStringBuffer = new StringBuffer(ZeroString);
CharBuffer HeaderCharBuffer = HeaderBuffer.asCharBuffer();
// Character ZeroChar = new Character("0");
for (int counter = 0; counter < HeaderBuffer.capacity();
counter++){
HeaderCharBuffer.put(counter, (char) 0);
}
Can someone tell me what I'm doing wrong here?
Thanks!