Skip to content

Commit 3b3399d

Browse files
committed
Fixes skipping 0xff bytes from result stream returned by Netty implementation of copy file/archive commands
1 parent 3ddda35 commit 3b3399d

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎pom.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<hamcrest.library.version>1.3</hamcrest.library.version>
6868
<hamcrest.jpa-matchers>1.6</hamcrest.jpa-matchers>
6969
<lambdaj.version>2.3.3</lambdaj.version>
70+
<mockito.version>1.10.19</mockito.version>
7071

7172

7273
<maven-jar-plugin.version>2.2</maven-jar-plugin.version>
@@ -195,6 +196,13 @@
195196
<scope>test</scope>
196197
</dependency>
197198

199+
<dependency>
200+
<groupId>org.mockito</groupId>
201+
<artifactId>mockito-core</artifactId>
202+
<version>${mockito.version}</version>
203+
<scope>test</scope>
204+
</dependency>
205+
198206
<dependency>
199207
<groupId>com.google.code.findbugs</groupId>
200208
<artifactId>annotations</artifactId>

‎src/main/java/com/github/dockerjava/netty/handler/HttpResponseStreamHandler.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public int read() throws IOException {
8181
}
8282

8383
if (current != null && current.readableBytes() > 0) {
84-
return current.readByte();
84+
return current.readByte() & 0xff;
8585
} else {
8686
return read();
8787
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.github.dockerjava.netty.handler;
2+
3+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
4+
import io.netty.buffer.ByteBuf;
5+
import io.netty.buffer.ByteBufInputStream;
6+
import io.netty.buffer.Unpooled;
7+
import io.netty.channel.ChannelHandlerContext;
8+
import org.apache.commons.io.IOUtils;
9+
import org.mockito.Mockito;
10+
import org.testng.Assert;
11+
import org.testng.annotations.Test;
12+
13+
import java.io.InputStream;
14+
15+
/**
16+
* @author Alexander Koshevoy
17+
*/
18+
public class HttpResponseStreamHandlerTest {
19+
@Test
20+
public void testNoBytesSkipped() throws Exception {
21+
ResultCallbackTest callback = new ResultCallbackTest();
22+
HttpResponseStreamHandler streamHandler = new HttpResponseStreamHandler(callback);
23+
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
24+
ByteBuf buffer = generateByteBuf();
25+
streamHandler.channelRead0(ctx, buffer);
26+
streamHandler.channelReadComplete(ctx);
27+
28+
Assert.assertTrue(IOUtils.contentEquals(callback.getInputStream(), new ByteBufInputStream(buffer)));
29+
}
30+
31+
private ByteBuf generateByteBuf() {
32+
byte[] array = new byte[256];
33+
for (int i = 0; i < array.length; i++) {
34+
array[i] = (byte) i;
35+
}
36+
return Unpooled.copiedBuffer(array);
37+
}
38+
39+
private static class ResultCallbackTest extends ResultCallbackTemplate<ResultCallbackTest, InputStream> {
40+
private InputStream stream;
41+
42+
@Override
43+
public void onNext(InputStream stream) {
44+
this.stream = stream;
45+
}
46+
47+
public InputStream getInputStream() {
48+
return stream;
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)