FileInputStream.c revision 0
3349N/A/*
3349N/A * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
3349N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3349N/A *
3349N/A * This code is free software; you can redistribute it and/or modify it
3349N/A * under the terms of the GNU General Public License version 2 only, as
3349N/A * published by the Free Software Foundation. Sun designates this
3349N/A * particular file as subject to the "Classpath" exception as provided
3349N/A * by Sun in the LICENSE file that accompanied this code.
3349N/A *
3349N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3349N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3349N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3349N/A * version 2 for more details (a copy is included in the LICENSE file that
3349N/A * accompanied this code).
3349N/A *
3349N/A * You should have received a copy of the GNU General Public License version
3349N/A * 2 along with this work; if not, write to the Free Software Foundation,
3349N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3349N/A *
3349N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3349N/A * CA 95054 USA or visit www.sun.com if you need additional information or
3349N/A * have any questions.
3349N/A */
3349N/A
3349N/A#include "jni.h"
3349N/A#include "jni_util.h"
3349N/A#include "jlong.h"
3349N/A#include "io_util.h"
3349N/A
3349N/A#include "jvm.h"
3349N/A
3349N/A#include "java_io_FileInputStream.h"
3349N/A
3349N/A#include <fcntl.h>
3349N/A#include <limits.h>
3349N/A
3349N/A#include "io_util_md.h"
3349N/A
3349N/A/*******************************************************************/
3349N/A/* BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
3349N/A/*******************************************************************/
3349N/A
3349N/AjfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
3349N/A
3349N/A/**************************************************************
3349N/A * static methods to store field ID's in initializers
3349N/A */
3349N/A
3349N/AJNIEXPORT void JNICALL
3349N/AJava_java_io_FileInputStream_initIDs(JNIEnv *env, jclass fdClass) {
3349N/A fis_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
3349N/A}
3349N/A
3349N/A/**************************************************************
3349N/A * Input stream
3349N/A */
3349N/A
3349N/AJNIEXPORT void JNICALL
3349N/AJava_java_io_FileInputStream_open(JNIEnv *env, jobject this, jstring path) {
3349N/A fileOpen(env, this, path, fis_fd, O_RDONLY);
3349N/A}
3349N/A
3349N/AJNIEXPORT jint JNICALL
3349N/AJava_java_io_FileInputStream_read(JNIEnv *env, jobject this) {
3349N/A return readSingle(env, this, fis_fd);
3349N/A}
3349N/A
3349N/AJNIEXPORT jint JNICALL
3349N/AJava_java_io_FileInputStream_readBytes(JNIEnv *env, jobject this,
3349N/A jbyteArray bytes, jint off, jint len) {
3349N/A return readBytes(env, this, bytes, off, len, fis_fd);
3349N/A}
3349N/A
3349N/AJNIEXPORT jlong JNICALL
3349N/AJava_java_io_FileInputStream_skip(JNIEnv *env, jobject this, jlong toSkip) {
3349N/A jlong cur = jlong_zero;
3349N/A jlong end = jlong_zero;
3349N/A FD fd = GET_FD(this, fis_fd);
3349N/A if (fd == -1) {
3349N/A JNU_ThrowIOException (env, "Stream Closed");
3349N/A return 0;
3349N/A }
3349N/A if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
3349N/A JNU_ThrowIOExceptionWithLastError(env, "Seek error");
3349N/A } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
3349N/A JNU_ThrowIOExceptionWithLastError(env, "Seek error");
3349N/A }
3349N/A return (end - cur);
3349N/A}
3349N/A
3349N/AJNIEXPORT jint JNICALL
3349N/AJava_java_io_FileInputStream_available(JNIEnv *env, jobject this) {
3349N/A jlong ret;
3349N/A FD fd = GET_FD(this, fis_fd);
3349N/A if (fd == -1) {
3349N/A JNU_ThrowIOException (env, "Stream Closed");
3349N/A return 0;
3349N/A }
3349N/A if (IO_Available(fd, &ret)) {
3349N/A if (ret > INT_MAX) {
3349N/A ret = (jlong) INT_MAX;
3349N/A }
3349N/A return jlong_to_jint(ret);
3349N/A }
3349N/A JNU_ThrowIOExceptionWithLastError(env, NULL);
3349N/A return 0;
3349N/A}
3349N/A