1 /*
   2  * Copyright (c) 1994, 2007, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <stdlib.h>
  27 #include <string.h>
  28 #include <stddef.h>
  29 
  30 #include "jni.h"
  31 #include "jni_util.h"
  32 #include "jvm.h"
  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 == JVM_IO_ERR) { /* error */
  51         JNU_ThrowIOExceptionWithLastError(env, "Read error");
  52     } else if (nread == JVM_IO_INTR) {
  53         JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
  54     }
  55     return ret & 0xFF;
  56 }
  57 
  58 /* The maximum size of a stack-allocated buffer.
  59  */
  60 #define BUF_SIZE 8192
  61 
  62 /*
  63  * Returns true if the array slice defined by the given offset and length
  64  * is out of bounds.
  65  */
  66 static int
  67 outOfBounds(JNIEnv *env, jint off, jint len, jbyteArray array) {
  68     return ((off < 0) ||
  69             (len < 0) ||
  70             // We are very careful to avoid signed integer overflow,
  71             // the result of which is undefined in C.
  72             ((*env)->GetArrayLength(env, array) - off < len));
  73 }
  74 
  75 jint
  76 readBytes(JNIEnv *env, jobject this, jbyteArray bytes,
  77           jint off, jint len, jfieldID fid)
  78 {
  79     jint nread;
  80     char stackBuf[BUF_SIZE];
  81     char *buf = NULL;
  82     FD fd;
  83 
  84     if (IS_NULL(bytes)) {
  85         JNU_ThrowNullPointerException(env, NULL);
  86         return -1;
  87     }
  88 
  89     if (outOfBounds(env, off, len, bytes)) {
  90         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
  91         return -1;
  92     }
  93 
  94     if (len == 0) {
  95         return 0;
  96     } else if (len > BUF_SIZE) {
  97         buf = malloc(len);
  98         if (buf == NULL) {
  99             JNU_ThrowOutOfMemoryError(env, NULL);
 100             return 0;
 101         }
 102     } else {
 103         buf = stackBuf;
 104     }
 105 
 106     fd = GET_FD(this, fid);
 107     if (fd == -1) {
 108         JNU_ThrowIOException(env, "Stream Closed");
 109         nread = -1;
 110     } else {
 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) {
 117             JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
 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, 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     n = IO_Write(fd, &c, 1);
 140     if (n == JVM_IO_ERR) {
 141         JNU_ThrowIOExceptionWithLastError(env, "Write error");
 142     } else if (n == JVM_IO_INTR) {
 143         JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
 144     }
 145 }
 146 
 147 void
 148 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
 149            jint off, jint len, jfieldID fid)
 150 {
 151     jint n;
 152     char stackBuf[BUF_SIZE];
 153     char *buf = NULL;
 154     FD fd;
 155 
 156     if (IS_NULL(bytes)) {
 157         JNU_ThrowNullPointerException(env, NULL);
 158         return;
 159     }
 160 
 161     if (outOfBounds(env, off, len, bytes)) {
 162         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
 163         return;
 164     }
 165 
 166     if (len == 0) {
 167         return;
 168     } else if (len > BUF_SIZE) {
 169         buf = malloc(len);
 170         if (buf == NULL) {
 171             JNU_ThrowOutOfMemoryError(env, NULL);
 172             return;
 173         }
 174     } else {
 175         buf = stackBuf;
 176     }
 177 
 178     (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
 179 
 180     if (!(*env)->ExceptionOccurred(env)) {
 181         off = 0;
 182         while (len > 0) {
 183             fd = GET_FD(this, fid);
 184             if (fd == -1) {
 185                 JNU_ThrowIOException(env, "Stream Closed");
 186                 break;
 187             }
 188             n = IO_Write(fd, buf+off, len);
 189             if (n == JVM_IO_ERR) {
 190                 JNU_ThrowIOExceptionWithLastError(env, "Write error");
 191                 break;
 192             } else if (n == JVM_IO_INTR) {
 193                 JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
 194                 break;
 195             }
 196             off += n;
 197             len -= n;
 198         }
 199     }
 200     if (buf != stackBuf) {
 201         free(buf);
 202     }
 203 }
 204 
 205 void
 206 throwFileNotFoundException(JNIEnv *env, jstring path)
 207 {
 208     char buf[256];
 209     size_t n;
 210     jobject x;
 211     jstring why = NULL;
 212 
 213     n = getLastErrorString(buf, sizeof(buf));
 214     if (n > 0) {
 215 #ifdef WIN32
 216         why = (*env)->NewStringUTF(env, buf);
 217 #else
 218         why = JNU_NewStringPlatform(env, buf);
 219 #endif
 220     }
 221     x = JNU_NewObjectByName(env,
 222                             "java/io/FileNotFoundException",
 223                             "(Ljava/lang/String;Ljava/lang/String;)V",
 224                             path, why);
 225     if (x != NULL) {
 226         (*env)->Throw(env, x);
 227     }
 228 }