518 result = WriteFile(h, /* File handle to write */
519 buf, /* pointers to the buffers */
520 len, /* number of bytes to write */
521 &written, /* receives number of bytes written */
522 lpOv); /* overlapped struct */
523 }
524 if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
525 return -1;
526 }
527 return (jint)written;
528 }
529
530 jint handleWrite(FD fd, const void *buf, jint len) {
531 return writeInternal(fd, buf, len, JNI_FALSE);
532 }
533
534 jint handleAppend(FD fd, const void *buf, jint len) {
535 return writeInternal(fd, buf, len, JNI_TRUE);
536 }
537
538 void
539 handleClose(JNIEnv *env, jobject this, jfieldID fid)
540 {
541 jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
542 if (fileDescriptor == NULL) {
543 return;
544 }
545 fileDescriptorClose(env, fileDescriptor);
546 }
547
548 // Function to close the fd held by this FileDescriptor and set fd to -1.
549 void
550 fileDescriptorClose(JNIEnv *env, jobject this)
551 {
552 FD fd = (*env)->GetLongField(env, this, IO_handle_fdID);
553 HANDLE h = (HANDLE)fd;
554 if ((*env)->ExceptionOccurred(env)) {
555 return;
556 }
557
558 if (h == INVALID_HANDLE_VALUE) {
559 return;
560 }
561
562 /* Set the fd to -1 before closing it so that the timing window
563 * of other threads using the wrong fd (closed but recycled fd,
564 * that gets re-opened with some other filename) is reduced.
565 * Practically the chance of its occurance is low, however, we are
566 * taking extra precaution over here.
567 */
|
518 result = WriteFile(h, /* File handle to write */
519 buf, /* pointers to the buffers */
520 len, /* number of bytes to write */
521 &written, /* receives number of bytes written */
522 lpOv); /* overlapped struct */
523 }
524 if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
525 return -1;
526 }
527 return (jint)written;
528 }
529
530 jint handleWrite(FD fd, const void *buf, jint len) {
531 return writeInternal(fd, buf, len, JNI_FALSE);
532 }
533
534 jint handleAppend(FD fd, const void *buf, jint len) {
535 return writeInternal(fd, buf, len, JNI_TRUE);
536 }
537
538 // Function to close the fd held by this FileDescriptor and set fd to -1.
539 void
540 fileDescriptorClose(JNIEnv *env, jobject this)
541 {
542 FD fd = (*env)->GetLongField(env, this, IO_handle_fdID);
543 HANDLE h = (HANDLE)fd;
544 if ((*env)->ExceptionOccurred(env)) {
545 return;
546 }
547
548 if (h == INVALID_HANDLE_VALUE) {
549 return;
550 }
551
552 /* Set the fd to -1 before closing it so that the timing window
553 * of other threads using the wrong fd (closed but recycled fd,
554 * that gets re-opened with some other filename) is reduced.
555 * Practically the chance of its occurance is low, however, we are
556 * taking extra precaution over here.
557 */
|