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"
33 * Be carefull with the memory management:
35 * - For every GetStringUTFChars call the corresponding ReleaseStringUTFChars.
36 * - For every Get<type>ArrayElements call the corresponding
37 * Release<type>ArrayElements
38 * - For every malloc call the corresponding free.
41 /*-------------------------------------------------------------------------*/
44 * Opens an AFS file, with the specified name, for appending.
46 * env the Java environment
47 * obj the current Java object
48 * fileNameUTF name of file to be opened
50 * @return file descriptor
51 * @exception AFSFileException if an I/O or other file related error occurs.
53 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_openReadOnly
54 (JNIEnv
*env
, jobject obj
, jstring fileNameUTF
)
57 int fd
= -1; //file descriptor
59 fd
= openAFSFile(env
, fileNameUTF
, O_RDONLY
, 0, &err
);
61 fprintf(stderr
, "FileInputStream::openReadOnly(): err=%d\n", err
);
62 throwAFSFileException( env
, err
, NULL
);
68 * Reads up to 'length' bytes of data from this input stream
69 * into an array of bytes. This method blocks until some input is
72 * env the Java environment
73 * obj the current Java object
74 * jbytes the data to be written
75 * offset the start offset in the data
76 * length the number of bytes that are written
78 * @return the total number of bytes read into the buffer, or
79 * <code>-1</code> if there is no more data because the end of
80 * the file has been reached.
81 * @exception AFSFileException if an I/O or other file related error occurs.
83 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_read
84 (JNIEnv
*env
, jobject obj
, jbyteArray jbytes
, jint offset
, jint length
)
86 int fd
, bytesLen
, bytesRead
;
88 jmethodID getFileDescriptorID
;
92 /* If we have to read 0 bytes just return */
93 if(length
== 0) return 0;
95 thisClass
= (*env
)->GetObjectClass(env
, obj
);
96 fid
= (*env
)->GetFieldID(env
, thisClass
, "fileDescriptor", "I");
97 fd
= (*env
)->GetIntField(env
, obj
, fid
);
100 fprintf(stderr
, "FileInputStream::read(): invalid file state\n");
101 throwAFSFileException(env
, 0, "Invalid file state");
105 bytes
= (*env
) -> GetByteArrayElements(env
, jbytes
, 0);
106 bytesLen
= (*env
) -> GetArrayLength(env
, jbytes
);
107 bytesRead
= uafs_read(fd
, bytes
, bytesLen
);
109 if (errno
!= 0) throwAFSFileException(env
, errno
, NULL
);
111 (*env
) -> ReleaseByteArrayElements(env
, jbytes
, bytes
, 0);
112 return (bytesRead
> 0) ? bytesRead
: -1;
116 * Closes this file input stream and releases any system resources
117 * associated with this stream. This file input stream may no longer
118 * be used for writing bytes.
120 * env the Java environment
121 * obj the current Java object
123 * @exception AFSFileException if an I/O or other file related error occurs.
125 JNIEXPORT
void JNICALL Java_org_openafs_jafs_FileInputStream_close
126 (JNIEnv
*env
, jobject obj
)
130 jmethodID getFileDescriptorID
;
134 thisClass
= (*env
)->GetObjectClass(env
, obj
);
135 fid
= (*env
)->GetFieldID(env
, thisClass
, "fileDescriptor", "I");
136 fd
= (*env
)->GetIntField(env
, obj
, fid
);
139 fprintf(stderr
, "FileInputStream::close(): invalid file state\n");
140 throwAFSFileException(env
, 0, "Invalid file state");
147 throwAFSFileException(env
, errno
, NULL
);