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 <afsconfig.h>
23 #include <afs/param.h>
27 #include "org_openafs_jafs_FileOutputStream.h"
31 /*#include <afs/afs_usrops.h>*/
38 * Be carefull with the memory management:
40 * - For every GetStringUTFChars call the corresponding ReleaseStringUTFChars.
41 * - For every Get<type>ArrayElements call the corresponding
42 * Release<type>ArrayElements
43 * - For every malloc call the corresponding free.
46 /*-------------------------------------------------------------------------*/
49 * Opens an AFS file, with the specified name, for appending.
51 * env the Java environment
52 * obj the current Java object
53 * fileNameUTF name of file to be opened
55 * @returns file descriptor
56 * @exception AFSFileException if an I/O or other file related error occurs.
58 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openWrite
59 (JNIEnv
*env
, jobject obj
, jstring fileNameUTF
)
62 jint fd
= -1; //file descriptor
64 fd
= openAFSFile(env
, fileNameUTF
, O_CREAT
| O_TRUNC
, 0644, &err
);
66 fprintf(stderr
, "FileOutputStream::openWrite(): err=%d\n", err
);
67 throwAFSFileException(env
, err
, NULL
);
73 * Opens an AFS file, with the specified name, for writing.
75 * env the Java environment
76 * obj the current Java object
77 * fileNameUTF name of file to be opened
79 * @return file descriptor
80 * @exception AFSFileException if an I/O or other file related error occurs.
82 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileOutputStream_openAppend
83 (JNIEnv
*env
, jobject obj
, jstring fileNameUTF
)
86 jint fd
= -1; //file descriptor
88 fd
= openAFSFile(env
, fileNameUTF
, O_CREAT
| O_APPEND
, 0644, &err
);
90 fprintf(stderr
, "FileOutputStream::openAppend(): err=%d\n", err
);
91 throwAFSFileException(env
, err
, NULL
);
97 * Writes 'lenght' bytes from the specified byte array starting at offset
98 * 'offset' to this file output stream.
100 * env the Java environment
101 * obj the current Java object
102 * jbytes the data to be written
103 * offset the start offset in the data
104 * length the number of bytes that are written
106 * @exception AFSFileException if an I/O or other file related error occurs.
108 JNIEXPORT
void JNICALL Java_org_openafs_jafs_FileOutputStream_write
109 (JNIEnv
*env
, jobject obj
, jbyteArray jbytes
, jint offset
, jint length
)
111 int fd
, written
, toWrite
;
114 jmethodID getFileDescriptorID
;
118 thisClass
= (*env
)->GetObjectClass(env
, obj
);
119 fid
= (*env
)->GetFieldID(env
, thisClass
, "fileDescriptor", "I");
120 fd
= (*env
)->GetIntField(env
, obj
, fid
);
122 fprintf(stderr
, "FileOutputStream::write(): failed to get file "
124 throwAFSFileException(env
, 0, "Failed to get file descriptor!");
126 bytes
= malloc(length
);
128 fprintf(stderr
, "FileOutputStream::write(): malloc failed of %d bytes\n",
130 throwAFSFileException(env
, 0, "Failed to allocate memory!");
132 (*env
) -> GetByteArrayRegion(env
, jbytes
, offset
, length
, bytes
);
136 written
= uafs_write(fd
, bytes
, length
);
140 throwAFSFileException(env
, errno
, NULL
);
148 * Closes this file output stream and releases any system resources
149 * associated with this stream. This file output stream may no longer
150 * be used for writing bytes.
152 * env the Java environment
153 * obj the current Java object
155 * @exception AFSFileException if an I/O or other file related error occurs.
157 JNIEXPORT
void JNICALL Java_org_openafs_jafs_FileOutputStream_close
158 (JNIEnv
*env
, jobject obj
)
162 jmethodID getFileDescriptorID
;
166 thisClass
= (*env
)->GetObjectClass(env
, obj
);
167 fid
= (*env
)->GetFieldID(env
, thisClass
, "fileDescriptor", "I");
168 fd
= (*env
)->GetIntField(env
, obj
, fid
);
170 fprintf(stderr
, "FileOutputStream::close(): failed to get file descriptor\n");
171 throwAFSFileException(env
, 0, "Failed to get file descriptor!");
175 throwAFSFileException(env
, rc
, NULL
);