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_FileInputStream.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 * @return file descriptor
52 * @exception AFSFileException if an I/O or other file related error occurs.
54 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_openReadOnly
55 (JNIEnv *env, jobject obj, jstring fileNameUTF)
58 int fd = -1; //file descriptor
60 fd = openAFSFile(env, fileNameUTF, O_RDONLY, 0, &err);
62 fprintf(stderr, "FileInputStream::openReadOnly(): err=%d\n", err);
63 throwAFSFileException( env, err, NULL );
69 * Reads up to 'length' bytes of data from this input stream
70 * into an array of bytes. This method blocks until some input is
73 * env the Java environment
74 * obj the current Java object
75 * jbytes the data to be written
76 * offset the start offset in the data
77 * length the number of bytes that are written
79 * @return the total number of bytes read into the buffer, or
80 * <code>-1</code> if there is no more data because the end of
81 * the file has been reached.
82 * @exception AFSFileException if an I/O or other file related error occurs.
84 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_read
85 (JNIEnv *env, jobject obj, jbyteArray jbytes, jint offset, jint length)
87 int fd, bytesLen, bytesRead;
89 jmethodID getFileDescriptorID;
93 /* If we have to read 0 bytes just return */
94 if(length == 0) return 0;
96 thisClass = (*env)->GetObjectClass(env, obj);
97 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
98 fd = (*env)->GetIntField(env, obj, fid);
101 fprintf(stderr, "FileInputStream::read(): invalid file state\n");
102 throwAFSFileException(env, 0, "Invalid file state");
106 bytes = (*env) -> GetByteArrayElements(env, jbytes, 0);
107 bytesLen = (*env) -> GetArrayLength(env, jbytes);
108 bytesRead = uafs_read(fd, bytes, bytesLen);
110 if (errno != 0) throwAFSFileException(env, errno, NULL);
112 (*env) -> ReleaseByteArrayElements(env, jbytes, bytes, 0);
113 return (bytesRead > 0) ? bytesRead : -1;
117 * Closes this file input stream and releases any system resources
118 * associated with this stream. This file input stream may no longer
119 * be used for writing bytes.
121 * env the Java environment
122 * obj the current Java object
124 * @exception AFSFileException if an I/O or other file related error occurs.
126 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileInputStream_close
127 (JNIEnv *env, jobject obj)
131 jmethodID getFileDescriptorID;
135 thisClass = (*env)->GetObjectClass(env, obj);
136 fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
137 fd = (*env)->GetIntField(env, obj, fid);
140 fprintf(stderr, "FileInputStream::close(): invalid file state\n");
141 throwAFSFileException(env, 0, "Invalid file state");
148 throwAFSFileException(env, errno, NULL);