0

I try to access a byte[] via a ByteBuffer in order to represent a file-type I defined. The first positions within the byte[] contain some Metadata and get treated with bit-manipulations. So they do not represent a char at all.

I want to add file-data (chars e. g.) at a certain fixed position.

byte[] file_portion contains a portion of a large file: the beginning section. Which includes the header with the Metadata. content is a String with information I want to add to that buffer. start_pos is the first position to hold the new file-data from content.

ByteBuffer my_content = ByteBuffer.allocate(this.file_portion.length);
content_buffer.wrap(this.file_portion);

for (int i = 0; i < content.length(); i++) {
    char tmp = content.toCharArray()[i];
    my_content.put(this.start_pos + i, (byte) tmp)
}

If I remap this I get a garbage and emptyness:

CharBuffer debug = my_content.asCharBuffer();
System.out.println("debug " + debug);

I could understand if the first positions show corrupted chars... but not a single one position is correct.

1
  • 2
    Besides the bug, content.toCharArray()[i] is ugly, you are allocating a new char array in each loop iteration. Build the char array outside, or use content.charAt(i)
    – leonbloy
    Commented Jan 13, 2011 at 21:30

2 Answers 2

2

If you are adding chars to a ByteBuffer and expecting them to be readable with a CharBuffer view, you should be using putChar(...) not put(...).

EDITED: per OP comments.

For example:

char[] chars = content.toCharArray();  // removed from loop per leonbloy's excellent comment  
CharBuffer cbuf = my_content.asCharBuffer();

for (int i = 0; i < content.length(); i++) {
    cbuf.putChar(chars[i]);
}

CharBuffer debug = my_content.asCharBuffer();
System.out.println(debug);

my_content.position(my_content.position() + 2*chars.length);

Otherwise the CharBuffer is reading two of your consecutive bytes as a single char. Now the cbuf buffer will start loading chars at the same point your byte buffer left off. After loading all of the chars, your original ByteBuffer will be positioned to the next location. Hopefully this is what you're looking for.

6
  • That .flip() looks very useful. But I still have to add the chars at a specifc position. If I do that with my_content.setPosition(this.start_pos+i) or even + i +1 the whole conversion gets corrupted.
    – wishi
    Commented Jan 13, 2011 at 21:30
  • 1
    I guess the point I'm trying to make is that you need to let the Buffers do two things for you to make your life easier. 1) manage the position in the buffer and 2) manage the data format of items being read/written. In your original code you're trying to do both, and running into problems because of it.
    – robert_x44
    Commented Jan 13, 2011 at 21:44
  • Thanks a lot for your efforts. You're absolutly right: managing the position is problematic here and I have to separate this. Still I have the problem that cbuf is filled from position 0. I need to fill it starting from position n. Each time I do this the whole stuff gets corrupted. :(
    – wishi
    Commented Jan 13, 2011 at 22:06
  • 1
    @wishi I guess I would need to see more of the context of what you're doing to suggest code that solves your problem.
    – robert_x44
    Commented Jan 13, 2011 at 22:14
  • Finally got it... thanks for the help. The CharBuffer has the .put(index, char) I needed. Thanks a lot!!
    – wishi
    Commented Jan 13, 2011 at 22:18
2

Are you aware that in Java a char occupies two bytes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.