Search code examples
cserial-portjava-native-interface

does read() in c read null character


I have written program to read from usb port (which is not connected to anything) .From java I am passing int filedescriptor and byte array and in jni I am converting byte array to char* and using read().

jbyte *bufferPtr2 = (*env)->GetByteArrayElements(env, buf, NULL);  
unsigned char* d_data2 = (unsigned char*)bufferPtr2;    
n = read(fd, d_data2, lengthOfArray); 

After executing , n=1 but d_data2 is empty. Why is this? Does read() read null character as data?


Solution

  • Per the POSIX documentation for read():

    The read() function shall attempt to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.

    The actual value of the bytes that are read does not matter.

    So yes, read() will read NUL bytes.