I'm currently developing an application where users can edit a ByteBuffer via a hex editor interface and also edit the corresponding text through a JTextPane. My current issue is because the JTextPane requires a String I need to convert the ByteBuffer to a String before displaying the value. However, during the conversion invalid characters are replaced by the charsets default replacement character. This squashes the invalid value so when I convert it back to a byte buffer the invalid characters value is replace by the byte value of the default replacement character. Is there an easy way to retain the byte value of an invalid character in a string? I've read the following stackoverflow posts but usually folks want to just replace unprintable characters, I need to preserve them.
Java: Converting String to and from ByteBuffer and associated problems
Is there an easy way to do this or do I need to keep track of all the changes that happen in the text editor and apply them to the ByteBuffer?
Here is code demonstrating the problem. The code uses byte[] instead of ByteBuffer but the issue is the same.
byte[] temp = new byte[16];
// 0x99 isn't a valid UTF-8 Character
Arrays.fill(temp,(byte)0x99);
System.out.println(Arrays.toString(temp));
// Prints [-103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103]
// -103 == 0x99
System.out.println(new String(temp));
// Prints ����������������
// � is the default char replacement string
// This takes the byte[], converts it to a string, converts it back to a byte[]
System.out.println(Arrays.toString(new String(temp).getBytes()));
// I need this to print [-103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103, -103]
// However, it prints
//[-17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67, -17, -65, -67]
// The printed byte is the byte representation of �