Skip to content

Commit 9ec46f4

Browse files
committed
Merge pull request docker-java#466 from rtimush/fix-exec-stdin
Fix exec start stdin encoding
2 parents ff4055c + 8754443 commit 9ec46f4

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/main/java/com/github/dockerjava/netty/InvocationBuilder.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
import io.netty.util.concurrent.GenericFutureListener;
2323

2424
import java.io.BufferedInputStream;
25-
import java.io.BufferedReader;
2625
import java.io.IOException;
2726
import java.io.InputStream;
28-
import java.io.InputStreamReader;
29-
import java.io.UnsupportedEncodingException;
30-
import java.nio.ByteBuffer;
31-
import java.nio.charset.Charset;
3227
import java.util.HashMap;
3328
import java.util.Map;
3429

@@ -249,9 +244,9 @@ public void operationComplete(Future<? super Void> future) throws Exception {
249244
// now we can start a new thread that reads from stdin and writes to the channel
250245
new Thread(new Runnable() {
251246

252-
private int read(BufferedReader reader) {
247+
private int read(InputStream is, byte[] buf) {
253248
try {
254-
return reader.read();
249+
return is.read(buf);
255250
} catch (IOException e) {
256251
throw new RuntimeException(e);
257252
}
@@ -260,19 +255,13 @@ private int read(BufferedReader reader) {
260255
@Override
261256
public void run() {
262257

263-
BufferedReader reader = new BufferedReader(new InputStreamReader(stdin, Charset.forName("UTF-8")));
258+
byte[] buffer = new byte[1024];
264259

265-
int read = -1;
266-
while ((read = read(reader)) != -1) {
267-
byte[] bytes = ByteBuffer.allocate(4).putInt(read).array();
268-
try {
269-
bytes = new String(bytes).getBytes("US-ASCII");
270-
} catch (UnsupportedEncodingException e) {
271-
throw new RuntimeException(e);
272-
}
273-
274-
channel.writeAndFlush(Unpooled.copiedBuffer(bytes));
260+
int read;
261+
while ((read = read(stdin, buffer)) != -1) {
262+
channel.writeAndFlush(Unpooled.copiedBuffer(buffer, 0, read));
275263
}
264+
276265
}
277266
}).start();
278267
}

src/test/java/com/github/dockerjava/netty/exec/ExecStartCmdExecTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ public void execStartAttachStdin() throws Exception {
109109

110110
dockerClient.startContainerCmd(container.getId()).exec();
111111

112-
InputStream stdin = new ByteArrayInputStream("echo STDIN\n".getBytes());
112+
InputStream stdin = new ByteArrayInputStream("STDIN\n".getBytes("UTF-8"));
113113

114114
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
115115

116116
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
117-
.withAttachStdout(true).withAttachStdin(true).withCmd("/bin/sh").exec();
117+
.withAttachStdout(true).withAttachStdin(true).withCmd("cat").exec();
118118
dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true).withStdIn(stdin)
119119
.exec(new ExecStartResultCallback(stdout, System.err)).awaitCompletion(5, TimeUnit.SECONDS);
120120

121-
assertEquals(stdout.toString(), "STDIN\n");
121+
assertEquals(stdout.toString("UTF-8"), "STDIN\n");
122122
}
123123

124124
@Test(groups = "ignoreInCircleCi")

0 commit comments

Comments
 (0)