forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpResponseStreamHandler.java
More file actions
177 lines (142 loc) · 4.8 KB
/
Copy pathHttpResponseStreamHandler.java
File metadata and controls
177 lines (142 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.github.dockerjava.netty.handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.io.IOException;
import java.io.InputStream;
import com.github.dockerjava.api.async.ResultCallback;
/**
* Handler that converts an incoming byte stream to an {@link InputStream}.
*
* @author marcus
*/
public class HttpResponseStreamHandler extends SimpleChannelInboundHandler<ByteBuf> {
private ResultCallback<InputStream> resultCallback;
private final HttpResponseInputStream stream = new HttpResponseInputStream();
public HttpResponseStreamHandler(ResultCallback<InputStream> resultCallback) {
this.resultCallback = resultCallback;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
invokeCallbackOnFirstRead();
stream.write(msg.copy());
}
private void invokeCallbackOnFirstRead() {
if (resultCallback != null) {
resultCallback.onNext(stream);
resultCallback = null;
}
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
stream.writeComplete();
super.channelInactive(ctx);
}
public static class HttpResponseInputStream extends InputStream {
private boolean writeCompleted = false;
private boolean closed = false;
private ByteBuf current = null;
private final Object lock = new Object();
public void write(ByteBuf byteBuf) throws InterruptedException {
synchronized (lock) {
if (closed) {
return;
}
while (current != null) {
lock.wait();
if (closed) {
return;
}
}
current = byteBuf;
lock.notifyAll();
}
}
public void writeComplete() {
synchronized (lock) {
writeCompleted = true;
lock.notifyAll();
}
}
@Override
public void close() throws IOException {
synchronized (lock) {
closed = true;
releaseCurrent();
lock.notifyAll();
}
}
@Override
public int available() throws IOException {
synchronized (lock) {
poll(0);
return readableBytes();
}
}
private int readableBytes() {
if (current != null) {
return current.readableBytes();
} else {
return 0;
}
}
@Override
public int read() throws IOException {
byte[] b = new byte[1];
int n = read(b, 0, 1);
return n != -1 ? b[0] : -1;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
synchronized (lock) {
off = poll(off);
if (current == null) {
return -1;
} else {
int availableBytes = Math.min(len, current.readableBytes() - off);
current.readBytes(b, off, availableBytes);
return availableBytes;
}
}
}
private int poll(int off) throws IOException {
synchronized (lock) {
while (readableBytes() <= off) {
try {
if (closed) {
throw new IOException("Stream closed");
}
off -= releaseCurrent();
if (writeCompleted) {
return off;
}
while (current == null) {
lock.wait();
if (closed) {
throw new IOException("Stream closed");
}
if (writeCompleted && current == null) {
return off;
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
return off;
}
}
private int releaseCurrent() {
synchronized (lock) {
if (current != null) {
int n = current.readableBytes();
current.release();
current = null;
lock.notifyAll();
return n;
}
return 0;
}
}
}
}