/** * Write multiple bytes to file or socket. * * @param file or socket descriptor * @param buffer to be written * @param number of bytes to write * @return number of bytes written (-2: error, -1: socket would have blocked, -3 EPIPE error) */ EXTERNAL int sysWriteBytes(int fd, char *buf, int cnt) { SYS_START(); TRACE_PRINTF("%s: sysWriteBytes %d %p %d\n", Me, fd, buf, cnt); #ifdef RVM_FOR_HARMONY return hyfile_write(fd, buf, cnt); #else while(1) { int rc = write(fd, buf, cnt); if (rc >= 0) return rc; int err = errno; if (err == EAGAIN) { TRACE_PRINTF("%s: write on %d would have blocked: needs retry\n", Me, fd); return -1; } else if (err == EINTR) { // interrupted by signal; try again } else if (err == EPIPE) { TRACE_PRINTF("%s: write on %d with nobody to read it\n", Me, fd); return -3; } else { ERROR_PRINTF("%s: write error %d (%s) on %d\n", Me, err, strerror( err ), fd); return -2; } } #endif // RVM_FOR_HARMONY }
/* * Class: org_apache_harmony_luni_platform_OSFileSystem * Method: writeDirectImpl * Signature: (JJI)J */ JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_writeDirectImpl (JNIEnv * env, jobject thiz, jlong fd, jlong buf, jint offset, jint nbytes) { PORT_ACCESS_FROM_ENV (env); return (jlong) hyfile_write ((IDATA) fd, (const void *) ((IDATA)(buf+offset)), (IDATA) nbytes); }
/* * Class: org_apache_harmony_luni_platform_OSFileSystem * Method: writeImpl * Signature: (J[BII)J */ JNIEXPORT jlong JNICALL Java_org_apache_harmony_luni_platform_OSFileSystem_writeImpl (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_write ((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, JNI_ABORT); return result; }
/** * Write one byte to file. * * @param fd file descriptor * @param data data to write * @return -2 operation would block, -1: error, 0: success */ EXTERNAL int sysWriteByte(int fd, int data) { SYS_START(); char ch = data; TRACE_PRINTF("%s: sysWriteByte %d %c\n", Me, fd, ch); #ifdef RVM_FOR_HARMONY return hyfile_write(fd, &ch, 1); #else while(1) { int rc = write(fd, &ch, 1); if (rc == 1) { return 0; // success } else if (errno == EAGAIN) { return -2; // operation would block } else if (errno == EINTR) { } else { ERROR_PRINTF("%s: writeByte, fd=%d, write returned error %d (%s)\n", Me, fd, errno, strerror(errno)); return -1; // some kind of error } } #endif // RVM_FOR_HARMONY }