author | martin |
Wed, 07 Jan 2009 11:50:32 -0800 | |
changeset 1768 | 27f2d52ea088 |
parent 715 | f16baef3a20e |
child 1769 | 27a33d1b16ec |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 1994-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 |
||
37 |
int |
|
38 |
readSingle(JNIEnv *env, jobject this, jfieldID fid) { |
|
39 |
int nread; |
|
40 |
char ret; |
|
41 |
FD fd = GET_FD(this, fid); |
|
42 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
43 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 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 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
61 |
/* |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
62 |
* Returns true if the array slice defined by the given offset and length |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
63 |
* is out of bounds. |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
64 |
*/ |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
65 |
static int |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
66 |
outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) { |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
67 |
return ((off < 0) || |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
68 |
(len < 0) || |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
69 |
// We are very careful to avoid signed integer overflow, |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
70 |
// the result of which is undefined in C. |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
71 |
((*env)->GetArrayLength(env, array) - off < len)); |
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
72 |
} |
2 | 73 |
|
74 |
int |
|
75 |
readBytes(JNIEnv *env, jobject this, jbyteArray bytes, |
|
76 |
jint off, jint len, jfieldID fid) |
|
77 |
{ |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
78 |
int nread; |
2 | 79 |
char stackBuf[BUF_SIZE]; |
80 |
char *buf = 0; |
|
81 |
FD fd; |
|
82 |
||
83 |
if (IS_NULL(bytes)) { |
|
84 |
JNU_ThrowNullPointerException(env, 0); |
|
85 |
return -1; |
|
86 |
} |
|
87 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
88 |
if (outOfBounds(env, off, len, bytes)) { |
2 | 89 |
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0); |
90 |
return -1; |
|
91 |
} |
|
92 |
||
93 |
if (len == 0) { |
|
94 |
return 0; |
|
95 |
} else if (len > BUF_SIZE) { |
|
96 |
buf = malloc(len); |
|
97 |
if (buf == 0) { |
|
98 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
99 |
return 0; |
|
100 |
} |
|
101 |
} else { |
|
102 |
buf = stackBuf; |
|
103 |
} |
|
104 |
||
105 |
fd = GET_FD(this, fid); |
|
106 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
107 |
JNU_ThrowIOException(env, "Stream Closed"); |
47
c8f0e41aea68
6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
44
diff
changeset
|
108 |
return -1; |
2 | 109 |
} |
110 |
||
111 |
nread = IO_Read(fd, buf, len); |
|
112 |
if (nread > 0) { |
|
113 |
(*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf); |
|
114 |
} else if (nread == JVM_IO_ERR) { |
|
115 |
JNU_ThrowIOExceptionWithLastError(env, "Read error"); |
|
116 |
} else if (nread == JVM_IO_INTR) { /* EOF */ |
|
117 |
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); |
|
118 |
} else { /* EOF */ |
|
119 |
nread = -1; |
|
120 |
} |
|
121 |
||
122 |
if (buf != stackBuf) { |
|
123 |
free(buf); |
|
124 |
} |
|
125 |
return nread; |
|
126 |
} |
|
127 |
||
128 |
void |
|
129 |
writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) { |
|
130 |
char c = byte; |
|
131 |
int n; |
|
132 |
FD fd = GET_FD(this, fid); |
|
133 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
134 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 135 |
return; |
136 |
} |
|
137 |
n = IO_Write(fd, &c, 1); |
|
138 |
if (n == JVM_IO_ERR) { |
|
139 |
JNU_ThrowIOExceptionWithLastError(env, "Write error"); |
|
140 |
} else if (n == JVM_IO_INTR) { |
|
141 |
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
void |
|
146 |
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, |
|
147 |
jint off, jint len, jfieldID fid) |
|
148 |
{ |
|
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
149 |
int n; |
2 | 150 |
char stackBuf[BUF_SIZE]; |
151 |
char *buf = 0; |
|
152 |
FD fd; |
|
153 |
||
154 |
if (IS_NULL(bytes)) { |
|
155 |
JNU_ThrowNullPointerException(env, 0); |
|
156 |
return; |
|
157 |
} |
|
158 |
||
1768
27f2d52ea088
6788196: (porting) Bounds checks in io_util.c rely on undefined behaviour
martin
parents:
715
diff
changeset
|
159 |
if (outOfBounds(env, off, len, bytes)) { |
2 | 160 |
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0); |
161 |
return; |
|
162 |
} |
|
163 |
||
164 |
if (len == 0) { |
|
165 |
return; |
|
166 |
} else if (len > BUF_SIZE) { |
|
167 |
buf = malloc(len); |
|
168 |
if (buf == 0) { |
|
169 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
170 |
return; |
|
171 |
} |
|
172 |
} else { |
|
173 |
buf = stackBuf; |
|
174 |
} |
|
175 |
||
176 |
(*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf); |
|
177 |
||
178 |
if (!(*env)->ExceptionOccurred(env)) { |
|
179 |
off = 0; |
|
180 |
while (len > 0) { |
|
181 |
fd = GET_FD(this, fid); |
|
182 |
if (fd == -1) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
183 |
JNU_ThrowIOException(env, "Stream Closed"); |
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
184 |
break; |
2 | 185 |
} |
186 |
n = IO_Write(fd, buf+off, len); |
|
187 |
if (n == JVM_IO_ERR) { |
|
188 |
JNU_ThrowIOExceptionWithLastError(env, "Write error"); |
|
189 |
break; |
|
190 |
} else if (n == JVM_IO_INTR) { |
|
191 |
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); |
|
192 |
break; |
|
193 |
} |
|
194 |
off += n; |
|
195 |
len -= n; |
|
196 |
} |
|
197 |
} |
|
198 |
if (buf != stackBuf) { |
|
199 |
free(buf); |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
void |
|
204 |
throwFileNotFoundException(JNIEnv *env, jstring path) |
|
205 |
{ |
|
206 |
char buf[256]; |
|
207 |
int n; |
|
208 |
jobject x; |
|
209 |
jstring why = NULL; |
|
210 |
||
211 |
n = JVM_GetLastErrorString(buf, sizeof(buf)); |
|
212 |
if (n > 0) { |
|
213 |
why = JNU_NewStringPlatform(env, buf); |
|
214 |
} |
|
215 |
x = JNU_NewObjectByName(env, |
|
216 |
"java/io/FileNotFoundException", |
|
217 |
"(Ljava/lang/String;Ljava/lang/String;)V", |
|
218 |
path, why); |
|
219 |
if (x != NULL) { |
|
220 |
(*env)->Throw(env, x); |
|
221 |
} |
|
222 |
} |