trouble with placing strings into charbuffers

  • Thread starter nooneinparticular314159
  • Start date
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!
 
G

grasp06110

Does the solution shown below work for you? It looks like the call to
headerBuffer.asCharBuffer(); was returning a zero capacity buffer. The
loop was using the capacity of the other buffer as a counter.
Therefore, the loop was entered (capacity of the other buffer was 4)
and an attempt was made to add data to the zero capacity buffer.

What IDE are you using? Eclipse has some nice debugging tools that
might have been helpful.

Hope this helps,
John


/**
*
*/
package com.mydomain.example;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
* @author greshje
*
*/
public class Example {

public static void main(String[] args) throws Exception {
// Test fixed length header strings
String zeroString = "0000";
byte[] bytes = zeroString.getBytes();
int messageHeaderSize = bytes.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(bytes);

StringBuffer headerStringBuffer = new StringBuffer(zeroString);
// CharBuffer headerCharBuffer = headerBuffer.asCharBuffer();
CharBuffer headerCharBuffer = CharBuffer.allocate(4);
for (int counter = 0;
counter < headerCharBuffer.capacity();
counter++) {
System.out.println(counter);
headerCharBuffer.put(counter, (char) 0);
}
}

}
 
N

nooneinparticular314159

Wow. Thanks! That makes sense. So basically, I need to be sure to
initialize the back end buffer, and what I was doing before didn't do
that. :)

I use Netbeans.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top