/** * Read one byte from file. * * @param fd file descriptor * @return data read (-3: error, -2: operation would block, -1: eof, >= 0: valid) */ EXTERNAL int sysReadByte(int fd) { unsigned char ch; SYS_START(); TRACE_PRINTF("%s: sysReadByte %d\n", Me, fd); #ifdef RVM_FOR_HARMONY return hyfile_read(fd, &ch, 1); #else int rc; again: rc = read(fd, &ch, 1); switch (rc) { case 1: /*fprintf("%s: read (byte) ch is %d\n", Me, (int) ch);*/ return (int) ch; case 0: /*fprintf("%s: read (byte) rc is 0\n", Me);*/ return -1; default: /*fprintf("%s: read (byte) rc is %d\n", Me, rc);*/ if (errno == EAGAIN) return -2; // Read would have blocked else if (errno == EINTR) goto again; // Read was interrupted; try again else return -3; // Some other error } #endif // RVM_FOR_HARMONY }
/** * Read multiple bytes from file or socket. * * @param fd file or socket descriptor * @param buf buffer to be filled * @param cnt number of bytes requested * @return number of bytes delivered (-2: error, -1: socket would have blocked) */ EXTERNAL int sysReadBytes(int fd, char *buf, int cnt) { SYS_START(); TRACE_PRINTF("%s: sysReadBytes %d %p %d\n", Me, fd, buf, cnt); #ifdef RVM_FOR_HARMONY return hyfile_read(fd, buf, cnt); #else while (1) { int rc = read(fd, buf, cnt); if (rc >= 0) return rc; int err = errno; if (err == EAGAIN) { TRACE_PRINTF("%s: read on %d would have blocked: needs retry\n", Me, fd); return -1; }else if (err != EINTR) { ERROR_PRINTF("%s: read error %d (%s) on %d\n", Me, err, strerror(err), fd); return -2; } else { // interrupted by signal; try again } } #endif // RVM_FOR_HARMONY }
/* * Class: org_apache_harmony_luni_platform_OSFileSystem * Method: readDirectImpl * Signature: (JJI)J */ JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_readDirectImpl (JNIEnv * env, jobject thiz, jlong fd, jlong buf, jint offset, jint nbytes) { PORT_ACCESS_FROM_ENV (env); return (jlong) hyfile_read ((IDATA) fd, (void *) ((IDATA)(buf+offset)), (IDATA) nbytes); }
/* * Class: org_apache_harmony_luni_platform_OSFileSystem * Method: readImpl * Signature: (J[BII)J */ JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_readImpl (JNIEnv * env, jobject thiz, jlong fd, jbyteArray byteArray, jint offset, jint nbytes) { PORT_ACCESS_FROM_ENV (env); jboolean isCopy; jbyte *bytes = (*env)->GetByteArrayElements (env, byteArray, &isCopy); jlong result; result = (jlong) hyfile_read ((IDATA) fd, (void *) (bytes + offset), (IDATA) nbytes); if(result == -1 && hyerror_last_error_number() == HYPORT_ERROR_FILE_LOCKED){ throwNewExceptionByName(env, "java/io/IOException", netLookupErrorString(env, HYPORT_ERROR_FILE_LOCKED)); } (*env)->ReleaseByteArrayElements (env, byteArray, bytes, result==0 || result==-1 ? JNI_ABORT : 0); return result; }