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.
content.toCharArray()[i]
is ugly, you are allocating a new char array in each loop iteration. Build the char array outside, or usecontent.charAt(i)