2 * Copyright (c) 2001-2002 International Business Machines Corp.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
9 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
13 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
14 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
15 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
16 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
17 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 #include <afs/param.h>
26 #include "org_openafs_jafs_FileOutputStream.h"
30 /*#include <afs/afs_usrops.h>*/
37 * Be carefull with the memory management:
39 * - For every GetStringUTFChars call the corresponding ReleaseStringUTFChars.
40 * - For every Get<type>ArrayElements call the corresponding
41 * Release<type>ArrayElements
42 * - For every malloc call the corresponding free.
45 /*-------------------------------------------------------------------------*/
48 * Opens an AFS file, with the specified name, for appending.
50 * env the Java environment
51 * obj the current Java object
52 * fileNameUTF name of file to be opened
54 * @returns file descriptor
55 * @exception AFSFileException if an I/O or other file related error occurs.
57 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openWrite
58 (JNIEnv *env, jobject obj, jstring fileNameUTF)
61 jint fd = -1; //file descriptor
63 fd = openAFSFile(env, fileNameUTF, O_CREAT | O_TRUNC, 0644, &err);
65 fprintf(stderr, "FileOutputStream::openWrite(): err=%d\n", err);
66 throwAFSFileException(env, err, NULL);
72 * Opens an AFS file, with the specified name, for writing.
74 * env the Java environment
75 * obj the current Java object
76 * fileNameUTF name of file to be opened
78 * @return file descriptor
79 * @exception AFSFileException if an I/O or other file related error occurs.
81 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openAppend
82 (JNIEnv *env, jobject obj, jstring fileNameUTF)
85 jint fd = -1; //file descriptor
87 fd = openAFSFile(env, fileNameUTF, O_CREAT | O_APPEND, 0644, &err);
89 fprintf(stderr, "FileOutputStream::openAppend(): err=%d\n", err);
90 throwAFSFileException(env, err, NULL);
96 * Writes 'lenght' bytes from the specified byte array starting at offset
97 * 'offset' to this file output stream.
99 * env the Java environment
100 * obj the current Java object
101 * jbytes the data to be written
102 * offset the start offset in the data
103 * length the number of bytes that are written
105 * @exception AFSFileException if an I/O or other file related error occurs.
107 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileOutputStream_write
108 (JNIEnv *env, jobject obj, jbyteArray jbytes, jint offset, jint length)
110 int fd, written, toWrite;
113 jmethodID getFileDescriptorID;
117 thisClass = (*env)->GetObjectClass(env, obj);
118 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
119 fd = (*env)->GetIntField(env, obj, fid);
121 fprintf(stderr, "FileOutputStream::write(): failed to get file "
123 throwAFSFileException(env, 0, "Failed to get file descriptor!");
125 bytes = (char*) malloc(length);
127 fprintf(stderr, "FileOutputStream::write(): malloc failed of %d bytes\n",
129 throwAFSFileException(env, 0, "Failed to allocate memory!");
131 (*env) -> GetByteArrayRegion(env, jbytes, offset, length, bytes);
135 written = uafs_write(fd, bytes, length);
139 throwAFSFileException(env, errno, NULL);
147 * Closes this file output stream and releases any system resources
148 * associated with this stream. This file output stream may no longer
149 * be used for writing bytes.
151 * env the Java environment
152 * obj the current Java object
154 * @exception AFSFileException if an I/O or other file related error occurs.
156 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileOutputStream_close
157 (JNIEnv *env, jobject obj)
161 jmethodID getFileDescriptorID;
165 thisClass = (*env)->GetObjectClass(env, obj);
166 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
167 fd = (*env)->GetIntField(env, obj, fid);
169 fprintf(stderr, "FileOutputStream::close(): failed to get file descriptor\n");
170 throwAFSFileException(env, 0, "Failed to get file descriptor!");
174 throwAFSFileException(env, rc, NULL);