RandomAccessFile.c revision 0
342N/A/*
579N/A * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
342N/A *
342N/A * This code is free software; you can redistribute it and/or modify it
342N/A * under the terms of the GNU General Public License version 2 only, as
342N/A * published by the Free Software Foundation. Sun designates this
342N/A * particular file as subject to the "Classpath" exception as provided
342N/A * by Sun in the LICENSE file that accompanied this code.
342N/A *
342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
342N/A * version 2 for more details (a copy is included in the LICENSE file that
342N/A * accompanied this code).
342N/A *
342N/A * You should have received a copy of the GNU General Public License version
342N/A * 2 along with this work; if not, write to the Free Software Foundation,
342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
342N/A *
342N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
342N/A * CA 95054 USA or visit www.sun.com if you need additional information or
342N/A * have any questions.
342N/A */
342N/A
342N/A#include "jni.h"
342N/A#include "jni_util.h"
342N/A#include "jlong.h"
342N/A#include "jvm.h"
342N/A
342N/A#include "io_util.h"
342N/A#include "io_util_md.h"
342N/A#include "java_io_RandomAccessFile.h"
342N/A
342N/A#include <fcntl.h>
342N/A
342N/A/*
342N/A * static method to store field ID's in initializers
342N/A */
342N/A
342N/AjfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_initIDs(JNIEnv *env, jclass fdClass) {
342N/A raf_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
342N/A}
342N/A
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_open(JNIEnv *env,
342N/A jobject this, jstring path, jint mode)
342N/A{
342N/A int flags = 0;
342N/A if (mode & java_io_RandomAccessFile_O_RDONLY)
342N/A flags = O_RDONLY;
342N/A else if (mode & java_io_RandomAccessFile_O_RDWR) {
342N/A flags = O_RDWR | O_CREAT;
342N/A if (mode & java_io_RandomAccessFile_O_SYNC)
342N/A flags |= O_SYNC;
342N/A else if (mode & java_io_RandomAccessFile_O_DSYNC)
342N/A flags |= O_DSYNC;
342N/A }
342N/A fileOpen(env, this, path, raf_fd, flags);
342N/A}
342N/A
342N/AJNIEXPORT jint JNICALL
342N/AJava_java_io_RandomAccessFile_read(JNIEnv *env, jobject this) {
342N/A return readSingle(env, this, raf_fd);
342N/A}
342N/A
342N/AJNIEXPORT jint JNICALL
342N/AJava_java_io_RandomAccessFile_readBytes(JNIEnv *env,
342N/A jobject this, jbyteArray bytes, jint off, jint len) {
342N/A return readBytes(env, this, bytes, off, len, raf_fd);
342N/A}
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_write(JNIEnv *env, jobject this, jint byte) {
342N/A writeSingle(env, this, byte, raf_fd);
342N/A}
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_writeBytes(JNIEnv *env,
342N/A jobject this, jbyteArray bytes, jint off, jint len) {
342N/A writeBytes(env, this, bytes, off, len, raf_fd);
342N/A}
342N/A
342N/AJNIEXPORT jlong JNICALL
342N/AJava_java_io_RandomAccessFile_getFilePointer(JNIEnv *env, jobject this) {
342N/A FD fd;
342N/A jlong ret;
342N/A
342N/A fd = GET_FD(this, raf_fd);
677N/A if (fd == -1) {
677N/A JNU_ThrowIOException(env, "Stream Closed");
342N/A return -1;
890N/A }
342N/A if ((ret = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
890N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
890N/A }
890N/A return ret;
890N/A}
890N/A
890N/AJNIEXPORT jlong JNICALL
342N/AJava_java_io_RandomAccessFile_length(JNIEnv *env, jobject this) {
342N/A FD fd;
342N/A jlong cur = jlong_zero;
342N/A jlong end = jlong_zero;
342N/A
342N/A fd = GET_FD(this, raf_fd);
342N/A if (fd == -1) {
342N/A JNU_ThrowIOException(env, "Stream Closed");
342N/A return -1;
342N/A }
342N/A if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) {
342N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
342N/A } else if ((end = IO_Lseek(fd, 0L, SEEK_END)) == -1) {
342N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
342N/A } else if (IO_Lseek(fd, cur, SEEK_SET) == -1) {
342N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
342N/A }
342N/A return end;
342N/A}
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_seek(JNIEnv *env,
342N/A jobject this, jlong pos) {
342N/A
342N/A FD fd;
342N/A
342N/A fd = GET_FD(this, raf_fd);
342N/A if (fd == -1) {
342N/A JNU_ThrowIOException(env, "Stream Closed");
342N/A return;
342N/A }
342N/A if (pos < jlong_zero) {
342N/A JNU_ThrowIOException(env, "Negative seek offset");
342N/A } else if (IO_Lseek(fd, pos, SEEK_SET) == -1) {
342N/A JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
342N/A }
342N/A}
342N/A
342N/AJNIEXPORT void JNICALL
342N/AJava_java_io_RandomAccessFile_setLength(JNIEnv *env, jobject this,
342N/A jlong newLength)
751N/A{
342N/A FD fd;
342N/A jlong cur;
342N/A
342N/A fd = GET_FD(this, raf_fd);
342N/A if (fd == -1) {
342N/A JNU_ThrowIOException(env, "Stream Closed");
342N/A return;
342N/A }
342N/A if ((cur = IO_Lseek(fd, 0L, SEEK_CUR)) == -1) goto fail;
342N/A if (IO_SetLength(fd, newLength) == -1) goto fail;
342N/A if (cur > newLength) {
342N/A if (IO_Lseek(fd, 0L, SEEK_END) == -1) goto fail;
342N/A } else {
342N/A if (IO_Lseek(fd, cur, SEEK_SET) == -1) goto fail;
342N/A }
342N/A return;
342N/A
342N/A fail:
342N/A JNU_ThrowIOExceptionWithLastError(env, "setLength failed");
342N/A}
342N/A