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.
23 #include "org_openafs_jafs_FileOutputStream.h"
27 #include <afs/afs_usrops.h>
34 * Be carefull with the memory management:
36 * - For every GetStringUTFChars call the corresponding ReleaseStringUTFChars.
37 * - For every Get<type>ArrayElements call the corresponding
38 * Release<type>ArrayElements
39 * - For every malloc call the corresponding free.
42 /*-------------------------------------------------------------------------*/
45 * Opens an AFS file, with the specified name, for appending.
47 * env the Java environment
48 * obj the current Java object
49 * fileNameUTF name of file to be opened
51 * @returns file descriptor
52 * @exception AFSFileException if an I/O or other file related error occurs.
54 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openWrite
55 (JNIEnv *env, jobject obj, jstring fileNameUTF)
58 jint fd = -1; //file descriptor
60 fd = openAFSFile(env, fileNameUTF, O_CREAT | O_TRUNC, 0644, &err);
62 fprintf(stderr, "FileOutputStream::openWrite(): err=%d\n", err);
63 throwAFSFileException(env, err, NULL);
69 * Opens an AFS file, with the specified name, for writing.
71 * env the Java environment
72 * obj the current Java object
73 * fileNameUTF name of file to be opened
75 * @return file descriptor
76 * @exception AFSFileException if an I/O or other file related error occurs.
78 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openAppend
79 (JNIEnv *env, jobject obj, jstring fileNameUTF)
82 jint fd = -1; //file descriptor
84 fd = openAFSFile(env, fileNameUTF, O_CREAT | O_APPEND, 0644, &err);
86 fprintf(stderr, "FileOutputStream::openAppend(): err=%d\n", err);
87 throwAFSFileException(env, err, NULL);
93 * Writes 'lenght' bytes from the specified byte array starting at offset
94 * 'offset' to this file output stream.
96 * env the Java environment
97 * obj the current Java object
98 * jbytes the data to be written
99 * offset the start offset in the data
100 * length the number of bytes that are written
102 * @exception AFSFileException if an I/O or other file related error occurs.
104 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileOutputStream_write
105 (JNIEnv *env, jobject obj, jbyteArray jbytes, jint offset, jint length)
107 int fd, written, toWrite;
110 jmethodID getFileDescriptorID;
114 thisClass = (*env)->GetObjectClass(env, obj);
115 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
116 fd = (*env)->GetIntField(env, obj, fid);
118 fprintf(stderr, "FileOutputStream::write(): failed to get file
120 throwAFSFileException(env, 0, "Failed to get file descriptor!");
122 bytes = (char*) malloc(length);
124 fprintf(stderr, "FileOutputStream::write(): malloc failed of %d bytes\n",
126 throwAFSFileException(env, 0, "Failed to allocate memory!");
128 (*env) -> GetByteArrayRegion(env, jbytes, offset, length, bytes);
132 written = uafs_write(fd, bytes, length);
136 throwAFSFileException(env, errno, NULL);
144 * Closes this file output stream and releases any system resources
145 * associated with this stream. This file output stream may no longer
146 * be used for writing bytes.
148 * env the Java environment
149 * obj the current Java object
151 * @exception AFSFileException if an I/O or other file related error occurs.
153 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileOutputStream_close
154 (JNIEnv *env, jobject obj)
158 jmethodID getFileDescriptorID;
162 thisClass = (*env)->GetObjectClass(env, obj);
163 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
164 fd = (*env)->GetIntField(env, obj, fid);
166 fprintf(stderr, "FileOutputStream::close(): failed to get file descriptor\n");
167 throwAFSFileException(env, 0, "Failed to get file descriptor!");
171 throwAFSFileException(env, rc, NULL);