src/java.base/share/native/libjava/io_util.c

Print this page
rev 10764 : 8023173: FileDescriptor should remember its append mode [win]

 106         JNU_ThrowIOException(env, "Stream Closed");
 107         nread = -1;
 108     } else {
 109         nread = IO_Read(fd, buf, len);
 110         if (nread > 0) {
 111             (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
 112         } else if (nread == -1) {
 113             JNU_ThrowIOExceptionWithLastError(env, "Read error");
 114         } else { /* EOF */
 115             nread = -1;
 116         }
 117     }
 118 
 119     if (buf != stackBuf) {
 120         free(buf);
 121     }
 122     return nread;
 123 }
 124 
 125 void
 126 writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid) {
 127     // Discard the 24 high-order bits of byte. See OutputStream#write(int)
 128     char c = (char) byte;
 129     jint n;
 130     FD fd = GET_FD(this, fid);
 131     if (fd == -1) {
 132         JNU_ThrowIOException(env, "Stream Closed");
 133         return;
 134     }
 135     if (append == JNI_TRUE) {
 136         n = IO_Append(fd, &c, 1);
 137     } else {
 138         n = IO_Write(fd, &c, 1);
 139     }
 140     if (n == -1) {
 141         JNU_ThrowIOExceptionWithLastError(env, "Write error");
 142     }
 143 }
 144 
 145 void
 146 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
 147            jint off, jint len, jboolean append, jfieldID fid)
 148 {
 149     jint n;
 150     char stackBuf[BUF_SIZE];
 151     char *buf = NULL;
 152     FD fd;
 153 
 154     if (IS_NULL(bytes)) {
 155         JNU_ThrowNullPointerException(env, NULL);
 156         return;
 157     }
 158 
 159     if (outOfBounds(env, off, len, bytes)) {
 160         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
 161         return;
 162     }
 163 
 164     if (len == 0) {
 165         return;
 166     } else if (len > BUF_SIZE) {
 167         buf = malloc(len);
 168         if (buf == NULL) {
 169             JNU_ThrowOutOfMemoryError(env, NULL);
 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) {
 183                 JNU_ThrowIOException(env, "Stream Closed");
 184                 break;
 185             }
 186             if (append == JNI_TRUE) {
 187                 n = IO_Append(fd, buf+off, len);
 188             } else {
 189                 n = IO_Write(fd, buf+off, len);
 190             }
 191             if (n == -1) {
 192                 JNU_ThrowIOExceptionWithLastError(env, "Write error");
 193                 break;
 194             }
 195             off += n;
 196             len -= n;
 197         }
 198     }
 199     if (buf != stackBuf) {
 200         free(buf);
 201     }
 202 }
 203 
 204 void
 205 throwFileNotFoundException(JNIEnv *env, jstring path)
 206 {
 207     char buf[256];
 208     size_t n;
 209     jobject x;
 210     jstring why = NULL;

 106         JNU_ThrowIOException(env, "Stream Closed");
 107         nread = -1;
 108     } else {
 109         nread = IO_Read(fd, buf, len);
 110         if (nread > 0) {
 111             (*env)->SetByteArrayRegion(env, bytes, off, nread, (jbyte *)buf);
 112         } else if (nread == -1) {
 113             JNU_ThrowIOExceptionWithLastError(env, "Read error");
 114         } else { /* EOF */
 115             nread = -1;
 116         }
 117     }
 118 
 119     if (buf != stackBuf) {
 120         free(buf);
 121     }
 122     return nread;
 123 }
 124 
 125 void
 126 writeSingle(JNIEnv *env, jobject this, jint byte, jfieldID fid) {
 127     // Discard the 24 high-order bits of byte. See OutputStream#write(int)
 128     char c = (char) byte;
 129     jint n;
 130     FD fd = GET_FD(this, fid);
 131     if (fd == -1) {
 132         JNU_ThrowIOException(env, "Stream Closed");
 133         return;
 134     }
 135     n = IO_Write(env, this, fid, &c, 1);




 136     if (n == -1) {
 137         JNU_ThrowIOExceptionWithLastError(env, "Write error");
 138     }
 139 }
 140 
 141 void
 142 writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
 143            jint off, jint len, jfieldID fid)
 144 {
 145     jint n;
 146     char stackBuf[BUF_SIZE];
 147     char *buf = NULL;
 148     FD fd;
 149 
 150     if (IS_NULL(bytes)) {
 151         JNU_ThrowNullPointerException(env, NULL);
 152         return;
 153     }
 154 
 155     if (outOfBounds(env, off, len, bytes)) {
 156         JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
 157         return;
 158     }
 159 
 160     if (len == 0) {
 161         return;
 162     } else if (len > BUF_SIZE) {
 163         buf = malloc(len);
 164         if (buf == NULL) {
 165             JNU_ThrowOutOfMemoryError(env, NULL);
 166             return;
 167         }
 168     } else {
 169         buf = stackBuf;
 170     }
 171 
 172     (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);
 173 
 174     if (!(*env)->ExceptionOccurred(env)) {
 175         off = 0;
 176         while (len > 0) {
 177             fd = GET_FD(this, fid);
 178             if (fd == -1) {
 179                 JNU_ThrowIOException(env, "Stream Closed");
 180                 break;
 181             }
 182             n = IO_Write(env, this, fid, buf+off, len);




 183             if (n == -1) {
 184                 JNU_ThrowIOExceptionWithLastError(env, "Write error");
 185                 break;
 186             }
 187             off += n;
 188             len -= n;
 189         }
 190     }
 191     if (buf != stackBuf) {
 192         free(buf);
 193     }
 194 }
 195 
 196 void
 197 throwFileNotFoundException(JNIEnv *env, jstring path)
 198 {
 199     char buf[256];
 200     size_t n;
 201     jobject x;
 202     jstring why = NULL;