< prev index next >

src/java.base/share/native/libjava/RandomAccessFile.c

Print this page

 56     else if (mode & java_io_RandomAccessFile_O_RDWR) {
 57         flags = O_RDWR | O_CREAT;
 58         if (mode & java_io_RandomAccessFile_O_SYNC)
 59             flags |= O_SYNC;
 60         else if (mode & java_io_RandomAccessFile_O_DSYNC)
 61             flags |= O_DSYNC;
 62     }
 63 #ifdef WIN32
 64     if (mode & java_io_RandomAccessFile_O_TEMPORARY)
 65         flags |= O_TEMPORARY;
 66 #endif
 67     fileOpen(env, this, path, raf_fd, flags);
 68 }
 69 
 70 JNIEXPORT jint JNICALL
 71 Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {
 72     return readSingle(env, this, raf_fd);
 73 }
 74 
 75 JNIEXPORT jint JNICALL
 76 Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
 77     jobject this, jbyteArray bytes, jint off, jint len) {
 78     return readBytes(env, this, bytes, off, len, raf_fd);
 79 }
 80 
 81 JNIEXPORT void JNICALL
 82 Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {
 83     writeSingle(env, this, byte, JNI_FALSE, raf_fd);
 84 }
 85 
 86 JNIEXPORT void JNICALL
 87 Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
 88     jobject this, jbyteArray bytes, jint off, jint len) {
 89     writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
 90 }
 91 
 92 JNIEXPORT jlong JNICALL
 93 Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
 94     FD fd;
 95     jlong ret;
 96 
 97     fd = getFD(env, this, raf_fd);
 98     if (fd == -1) {
 99         JNU_ThrowIOException(env, "Stream Closed");
100         return -1;
101     }
102     if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
103         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
104     }
105     return ret;
106 }
107 
108 JNIEXPORT jlong JNICALL
109 Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
110 
111     FD fd;
112     jlong length = jlong_zero;
113 
114     fd = getFD(env, this, raf_fd);
115     if (fd == -1) {
116         JNU_ThrowIOException(env, "Stream Closed");
117         return -1;
118     }
119     if ((length = IO_GetLength(fd)) == -1) {
120         JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
121     }
122     return length;
123 }
124 
125 JNIEXPORT void JNICALL
126 Java_java_io_RandomAccessFile_seek0(JNIEnv *env,
127                     jobject this, jlong pos) {
128 
129     FD fd;
130 
131     fd = getFD(env, this, raf_fd);
132     if (fd == -1) {
133         JNU_ThrowIOException(env, "Stream Closed");
134         return;
135     }
136     if (pos < jlong_zero) {
137         JNU_ThrowIOException(env, "Negative seek offset");
138     } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
139         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
140     }
141 }
142 
143 JNIEXPORT void JNICALL
144 Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,
145                                         jlong newLength)
146 {
147     FD fd;
148     jlong cur;
149 
150     fd = getFD(env, this, raf_fd);
151     if (fd == -1) {
152         JNU_ThrowIOException(env, "Stream Closed");
153         return;
154     }
155     if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
156     if (IO_SetLength(fd, newLength) == -1) goto fail;
157     if (cur > newLength) {
158         if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
159     } else {
160         if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
161     }
162     return;
163 
164  fail:
165     JNU_ThrowIOExceptionWithLastError(env, "setLength failed");

 56     else if (mode & java_io_RandomAccessFile_O_RDWR) {
 57         flags = O_RDWR | O_CREAT;
 58         if (mode & java_io_RandomAccessFile_O_SYNC)
 59             flags |= O_SYNC;
 60         else if (mode & java_io_RandomAccessFile_O_DSYNC)
 61             flags |= O_DSYNC;
 62     }
 63 #ifdef WIN32
 64     if (mode & java_io_RandomAccessFile_O_TEMPORARY)
 65         flags |= O_TEMPORARY;
 66 #endif
 67     fileOpen(env, this, path, raf_fd, flags);
 68 }
 69 
 70 JNIEXPORT jint JNICALL
 71 Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {
 72     return readSingle(env, this, raf_fd);
 73 }
 74 
 75 JNIEXPORT jint JNICALL
 76 Java_java_io_RandomAccessFile_readBytes0(JNIEnv *env,
 77     jobject this, jbyteArray bytes, jint off, jint len) {
 78     return readBytes(env, this, bytes, off, len, raf_fd);
 79 }
 80 
 81 JNIEXPORT void JNICALL
 82 Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {
 83     writeSingle(env, this, byte, JNI_FALSE, raf_fd);
 84 }
 85 
 86 JNIEXPORT void JNICALL
 87 Java_java_io_RandomAccessFile_writeBytes0(JNIEnv *env,
 88     jobject this, jbyteArray bytes, jint off, jint len) {
 89     writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
 90 }
 91 
 92 JNIEXPORT jlong JNICALL
 93 Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
 94     FD fd;
 95     jlong ret;
 96 
 97     fd = getFD(env, this, raf_fd);
 98     if (fd == -1) {
 99         JNU_ThrowIOException(env, "Stream Closed");
100         return -1;
101     }
102     if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
103         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
104     }
105     return ret;
106 }
107 
108 JNIEXPORT jlong JNICALL
109 Java_java_io_RandomAccessFile_length0(JNIEnv *env, jobject this) {
110 
111     FD fd;
112     jlong length = jlong_zero;
113 
114     fd = getFD(env, this, raf_fd);
115     if (fd == -1) {
116         JNU_ThrowIOException(env, "Stream Closed");
117         return -1;
118     }
119     if ((length = IO_GetLength(fd)) == -1) {
120         JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");
121     }
122     return length;
123 }
124 
125 JNIEXPORT void JNICALL
126 Java_java_io_RandomAccessFile_seek0(JNIEnv *env,
127                     jobject this, jlong pos) {
128 
129     FD fd;
130 
131     fd = getFD(env, this, raf_fd);
132     if (fd == -1) {
133         JNU_ThrowIOException(env, "Stream Closed");
134         return;
135     }
136     if (pos < jlong_zero) {
137         JNU_ThrowIOException(env, "Negative seek offset");
138     } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
139         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
140     }
141 }
142 
143 JNIEXPORT void JNICALL
144 Java_java_io_RandomAccessFile_setLength0(JNIEnv *env, jobject this,
145                                          jlong newLength)
146 {
147     FD fd;
148     jlong cur;
149 
150     fd = getFD(env, this, raf_fd);
151     if (fd == -1) {
152         JNU_ThrowIOException(env, "Stream Closed");
153         return;
154     }
155     if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
156     if (IO_SetLength(fd, newLength) == -1) goto fail;
157     if (cur > newLength) {
158         if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
159     } else {
160         if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
161     }
162     return;
163 
164  fail:
165     JNU_ThrowIOExceptionWithLastError(env, "setLength failed");
< prev index next >