8164147: Improve streaming socket output

Reviewed-by: chegar, igerasim
This commit is contained in:
Mark Sheppard 2016-09-13 11:59:56 +01:00
parent 6e132741b6
commit 3463ee94d8
4 changed files with 90 additions and 80 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -155,11 +155,12 @@ class SocketInputStream extends FileInputStream
} }
// bounds check // bounds check
if (length <= 0 || off < 0 || off + length > b.length) { if (length <= 0 || off < 0 || length > b.length - off) {
if (length == 0) { if (length == 0) {
return 0; return 0;
} }
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException("length == " + length
+ " off == " + off + " buffer length == " + b.length);
} }
boolean gotReset = false; boolean gotReset = false;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -97,11 +97,13 @@ class SocketOutputStream extends FileOutputStream
*/ */
private void socketWrite(byte b[], int off, int len) throws IOException { private void socketWrite(byte b[], int off, int len) throws IOException {
if (len <= 0 || off < 0 || off + len > b.length) {
if (len <= 0 || off < 0 || len > b.length - off) {
if (len == 0) { if (len == 0) {
return; return;
} }
throw new ArrayIndexOutOfBoundsException(); throw new ArrayIndexOutOfBoundsException("len == " + len
+ " off == " + off + " buffer length == " + b.length);
} }
FileDescriptor fd = impl.acquireFD(); FileDescriptor fd = impl.acquireFD();

View File

@ -98,6 +98,9 @@ Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
int llen = chunkLen; int llen = chunkLen;
(*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
if ((*env)->ExceptionCheck(env)) {
break;
} else {
while(llen > 0) { while(llen > 0) {
int n = NET_Send(fd, bufP + loff, llen, 0); int n = NET_Send(fd, bufP + loff, llen, 0);
if (n > 0) { if (n > 0) {
@ -120,6 +123,7 @@ Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
len -= chunkLen; len -= chunkLen;
off += chunkLen; off += chunkLen;
} }
}
if (bufP != BUF) { if (bufP != BUF) {
free(bufP); free(bufP);

View File

@ -92,7 +92,9 @@ Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
int retry = 0; int retry = 0;
(*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
if ((*env)->ExceptionCheck(env)) {
break;
} else {
while(llen > 0) { while(llen > 0) {
int n = send(fd, bufP + loff, llen, 0); int n = send(fd, bufP + loff, llen, 0);
if (n > 0) { if (n > 0) {
@ -153,6 +155,7 @@ Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
len -= chunkLen; len -= chunkLen;
off += chunkLen; off += chunkLen;
} }
}
if (bufP != BUF) { if (bufP != BUF) {
free(bufP); free(bufP);