blob: 4f7372babb971e85a807c8f50a8698c1736bb657 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1994-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26#include <stdlib.h>
27#include <string.h>
28
29#include "jni.h"
30#include "jni_util.h"
31#include "jvm.h"
32#include "io_util.h"
33#include "io_util_md.h"
34
35/* IO helper functions */
36
37int
38readSingle(JNIEnv *env, jobject this, jfieldID fid) {
39 int nread;
40 char ret;
41 FD fd = GET_FD(this, fid);
42 if (fd == -1) {
43 JNU_ThrowIOException (env, "Stream Closed");
44 return -1;
45 }
46 nread = IO_Read(fd, &ret, 1);
47 if (nread == 0) { /* EOF */
48 return -1;
49 } else if (nread == JVM_IO_ERR) { /* error */
50 JNU_ThrowIOExceptionWithLastError(env, "Read error");
51 } else if (nread == JVM_IO_INTR) {
52 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
53 }
54 return ret & 0xFF;
55}
56
57/* The maximum size of a stack-allocated buffer.
58 */
59#define BUF_SIZE 8192
60
61
62int
63readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
64 jint off, jint len, jfieldID fid)
65{
66 int nread, datalen;
67 char stackBuf[BUF_SIZE];
68 char *buf = 0;
69 FD fd;
70
71 if (IS_NULL(bytes)) {
72 JNU_ThrowNullPointerException(env, 0);
73 return -1;
74 }
75 datalen = (*env)->GetArrayLength(env, bytes);
76
77 if ((off < 0) || (off > datalen) ||
78 (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
79 JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
80 return -1;
81 }
82
83 if (len == 0) {
84 return 0;
85 } else if (len > BUF_SIZE) {
86 buf = malloc(len);
87 if (buf == 0) {
88 JNU_ThrowOutOfMemoryError(env, 0);
89 return 0;
90 }
91 } else {
92 buf = stackBuf;
93 }
94
95 fd = GET_FD(this, fid);
96 if (fd == -1) {
97 JNU_ThrowIOException (env, "Stream Closed");
98 return -1;
99 }
100
101 nread = IO_Read(fd, buf, len);
102 if (nread > 0) {
103 (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
104 } else if (nread == JVM_IO_ERR) {
105 JNU_ThrowIOExceptionWithLastError(env, "Read error");
106 } else if (nread == JVM_IO_INTR) { /* EOF */
107 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
108 } else { /* EOF */
109 nread = -1;
110 }
111
112 if (buf != stackBuf) {
113 free(buf);
114 }
115 return nread;
116}
117
118void
119writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) {
120 char c = byte;
121 int n;
122 FD fd = GET_FD(this, fid);
123 if (fd == -1) {
124 JNU_ThrowIOException (env, "Stream Closed");
125 return;
126 }
127 n = IO_Write(fd, &c, 1);
128 if (n == JVM_IO_ERR) {
129 JNU_ThrowIOExceptionWithLastError(env, "Write error");
130 } else if (n == JVM_IO_INTR) {
131 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
132 }
133}
134
135void
136writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
137 jint off, jint len, jfieldID fid)
138{
139 int n, datalen;
140 char stackBuf[BUF_SIZE];
141 char *buf = 0;
142 FD fd;
143
144 if (IS_NULL(bytes)) {
145 JNU_ThrowNullPointerException(env, 0);
146 return;
147 }
148 datalen = (*env)->GetArrayLength(env, bytes);
149
150 if ((off < 0) || (off > datalen) ||
151 (len < 0) || ((off + len) > datalen) || ((off + len) < 0)) {
152 JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0);
153 return;
154 }
155
156 if (len == 0) {
157 return;
158 } else if (len > BUF_SIZE) {
159 buf = malloc(len);
160 if (buf == 0) {
161 JNU_ThrowOutOfMemoryError(env, 0);
162 return;
163 }
164 } else {
165 buf = stackBuf;
166 }
167
168 (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
169
170 if (!(*env)->ExceptionOccurred(env)) {
171 off = 0;
172 while (len > 0) {
173 fd = GET_FD(this, fid);
174 if (fd == -1) {
175 JNU_ThrowIOException (env, "Stream Closed");
176 return;
177 }
178 n = IO_Write(fd, buf+off, len);
179 if (n == JVM_IO_ERR) {
180 JNU_ThrowIOExceptionWithLastError(env, "Write error");
181 break;
182 } else if (n == JVM_IO_INTR) {
183 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
184 break;
185 }
186 off += n;
187 len -= n;
188 }
189 }
190 if (buf != stackBuf) {
191 free(buf);
192 }
193}
194
195void
196throwFileNotFoundException(JNIEnv *env, jstring path)
197{
198 char buf[256];
199 int n;
200 jobject x;
201 jstring why = NULL;
202
203 n = JVM_GetLastErrorString(buf, sizeof(buf));
204 if (n > 0) {
205 why = JNU_NewStringPlatform(env, buf);
206 }
207 x = JNU_NewObjectByName(env,
208 "java/io/FileNotFoundException",
209 "(Ljava/lang/String;Ljava/lang/String;)V",
210 path, why);
211 if (x != NULL) {
212 (*env)->Throw(env, x);
213 }
214}