33 #include "io_util.h"
34 #include "io_util_md.h"
35
36 /* IO helper functions */
37
38 jint
39 readSingle(JNIEnv *env, jobject this, jfieldID fid) {
40 jint nread;
41 char ret;
42 FD fd = GET_FD(this, fid);
43 if (fd == -1) {
44 JNU_ThrowIOException(env, "Stream Closed");
45 return -1;
46 }
47 nread = IO_Read(fd, &ret, 1);
48 if (nread == 0) { /* EOF */
49 return -1;
50 } else if (nread == -1) { /* error */
51 JNU_ThrowIOExceptionWithLastError(env, "Read error");
52 }
53 return ret & 0xFF;
54 }
55
56 /* The maximum size of a stack-allocated buffer.
57 */
58 #define BUF_SIZE 8192
59
60 /*
61 * Returns true if the array slice defined by the given offset and length
62 * is out of bounds.
63 */
64 static int
65 outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
66 return ((off < 0) ||
67 (len < 0) ||
68 // We are very careful to avoid signed integer overflow,
69 // the result of which is undefined in C.
70 ((*env)->GetArrayLength(env, array) - off < len));
71 }
72
92 if (len == 0) {
93 return 0;
94 } else if (len > BUF_SIZE) {
95 buf = malloc(len);
96 if (buf == NULL) {
97 JNU_ThrowOutOfMemoryError(env, NULL);
98 return 0;
99 }
100 } else {
101 buf = stackBuf;
102 }
103
104 fd = GET_FD(this, fid);
105 if (fd == -1) {
106 JNU_ThrowIOException(env, "Stream Closed");
107 nread = -1;
108 } else {
109 nread = IO_Read(fd, buf, len);
110 if (nread > 0) {
111 (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
112 } else if (nread == -1) {
113 JNU_ThrowIOExceptionWithLastError(env, "Read error");
114 } else { /* EOF */
115 nread = -1;
116 }
117 }
118
119 if (buf != stackBuf) {
120 free(buf);
121 }
122 return nread;
123 }
124
125 void
126 writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) {
127 // Discard the 24 high-order bits of byte. See OutputStream#write(int)
128 char c = (char) byte;
129 jint n;
130 FD fd = GET_FD(this, fid);
131 if (fd == -1) {
132 JNU_ThrowIOException(env, "Stream Closed");
133 return;
134 }
135 if (append == JNI_TRUE) {
136 n = IO_Append(fd, &c, 1);
137 } else {
138 n = IO_Write(fd, &c, 1);
139 }
140 if (n == -1) {
141 JNU_ThrowIOExceptionWithLastError(env, "Write error");
142 }
143 }
144
145 void
146 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
147 jint off, jint len, jboolean append, jfieldID fid)
148 {
149 jint n;
150 char stackBuf[BUF_SIZE];
151 char *buf = NULL;
152 FD fd;
153
154 if (IS_NULL(bytes)) {
155 JNU_ThrowNullPointerException(env, NULL);
156 return;
157 }
158
159 if (outOfBounds(env, off, len, bytes)) {
160 JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
161 return;
162 }
178 if (!(*env)->ExceptionOccurred(env)) {
179 off = 0;
180 while (len > 0) {
181 fd = GET_FD(this, fid);
182 if (fd == -1) {
183 JNU_ThrowIOException(env, "Stream Closed");
184 break;
185 }
186 if (append == JNI_TRUE) {
187 n = IO_Append(fd, buf+off, len);
188 } else {
189 n = IO_Write(fd, buf+off, len);
190 }
191 if (n == -1) {
192 JNU_ThrowIOExceptionWithLastError(env, "Write error");
193 break;
194 }
195 off += n;
196 len -= n;
197 }
198 }
199 if (buf != stackBuf) {
200 free(buf);
201 }
202 }
203
204 void
205 throwFileNotFoundException(JNIEnv *env, jstring path)
206 {
207 char buf[256];
208 size_t n;
209 jobject x;
210 jstring why = NULL;
211
212 n = getLastErrorString(buf, sizeof(buf));
213 if (n > 0) {
214 why = JNU_NewStringPlatform(env, buf);
215 CHECK_NULL(why);
216 }
217 x = JNU_NewObjectByName(env,
|
33 #include "io_util.h"
34 #include "io_util_md.h"
35
36 /* IO helper functions */
37
38 jint
39 readSingle(JNIEnv *env, jobject this, jfieldID fid) {
40 jint nread;
41 char ret;
42 FD fd = GET_FD(this, fid);
43 if (fd == -1) {
44 JNU_ThrowIOException(env, "Stream Closed");
45 return -1;
46 }
47 nread = IO_Read(fd, &ret, 1);
48 if (nread == 0) { /* EOF */
49 return -1;
50 } else if (nread == -1) { /* error */
51 JNU_ThrowIOExceptionWithLastError(env, "Read error");
52 }
53
54 JVM_callFileReadBytes(env, 1);
55
56 return ret & 0xFF;
57 }
58
59 /* The maximum size of a stack-allocated buffer.
60 */
61 #define BUF_SIZE 8192
62
63 /*
64 * Returns true if the array slice defined by the given offset and length
65 * is out of bounds.
66 */
67 static int
68 outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
69 return ((off < 0) ||
70 (len < 0) ||
71 // We are very careful to avoid signed integer overflow,
72 // the result of which is undefined in C.
73 ((*env)->GetArrayLength(env, array) - off < len));
74 }
75
95 if (len == 0) {
96 return 0;
97 } else if (len > BUF_SIZE) {
98 buf = malloc(len);
99 if (buf == NULL) {
100 JNU_ThrowOutOfMemoryError(env, NULL);
101 return 0;
102 }
103 } else {
104 buf = stackBuf;
105 }
106
107 fd = GET_FD(this, fid);
108 if (fd == -1) {
109 JNU_ThrowIOException(env, "Stream Closed");
110 nread = -1;
111 } else {
112 nread = IO_Read(fd, buf, len);
113 if (nread > 0) {
114 (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
115 JVM_callFileReadBytes(env, nread);
116 } else if (nread == -1) {
117 JNU_ThrowIOExceptionWithLastError(env, "Read error");
118 } else { /* EOF */
119 nread = -1;
120 }
121 }
122
123 if (buf != stackBuf) {
124 free(buf);
125 }
126 return nread;
127 }
128
129 void
130 writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) {
131 // Discard the 24 high-order bits of byte. See OutputStream#write(int)
132 char c = (char) byte;
133 jint n;
134 FD fd = GET_FD(this, fid);
135 if (fd == -1) {
136 JNU_ThrowIOException(env, "Stream Closed");
137 return;
138 }
139 if (append == JNI_TRUE) {
140 n = IO_Append(fd, &c, 1);
141 } else {
142 n = IO_Write(fd, &c, 1);
143 }
144 if (n == -1) {
145 JNU_ThrowIOExceptionWithLastError(env, "Write error");
146 }
147 JVM_callFileWriteBytes(env, 1);
148 }
149
150 void
151 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
152 jint off, jint len, jboolean append, jfieldID fid)
153 {
154 jint n;
155 char stackBuf[BUF_SIZE];
156 char *buf = NULL;
157 FD fd;
158
159 if (IS_NULL(bytes)) {
160 JNU_ThrowNullPointerException(env, NULL);
161 return;
162 }
163
164 if (outOfBounds(env, off, len, bytes)) {
165 JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
166 return;
167 }
183 if (!(*env)->ExceptionOccurred(env)) {
184 off = 0;
185 while (len > 0) {
186 fd = GET_FD(this, fid);
187 if (fd == -1) {
188 JNU_ThrowIOException(env, "Stream Closed");
189 break;
190 }
191 if (append == JNI_TRUE) {
192 n = IO_Append(fd, buf+off, len);
193 } else {
194 n = IO_Write(fd, buf+off, len);
195 }
196 if (n == -1) {
197 JNU_ThrowIOExceptionWithLastError(env, "Write error");
198 break;
199 }
200 off += n;
201 len -= n;
202 }
203 JVM_callFileWriteBytes(env, off);
204 }
205 if (buf != stackBuf) {
206 free(buf);
207 }
208 }
209
210 void
211 throwFileNotFoundException(JNIEnv *env, jstring path)
212 {
213 char buf[256];
214 size_t n;
215 jobject x;
216 jstring why = NULL;
217
218 n = getLastErrorString(buf, sizeof(buf));
219 if (n > 0) {
220 why = JNU_NewStringPlatform(env, buf);
221 CHECK_NULL(why);
222 }
223 x = JNU_NewObjectByName(env,
|