提交 bfbaf2c9 编写于 作者: J Jerome Lacoste

JENKINS-8686 IO stream copies are not done properly.

First pass at making sure we don't stop copying when the reader reads 0 characters.

I will now also review the callers for our internal code, and check if we can use IOUtils methods instead.
上级 2332b114
......@@ -306,7 +306,7 @@ public abstract class Proc {
try {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
out.flush();
}
......@@ -315,7 +315,7 @@ public abstract class Proc {
out.close();
}
} catch (IOException e) {
// TODO: what to do?
throw new RuntimeException(e);
}
}
}
......@@ -331,7 +331,7 @@ public abstract class Proc {
}
/**
* Retemoly launched process via {@link Channel}.
* Remotely launched process via {@link hudson.remoting.Channel}.
*/
public static final class RemoteProc extends Proc {
private final Future<Integer> process;
......
......@@ -422,14 +422,14 @@ public class Util {
public static void copyStream(InputStream in,OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0)
while((len=in.read(buf))>=0)
out.write(buf,0,len);
}
public static void copyStream(Reader in, Writer out) throws IOException {
char[] buf = new char[8192];
int len;
while((len=in.read(buf))>0)
while((len=in.read(buf))>=0)
out.write(buf,0,len);
}
......@@ -519,7 +519,7 @@ public class Util {
DigestInputStream in =new DigestInputStream(source,md5);
try {
while(in.read(garbage)>0)
while(in.read(garbage)>=0)
; // simply discard the input
} finally {
in.close();
......
......@@ -365,7 +365,7 @@ public class LargeText {
return in.read(buf);
}
public int read(byte[] buf, int offset, int length) throws IOException {
public int read(byte[] buf, int offset, int length) throws IOException { // FIXME-CHECK-CALLERS
return in.read(buf,offset,length);
}
}
......
......@@ -291,7 +291,7 @@ public class TarInputStream extends FilterInputStream {
* @throws IOException on error
*/
public int read() throws IOException {
int num = this.read(this.oneBuf, 0, 1);
int num = this.read(this.oneBuf, 0, 1); // FIXME check if read() guarantees to return at least 1 character, otherwise bug lurking
return num == -1 ? -1 : ((int) this.oneBuf[0]) & 0xFF;
}
......
......@@ -138,7 +138,7 @@ public class ChunkedInputStream extends InputStream {
* @throws IOException if an IO problem occurs.
*/
@Override
public int read (byte[] b, int off, int len) throws IOException {
public int read (byte[] b, int off, int len) throws IOException { // FIXME-CHECK-CALLERS
if (closed) {
throw new IOException("Attempted read from closed stream.");
......@@ -168,7 +168,7 @@ public class ChunkedInputStream extends InputStream {
* @throws IOException if an IO problem occurs.
*/
@Override
public int read (byte[] b) throws IOException {
public int read (byte[] b) throws IOException { // FIXME-CHECK-CALLERS
return read(b, 0, b.length);
}
......
......@@ -57,7 +57,7 @@ public class HeadBufferingStream extends FilterInputStream {
}
@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte b[], int off, int len) throws IOException { // FIXME-CHECK-CALLERS
int r = in.read(b, off, len);
if(r>0) {
int sp = space();
......
......@@ -57,7 +57,7 @@ public class StreamCopyThread extends Thread {
try {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0)
while ((len = in.read(buf)) >= 0)
out.write(buf, 0, len);
} finally {
// it doesn't make sense not to close InputStream that's already EOF-ed,
......@@ -67,7 +67,7 @@ public class StreamCopyThread extends Thread {
out.close();
}
} catch (IOException e) {
// TODO: what to do?
throw new RuntimeException(e);
}
}
}
......@@ -57,10 +57,13 @@ final class ZipArchiver extends Archiver {
} else {
zip.putNextEntry(new ZipEntry(relativePath));
FileInputStream in = new FileInputStream(f);
int len;
while((len=in.read(buf))>0)
zip.write(buf,0,len);
in.close();
try {
int len;
while((len=in.read(buf))>=0)
zip.write(buf,0,len);
} finally {
in.close();
}
zip.closeEntry();
}
entriesWritten++;
......
......@@ -90,7 +90,7 @@ public class LaunchTest extends TestCase {
public static void copyStream(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0)
while((len=in.read(buf))>=0)
out.write(buf,0,len);
}
}
......@@ -186,7 +186,10 @@ public final class BinarySafeStream {
int avail = super.available();
if(avail >0) {
byte[] buf = new byte[avail];
baos.write(buf,0,super.read(buf));
int len2 = super.read(buf);
if (len2 > 0) {
baos.write(buf,0,len2);
}
}
StringBuilder buf = new StringBuilder("Invalid encoded sequence encountered:");
for (byte ch : baos.toByteArray())
......
......@@ -143,11 +143,11 @@ public class FastPipedInputStream extends InputStream {
public int read() throws IOException {
byte[] b = new byte[1];
return read(b, 0, b.length) == -1 ? -1 : (255 & b[0]);
return read(b, 0, b.length) == -1 ? -1 : (255 & b[0]); // FIXME check if read() guarantees to return at least 1 character, otherwise bug lurking
}
@Override
public int read(byte[] b) throws IOException {
public int read(byte[] b) throws IOException { // FIXME-CHECK-CALLERS
return read(b, 0, b.length);
}
......
......@@ -453,9 +453,12 @@ final class RemoteClassLoader extends URLClassLoader {
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0)
baos.write(buf,0,len);
in.close();
try {
while((len=in.read(buf))>=0)
baos.write(buf,0,len);
} finally {
in.close();
}
return baos.toByteArray();
}
......
......@@ -68,11 +68,11 @@ public class RemoteInputStream extends InputStream implements Serializable {
return core.read();
}
public int read(byte[] b) throws IOException {
public int read(byte[] b) throws IOException { // FIXME-CHECK-CALLERS
return core.read(b);
}
public int read(byte[] b, int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException { // FIXME-CHECK-CALLERS
return core.read(b, off, len);
}
......
......@@ -24,14 +24,14 @@ final class CopyThread extends Thread {
try {
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0)
while ((len = in.read(buf)) >= 0)
out.write(buf, 0, len);
} finally {
in.close();
out.close();
}
} catch (IOException e) {
// TODO: what to do?
throw new RuntimeException(e);
}
}
}
......@@ -46,11 +46,14 @@ class Copier extends Thread {
try {
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0)
out.write(buf,0,len);
in.close();
try {
while((len=in.read(buf))>=0)
out.write(buf,0,len);
} finally {
in.close();
}
} catch (IOException e) {
// TODO: what to do?
throw new RuntimeException(e);
}
}
}
......@@ -45,9 +45,12 @@ public class TestCallable implements Callable {
byte[] buf = new byte[8192];
int len;
while((len=in.read(buf))>0)
baos.write(buf,0,len);
in.close();
try {
while((len=in.read(buf))>=0)
baos.write(buf,0,len);
} finally {
in.close();
}
r[1] = baos.toByteArray();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册