Search code examples
javac++java-native-interface

does using String in java to hold binary data is wrong?


I need to pass binary data (red from a file) from java to c++ (using jni), so I have a C++ function that expects string (because in c++ string is just char array).

I read my binary file in java using the following code :

byte[] buffer = new byte[512];
FileInputStream in = new FileInputStream("some_file");
int rc = in.read(buffer);
while(rc != -1)
{
 // rc should contain the number of bytes read in this operation.
  // do stuff...

  // next read
  rc = in.read(buffer);
String s = new String(buffer);
// here i call my c++ function an pass "s"
}

I'm worried about the line that creates the string, what actually happens when i put the buffer inside a string ? It seems that when the data arrives to my c++ code it is different from what i expect him to be.

does the "string" constructor changes the data somehow ?


Solution

  • Strings are not char arrays at all. They are complex Unicode beasts with semantic interactions between the codepoints, different binary encodings, etc. This is true for all programs. The only thing that's different about C++ is that they haven't finished complaining and started doing things about it yet.

    In all languages, for binary data, use an explicit binary data type, like array of bytes.