1 /*
   2  * Copyright (c) 1997, 2010, 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 "jvm.h"
  29 
  30 #include "io_util.h"
  31 #include "io_util_md.h"
  32 #include "java_io_FileOutputStream.h"
  33 
  34 #include <fcntl.h>
  35 
  36 /*******************************************************************/
  37 /*  BEGIN JNI ********* BEGIN JNI *********** BEGIN JNI ************/
  38 /*******************************************************************/
  39 
  40 jfieldID fos_fd; /* id for jobject 'fd' in java.io.FileOutputStream */
  41 jfieldID fos_pgsz; /* id for jobject 'pageSize' in java.io.FileOutputStream */
  42 
  43 /**************************************************************
  44  * static methods to store field ID's in initializers
  45  */
  46 
  47 JNIEXPORT void JNICALL
  48 Java_java_io_FileOutputStream_initIDs(JNIEnv *env, jclass fdClass) {
  49     fos_fd = (*env)->GetFieldID(env, fdClass, "fd", "Ljava/io/FileDescriptor;");
  50     fos_pgsz = (*env)->GetFieldID(env, fdClass, "pageSize", "I");
  51 }
  52 
  53 /**************************************************************
  54  * Output stream
  55  */
  56 
  57 JNIEXPORT void JNICALL
  58 Java_java_io_FileOutputStream_open0(JNIEnv *env, jobject this,
  59                                     jstring path, jboolean append, jboolean direct) {
  60     if (direct) { 
  61         fileOpen(env, this, path, fos_fd,
  62              O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC) | O_DIRECT);
  63     } else {
  64         fileOpen(env, this, path, fos_fd,
  65              O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC));
  66     }
  67 }
  68 
  69 JNIEXPORT void JNICALL
  70 Java_java_io_FileOutputStream_write(JNIEnv *env, jobject this, jint byte, jboolean append) {
  71     writeSingle(env, this, byte, append, fos_fd);
  72 }
  73 
  74 JNIEXPORT void JNICALL
  75 Java_java_io_FileOutputStream_writeBytes(JNIEnv *env,
  76     jobject this, jbyteArray bytes, jint off, jint len, jboolean append) {
  77     writeBytes(env, this, bytes, off, len, append, fos_fd);
  78 }
  79 
  80 JNIEXPORT void JNICALL
  81 Java_java_io_FileOutputStream_writeBytesD(JNIEnv *env,
  82     jobject this, jbyteArray bytes, jint off, jint len, jboolean append) {
  83     writeBytesD(env, this, bytes, off, len, append, fos_fd, fos_pgsz);
  84 }
  85 
  86 JNIEXPORT jint JNICALL
  87 Java_java_io_FileOutputStream_getPageSize0(JNIEnv *env, jobject this) {
  88     return getpagesize();
  89 }
  90 
  91 JNIEXPORT void JNICALL
  92 Java_java_io_FileOutputStream_close0(JNIEnv *env, jobject this) {
  93     fileClose(env, this, fos_fd);
  94 }
  95 
  96 JNIEXPORT jlong JNICALL
  97 Java_java_io_FileOutputStream_getCurrentLocation0(JNIEnv *env, jobject this) {
  98     FD fd;
  99 
 100     fd = GET_FD(this, fos_fd);
 101     if (fd == -1) {
 102         JNU_ThrowIOException(env, "Stream Closed");
 103         return -1;
 104     }
 105     jlong currentLocation = IO_Lseek(fd, 0, SEEK_CUR);
 106     if (currentLocation == -1) {
 107         JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
 108     }
 109     return currentLocation;
 110 }
 111