1 /*
   2  * Copyright (c) 1997, 2013, 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 
  43 JNIEXPORT void JNICALL
  44 Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {
  45     raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
  46 }
  47 
  48 
  49 JNIEXPORT void JNICALL
  50 Java_java_io_RandomAccessFile_open(JNIEnv *env,
  51                                    jobject this, jstring path, jint mode)
  52 {
  53     int flags = 0;
  54     if (mode & java_io_RandomAccessFile_O_RDONLY)
  55         flags = O_RDONLY;
  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     fileOpen(env, this, path, raf_fd, flags);
  64 }
  65 
  66 JNIEXPORT jint JNICALL
  67 Java_java_io_RandomAccessFile_read(JNIEnv *env, jobject this) {
  68     return readSingle(env, this, raf_fd);
  69 }
  70 
  71 JNIEXPORT jint JNICALL
  72 Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,
  73     jobject this, jbyteArray bytes, jint off, jint len) {
  74     return readBytes(env, this, bytes, off, len, raf_fd);
  75 }
  76 
  77 JNIEXPORT void JNICALL
  78 Java_java_io_RandomAccessFile_write(JNIEnv *env, jobject this, jint byte) {
  79     writeSingle(env, this, byte, JNI_FALSE, raf_fd);
  80 }
  81 
  82 JNIEXPORT void JNICALL
  83 Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
  84     jobject this, jbyteArray bytes, jint off, jint len) {
  85     writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);
  86 }
  87 
  88 JNIEXPORT jlong JNICALL
  89 Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
  90     FD fd;
  91     jlong ret;
  92 
  93     fd = GET_FD(this, raf_fd);
  94     if (fd == -1) {
  95         JNU_ThrowIOException(env, "Stream Closed");
  96         return -1;
  97     }
  98     if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
  99         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
 100     }
 101     return ret;
 102 }
 103 
 104 JNIEXPORT jlong JNICALL
 105 Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
 106     FD fd;
 107     jlong cur = jlong_zero;
 108     jlong end = jlong_zero;
 109 
 110     fd = GET_FD(this, raf_fd);
 111     if (fd == -1) {
 112         JNU_ThrowIOException(env, "Stream Closed");
 113         return -1;
 114     }
 115     if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
 116         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
 117     } else if ((end = IO_Lseek(fd, 0L, SEEK_END)) == -1) {
 118         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
 119     } else if (IO_Lseek(fd, cur, SEEK_SET) == -1) {
 120         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
 121     }
 122     return end;
 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 = GET_FD(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 = GET_FD(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");
 166 }