1 /* 2 * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <fcntl.h> 29 #include <unistd.h> 30 31 #include "jni.h" 32 #include "jni_util.h" 33 #include "jvm.h" 34 #include "io_util.h" 35 #include "io_util_md.h" 36 37 #include "java_io_RandomAccessFile.h" 38 39 extern jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */ 40 extern jfieldID raf_pgsz; /* id for jobject 'fd' in java.io.RandomAccessFile */ 41 42 /********************************************************************* 43 * Platform specific implementation of input stream native methods 44 */ 45 46 JNIEXPORT void JNICALL 47 Java_java_io_RandomAccessFile_close0(JNIEnv *env, jobject this) { 48 fileClose(env, this, raf_fd); 49 } 50 51 JNIEXPORT void JNICALL 52 Java_java_io_RandomAccessFile_open0(JNIEnv *env, 53 jobject this, jstring path, jint mode) 54 { 55 int flags = 0; 56 if (mode & java_io_RandomAccessFile_O_RDONLY) { 57 flags = O_RDONLY; 58 if (mode & java_io_RandomAccessFile_O_DIRECT) 59 flags |= O_DIRECT; 60 } 61 else if (mode & java_io_RandomAccessFile_O_RDWR) { 62 flags = O_RDWR | O_CREAT; 63 if (mode & java_io_RandomAccessFile_O_SYNC) 64 flags |= O_SYNC; 65 else if (mode & java_io_RandomAccessFile_O_DSYNC) 66 flags |= O_DSYNC; 67 else if (mode & java_io_RandomAccessFile_O_DIRECT) 68 flags |= O_DIRECT; 69 } 70 fileOpen(env, this, path, raf_fd, flags); 71 } 72 73 JNIEXPORT jlong JNICALL 74 Java_java_io_RandomAccessFile_getCurrentLocation0(JNIEnv *env, jobject this) { 75 76 FD fd; 77 78 fd = GET_FD(this, raf_fd); 79 if (fd == -1) { 80 JNU_ThrowIOException(env, "Stream Closed"); 81 return -1; 82 } 83 jlong currentLocation = IO_Lseek(fd, 0, SEEK_CUR); 84 if (currentLocation == -1) { 85 JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); 86 } 87 return currentLocation; 88 } 89 90 JNIEXPORT jint JNICALL 91 Java_java_io_RandomAccessFile_getPageSize0(JNIEnv *env, jobject this) { 92 return getpagesize(); 93 } 94 95 JNIEXPORT jint JNICALL 96 Java_java_io_RandomAccessFile_readBytesD(JNIEnv *env, 97 jobject this, jbyteArray bytes, jint off, jint len) { 98 return readBytesD(env, this, bytes, off, len, raf_fd, raf_pgsz); 99 } 100 101 JNIEXPORT void JNICALL 102 Java_java_io_RandomAccessFile_writeBytesD(JNIEnv *env, 103 jobject this, jbyteArray bytes, jint off, jint len) { 104 writeBytesD(env, this, bytes, off, len, JNI_FALSE, raf_fd, raf_pgsz); 105 }