diff --git a/src/main/java/com/github/dockerjava/netty/InvocationBuilder.java b/src/main/java/com/github/dockerjava/netty/InvocationBuilder.java index 83ce63979..70737eeac 100644 --- a/src/main/java/com/github/dockerjava/netty/InvocationBuilder.java +++ b/src/main/java/com/github/dockerjava/netty/InvocationBuilder.java @@ -22,13 +22,8 @@ import io.netty.util.concurrent.GenericFutureListener; import java.io.BufferedInputStream; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map; @@ -249,9 +244,9 @@ public void operationComplete(Future future) throws Exception { // now we can start a new thread that reads from stdin and writes to the channel new Thread(new Runnable() { - private int read(BufferedReader reader) { + private int read(InputStream is, byte[] buf) { try { - return reader.read(); + return is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } @@ -260,19 +255,13 @@ private int read(BufferedReader reader) { @Override public void run() { - BufferedReader reader = new BufferedReader(new InputStreamReader(stdin, Charset.forName("UTF-8"))); + byte[] buffer = new byte[1024]; - int read = -1; - while ((read = read(reader)) != -1) { - byte[] bytes = ByteBuffer.allocate(4).putInt(read).array(); - try { - bytes = new String(bytes).getBytes("US-ASCII"); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - - channel.writeAndFlush(Unpooled.copiedBuffer(bytes)); + int read; + while ((read = read(stdin, buffer)) != -1) { + channel.writeAndFlush(Unpooled.copiedBuffer(buffer, 0, read)); } + } }).start(); } diff --git a/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java b/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java index e4ceedac7..6436cd5da 100644 --- a/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java +++ b/src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java @@ -109,16 +109,16 @@ public void execStartAttachStdin() throws Exception { dockerClient.startContainerCmd(container.getId()).exec(); - InputStream stdin = new ByteArrayInputStream("echo STDIN\n".getBytes()); + InputStream stdin = new ByteArrayInputStream("STDIN\n".getBytes("UTF-8")); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()) - .withAttachStdout(true).withAttachStdin(true).withCmd("/bin/sh").exec(); + .withAttachStdout(true).withAttachStdin(true).withCmd("cat").exec(); dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true).withStdIn(stdin) .exec(new ExecStartResultCallback(stdout, System.err)).awaitCompletion(5, TimeUnit.SECONDS); - assertEquals(stdout.toString(), "STDIN\n"); + assertEquals(stdout.toString("UTF-8"), "STDIN\n"); } @Test(groups = "ignoreInCircleCi")