提交 08af5768 编写于 作者: M martin

6792066: src/share/native/java/io/io_util.c clean-ups

Reviewed-by: alanb
上级 d389ef8a
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stddef.h>
#include "jni.h" #include "jni.h"
#include "jni_util.h" #include "jni_util.h"
...@@ -34,9 +35,9 @@ ...@@ -34,9 +35,9 @@
/* IO helper functions */ /* IO helper functions */
int jint
readSingle(JNIEnv *env, jobject this, jfieldID fid) { readSingle(JNIEnv *env, jobject this, jfieldID fid) {
int nread; jint nread;
char ret; char ret;
FD fd = GET_FD(this, fid); FD fd = GET_FD(this, fid);
if (fd == -1) { if (fd == -1) {
...@@ -49,7 +50,7 @@ readSingle(JNIEnv *env, jobject this, jfieldID fid) { ...@@ -49,7 +50,7 @@ readSingle(JNIEnv *env, jobject this, jfieldID fid) {
} else if (nread == JVM_IO_ERR) { /* error */ } else if (nread == JVM_IO_ERR) { /* error */
JNU_ThrowIOExceptionWithLastError(env, "Read error"); JNU_ThrowIOExceptionWithLastError(env, "Read error");
} else if (nread == JVM_IO_INTR) { } else if (nread == JVM_IO_INTR) {
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
} }
return ret & 0xFF; return ret & 0xFF;
} }
...@@ -71,22 +72,22 @@ outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) { ...@@ -71,22 +72,22 @@ outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
((*env)->GetArrayLength(env, array) - off < len)); ((*env)->GetArrayLength(env, array) - off < len));
} }
int jint
readBytes(JNIEnv *env, jobject this, jbyteArray bytes, readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
jint off, jint len, jfieldID fid) jint off, jint len, jfieldID fid)
{ {
int nread; jint nread;
char stackBuf[BUF_SIZE]; char stackBuf[BUF_SIZE];
char *buf = 0; char *buf = NULL;
FD fd; FD fd;
if (IS_NULL(bytes)) { if (IS_NULL(bytes)) {
JNU_ThrowNullPointerException(env, 0); JNU_ThrowNullPointerException(env, NULL);
return -1; return -1;
} }
if (outOfBounds(env, off, len, bytes)) { if (outOfBounds(env, off, len, bytes)) {
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0); JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
return -1; return -1;
} }
...@@ -94,8 +95,8 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes, ...@@ -94,8 +95,8 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
return 0; return 0;
} else if (len > BUF_SIZE) { } else if (len > BUF_SIZE) {
buf = malloc(len); buf = malloc(len);
if (buf == 0) { if (buf == NULL) {
JNU_ThrowOutOfMemoryError(env, 0); JNU_ThrowOutOfMemoryError(env, NULL);
return 0; return 0;
} }
} else { } else {
...@@ -112,8 +113,8 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes, ...@@ -112,8 +113,8 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
(*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf); (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
} else if (nread == JVM_IO_ERR) { } else if (nread == JVM_IO_ERR) {
JNU_ThrowIOExceptionWithLastError(env, "Read error"); JNU_ThrowIOExceptionWithLastError(env, "Read error");
} else if (nread == JVM_IO_INTR) { /* EOF */ } else if (nread == JVM_IO_INTR) {
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
} else { /* EOF */ } else { /* EOF */
nread = -1; nread = -1;
} }
...@@ -127,8 +128,9 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes, ...@@ -127,8 +128,9 @@ readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
void void
writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) { writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) {
char c = byte; // Discard the 24 high-order bits of byte. See OutputStream#write(int)
int n; char c = (char) byte;
jint n;
FD fd = GET_FD(this, fid); FD fd = GET_FD(this, fid);
if (fd == -1) { if (fd == -1) {
JNU_ThrowIOException(env, "Stream Closed"); JNU_ThrowIOException(env, "Stream Closed");
...@@ -138,26 +140,26 @@ writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) { ...@@ -138,26 +140,26 @@ writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) {
if (n == JVM_IO_ERR) { if (n == JVM_IO_ERR) {
JNU_ThrowIOExceptionWithLastError(env, "Write error"); JNU_ThrowIOExceptionWithLastError(env, "Write error");
} else if (n == JVM_IO_INTR) { } else if (n == JVM_IO_INTR) {
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
} }
} }
void void
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
jint off, jint len, jfieldID fid) jint off, jint len, jfieldID fid)
{ {
int n; jint n;
char stackBuf[BUF_SIZE]; char stackBuf[BUF_SIZE];
char *buf = 0; char *buf = NULL;
FD fd; FD fd;
if (IS_NULL(bytes)) { if (IS_NULL(bytes)) {
JNU_ThrowNullPointerException(env, 0); JNU_ThrowNullPointerException(env, NULL);
return; return;
} }
if (outOfBounds(env, off, len, bytes)) { if (outOfBounds(env, off, len, bytes)) {
JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", 0); JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
return; return;
} }
...@@ -165,8 +167,8 @@ writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, ...@@ -165,8 +167,8 @@ writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
return; return;
} else if (len > BUF_SIZE) { } else if (len > BUF_SIZE) {
buf = malloc(len); buf = malloc(len);
if (buf == 0) { if (buf == NULL) {
JNU_ThrowOutOfMemoryError(env, 0); JNU_ThrowOutOfMemoryError(env, NULL);
return; return;
} }
} else { } else {
...@@ -188,7 +190,7 @@ writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, ...@@ -188,7 +190,7 @@ writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
JNU_ThrowIOExceptionWithLastError(env, "Write error"); JNU_ThrowIOExceptionWithLastError(env, "Write error");
break; break;
} else if (n == JVM_IO_INTR) { } else if (n == JVM_IO_INTR) {
JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
break; break;
} }
off += n; off += n;
...@@ -204,19 +206,19 @@ void ...@@ -204,19 +206,19 @@ void
throwFileNotFoundException(JNIEnv *env, jstring path) throwFileNotFoundException(JNIEnv *env, jstring path)
{ {
char buf[256]; char buf[256];
int n; jint n;
jobject x; jobject x;
jstring why = NULL; jstring why = NULL;
n = JVM_GetLastErrorString(buf, sizeof(buf)); n = JVM_GetLastErrorString(buf, sizeof(buf));
if (n > 0) { if (n > 0) {
why = JNU_NewStringPlatform(env, buf); why = JNU_NewStringPlatform(env, buf);
} }
x = JNU_NewObjectByName(env, x = JNU_NewObjectByName(env,
"java/io/FileNotFoundException", "java/io/FileNotFoundException",
"(Ljava/lang/String;Ljava/lang/String;)V", "(Ljava/lang/String;Ljava/lang/String;)V",
path, why); path, why);
if (x != NULL) { if (x != NULL) {
(*env)->Throw(env, x); (*env)->Throw(env, x);
} }
} }
...@@ -38,9 +38,9 @@ extern jfieldID IO_handle_fdID; ...@@ -38,9 +38,9 @@ extern jfieldID IO_handle_fdID;
* IO helper functions * IO helper functions
*/ */
int readSingle(JNIEnv *env, jobject this, jfieldID fid); jint readSingle(JNIEnv *env, jobject this, jfieldID fid);
int readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off, jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
jint len, jfieldID fid); jint len, jfieldID fid);
void writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid); void writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid);
void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off, void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,
jint len, jfieldID fid); jint len, jfieldID fid);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册