Path: blob/master/src/java.base/share/native/libjava/RandomAccessFile.c
41149 views
/*1* Copyright (c) 1997, 2020, 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}62#ifdef WIN3263if (mode & java_io_RandomAccessFile_O_TEMPORARY)64flags |= O_TEMPORARY;65#endif66fileOpen(env, this, path, raf_fd, flags);67}6869JNIEXPORT jint JNICALL70Java_java_io_RandomAccessFile_read0(JNIEnv *env, jobject this) {71return readSingle(env, this, raf_fd);72}7374JNIEXPORT jint JNICALL75Java_java_io_RandomAccessFile_readBytes(JNIEnv *env,76jobject this, jbyteArray bytes, jint off, jint len) {77return readBytes(env, this, bytes, off, len, raf_fd);78}7980JNIEXPORT void JNICALL81Java_java_io_RandomAccessFile_write0(JNIEnv *env, jobject this, jint byte) {82writeSingle(env, this, byte, JNI_FALSE, raf_fd);83}8485JNIEXPORT void JNICALL86Java_java_io_RandomAccessFile_writeBytes(JNIEnv *env,87jobject this, jbyteArray bytes, jint off, jint len) {88writeBytes(env, this, bytes, off, len, JNI_FALSE, raf_fd);89}9091JNIEXPORT jlong JNICALL92Java_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {93FD fd;94jlong ret;9596fd = getFD(env, this, raf_fd);97if (fd == -1) {98JNU_ThrowIOException(env, "Stream Closed");99return -1;100}101if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {102JNU_ThrowIOExceptionWithLastError(env, "Seek failed");103}104return ret;105}106107JNIEXPORT jlong JNICALL108Java_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {109110FD fd;111jlong length = jlong_zero;112113fd = getFD(env, this, raf_fd);114if (fd == -1) {115JNU_ThrowIOException(env, "Stream Closed");116return -1;117}118if ((length = IO_GetLength(fd)) == -1) {119JNU_ThrowIOExceptionWithLastError(env, "GetLength failed");120}121return length;122}123124JNIEXPORT void JNICALL125Java_java_io_RandomAccessFile_seek0(JNIEnv *env,126jobject this, jlong pos) {127128FD fd;129130fd = getFD(env, 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 = getFD(env, 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