author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 47 | c8f0e41aea68 |
child 1768 | 27f2d52ea088 |
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 |
||
61 |
||
62 |
int |
|
63 |
readBytes(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) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
97 |
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
|
98 |
return -1; |
2 | 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 |
||
118 |
void |
|
119 |
writeSingle(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) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
124 |
JNU_ThrowIOException(env, "Stream Closed"); |
2 | 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 |
||
135 |
void |
|
136 |
writeBytes(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) { |
|
44
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
175 |
JNU_ThrowIOException(env, "Stream Closed"); |
9291315d799d
6632696: Writing to closed output files (writeBytes) leaks native memory (unix)
martin
parents:
2
diff
changeset
|
176 |
break; |
2 | 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 |
||
195 |
void |
|
196 |
throwFileNotFoundException(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 |
} |