1 /* 2 * Copyright (c) 1997, 2015, 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 "jni.h" 27 #include "jni_util.h" 28 #include "jlong.h" 29 #include "jvm.h" 30 31 #include "io_util.h" 32 #include "io_util_md.h" 33 #include "java_io_RandomAccessFile.h" 34 35 #include <fcntl.h> 36 37 /* 38 * static method to store field ID's in initializers 39 */ 40 41 jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */ 42 jfieldID raf_pgsz; /* id for jobject 'pageSize' in java.io.RandomAccessFile */ 43 44 JNIEXPORT void JNICALL 45 Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) { 46 raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;"); 47 raf_pgsz = (*env)->GetFieldID(env, fdClass, "pageSize", "I"); 48 } 49 50 JNIEXPORT jint JNICALL 51 Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) { 52 return readSingle(env, this, raf_fd); 53 } 54 55 JNIEXPORT jint JNICALL 56 Java_java_io_RandomAccessFile_readBytes(JNIEnv *env, 57 jobject this, jbyteArray bytes, jint off, jint len) { 58 return readBytes(env, this, bytes, off, len, raf_fd); 59 } 60 61 JNIEXPORT void JNICALL 62 Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) { 63 writeSingle(env, this, byte, JNI_FALSE, raf_fd); 64 } 65 66 JNIEXPORT void JNICALL 67 Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env, 68 jobject this, jbyteArray bytes, jint off, jint len) { 69 writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd); 70 } 71 72 JNIEXPORT jlong JNICALL 73 Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) { 74 FD fd; 75 jlong ret; 76 77 fd = GET_FD(this, raf_fd); 78 if (fd == -1) { 79 JNU_ThrowIOException(env, "Stream Closed"); 80 return -1; 81 } 82 if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) { 83 JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); 84 } 85 return ret; 86 } 87 88 JNIEXPORT jlong JNICALL 89 Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) { 90 91 FD fd; 92 jlong length = jlong_zero; 93 94 fd = GET_FD(this, raf_fd); 95 if (fd == -1) { 96 JNU_ThrowIOException(env, "Stream Closed"); 97 return -1; 98 } 99 if ((length = IO_GetLength(fd)) == -1) { 100 JNU_ThrowIOExceptionWithLastError(env, "GetLength failed"); 101 } 102 return length; 103 } 104 105 JNIEXPORT void JNICALL 106 Java_java_io_RandomAccessFile_seek0(JNIEnv *env, 107 jobject this, jlong pos) { 108 109 FD fd; 110 111 fd = GET_FD(this, raf_fd); 112 if (fd == -1) { 113 JNU_ThrowIOException(env, "Stream Closed"); 114 return; 115 } 116 if (pos < jlong_zero) { 117 JNU_ThrowIOException(env, "Negative seek offset"); 118 } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) { 119 JNU_ThrowIOExceptionWithLastError(env, "Seek failed"); 120 } 121 } 122 123 JNIEXPORT void JNICALL 124 Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this, 125 jlong newLength) 126 { 127 FD fd; 128 jlong cur; 129 130 fd = GET_FD(this, raf_fd); 131 if (fd == -1) { 132 JNU_ThrowIOException(env, "Stream Closed"); 133 return; 134 } 135 if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail; 136 if (IO_SetLength(fd, newLength) == -1) goto fail; 137 if (cur > newLength) { 138 if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail; 139 } else { 140 if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail; 141 } 142 return; 143 144 fail: 145 JNU_ThrowIOExceptionWithLastError(env, "setLength failed"); 146 }