java-errno-header-20090526
[openafs.git] / src / JAVA / libjafs / FileInputStream.c
1 /*
2  * Copyright (c) 2001-2002 International Business Machines Corp.
3  * All rights reserved.
4  *
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
8  * 
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.
20  */
21
22 #include "Internal.h"
23 #include "org_openafs_jafs_FileInputStream.h"
24 #include <errno.h>
25
26 #include <fcntl.h>
27
28 #ifdef DMALLOC
29 #include "dmalloc.h"
30 #endif
31
32 /**
33  * Be carefull with the memory management:
34  *
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.
39  */
40
41 /*-------------------------------------------------------------------------*/
42
43 /**
44  * Opens an AFS file, with the specified name, for appending.
45  * 
46  *  env         the Java environment
47  *  obj         the current Java object
48  *  fileNameUTF name of file to be opened
49  *
50  * @return              file descriptor
51  * @exception   AFSFileException  if an I/O or other file related error occurs.
52  */
53 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_openReadOnly
54   (JNIEnv *env, jobject obj, jstring fileNameUTF)
55 {
56   int err;
57   int fd = -1;          //file descriptor
58
59   fd = openAFSFile(env, fileNameUTF, O_RDONLY, 0, &err);
60   if (fd < 0) {
61     fprintf(stderr, "FileInputStream::openReadOnly(): err=%d\n", err);
62     throwAFSFileException( env, err, NULL );
63   }
64   return fd;
65 }
66
67 /**
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
70  * available.
71  * 
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
77  *
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.
82  */
83 JNIEXPORT jint JNICALL Java_org_openafs_jafs_FileInputStream_read
84   (JNIEnv *env, jobject obj, jbyteArray jbytes, jint offset, jint length)
85 {
86   int fd, bytesLen, bytesRead;
87   jclass thisClass;
88   jmethodID getFileDescriptorID;
89   jbyte *bytes;
90   jfieldID fid;
91
92   /* If we have to read 0 bytes just return */
93   if(length == 0) return 0;
94
95   thisClass = (*env)->GetObjectClass(env, obj);
96   fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
97   fd = (*env)->GetIntField(env, obj, fid);
98
99   if(fd < 0) {
100     fprintf(stderr, "FileInputStream::read(): invalid file state\n");
101     throwAFSFileException(env, 0, "Invalid file state");
102     return -1;
103   }
104
105   bytes = (*env) -> GetByteArrayElements(env, jbytes, 0);
106   bytesLen = (*env) -> GetArrayLength(env, jbytes);
107   bytesRead = uafs_read(fd, bytes, bytesLen);
108
109   if (errno != 0) throwAFSFileException(env, errno, NULL);
110
111   (*env) -> ReleaseByteArrayElements(env, jbytes, bytes, 0);
112   return (bytesRead > 0) ? bytesRead : -1;
113 }
114
115 /**
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. 
119  * 
120  *  env         the Java environment
121  *  obj         the current Java object
122  *
123  * @exception   AFSFileException  if an I/O or other file related error occurs.
124  */
125 JNIEXPORT void JNICALL Java_org_openafs_jafs_FileInputStream_close
126   (JNIEnv *env, jobject obj)
127 {
128   int fd, rc;
129   jclass thisClass;
130   jmethodID getFileDescriptorID;
131   jfieldID fid;
132   char *bytes;
133
134   thisClass = (*env)->GetObjectClass(env, obj);
135   fid = (*env)->GetFieldID(env, thisClass, "fileDescriptor", "I");
136   fd = (*env)->GetIntField(env, obj, fid);
137
138   if(fd < 0) {
139     fprintf(stderr, "FileInputStream::close(): invalid file state\n");
140     throwAFSFileException(env, 0, "Invalid file state");
141     return;
142   }
143
144   rc = uafs_close(fd);
145
146   if (rc != 0) {
147     throwAFSFileException(env, errno, NULL);
148   }
149 }
150
151
152