Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/java/io/RandomAccessFile.c
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"27#include "jlong.h"28#include "jvm.h"2930#include "io_util.h"31#include "io_util_md.h"32#include "java_io_RandomAccessFile.h"3334#include <fcntl.h>3536/*37* static method to store field ID's in initializers38*/3940jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */4142JNIEXPORT void JNICALL43Java_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {44raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");45}464748JNIEXPORT void JNICALL49Java_java_io_RandomAccessFile_open0(JNIEnv *env,50jobject this, jstring path, jint mode)51{52int flags = 0;53if (mode & java_io_RandomAccessFile_O_RDONLY)54flags = O_RDONLY;55else if (mode & java_io_RandomAccessFile_O_RDWR) {56flags = O_RDWR | O_CREAT;57if (mode & java_io_RandomAccessFile_O_SYNC)58flags |= O_SYNC;59else if (mode & java_io_RandomAccessFile_O_DSYNC)60flags |= O_DSYNC;61}62fileOpen(env, this, path, raf_fd, flags);63}6465JNIEXPORT jint JNICALL66Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {67return readSingle(env, this, raf_fd);68}6970JNIEXPORT jint JNICALL71Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,72jobject this, jbyteArray bytes, jint off, jint len) {73return readBytes(env, this, bytes, off, len, raf_fd);74}7576JNIEXPORT void JNICALL77Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {78writeSingle(env, this, byte, JNI_FALSE, raf_fd);79}8081JNIEXPORT void JNICALL82Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,83jobject this, jbyteArray bytes, jint off, jint len) {84writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);85}8687JNIEXPORT jlong JNICALL88Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {89FD fd;90jlong ret;9192fd = GET_FD(this, raf_fd);93if (fd == -1) {94JNU_ThrowIOException(env, "Stream Closed");95return -1;96}97if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {98JNU_ThrowIOExceptionWithLastError(env, "Seek failed");99}100return ret;101}102103JNIEXPORT jlong JNICALL104Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {105FD fd;106jlong cur = jlong_zero;107jlong end = jlong_zero;108109fd = GET_FD(this, raf_fd);110if (fd == -1) {111JNU_ThrowIOException(env, "Stream Closed");112return -1;113}114if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {115JNU_ThrowIOExceptionWithLastError(env, "Seek failed");116} else if ((end = IO_Lseek(fd, 0L, SEEK_END)) == -1) {117JNU_ThrowIOExceptionWithLastError(env, "Seek failed");118} else if (IO_Lseek(fd, cur, SEEK_SET) == -1) {119JNU_ThrowIOExceptionWithLastError(env, "Seek failed");120}121return end;122}123124JNIEXPORT void JNICALL125Java_java_io_RandomAccessFile_seek0(JNIEnv *env,126jobject this, jlong pos) {127128FD fd;129130fd = GET_FD(this, raf_fd);131if (fd == -1) {132JNU_ThrowIOException(env, "Stream Closed");133return;134}135if (pos < jlong_zero) {136JNU_ThrowIOException(env, "Negative seek offset");137} else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {138JNU_ThrowIOExceptionWithLastError(env, "Seek failed");139}140}141142JNIEXPORT void JNICALL143Java_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,144jlong newLength)145{146FD fd;147jlong cur;148149fd = GET_FD(this, raf_fd);150if (fd == -1) {151JNU_ThrowIOException(env, "Stream Closed");152return;153}154if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;155if (IO_SetLength(fd, newLength) == -1) goto fail;156if (cur > newLength) {157if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;158} else {159if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;160}161return;162163fail:164JNU_ThrowIOExceptionWithLastError(env, "setLength failed");165}166167168