forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpClientReadableByteChannel.java
139 lines (122 loc) · 3.96 KB
/
HttpClientReadableByteChannel.java
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
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.http;
import io.fabric8.kubernetes.client.http.HttpClient.AsyncBody;
import io.fabric8.kubernetes.client.http.HttpClient.BodyConsumer;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ReadableByteChannel;
import java.util.LinkedList;
import java.util.List;
/**
* Creates a blocking {@link ReadableByteChannel} from a {@link HttpResponse} containing an {@link AsyncBody}
* <p>
* May be useful eventually to provide a non-blocking channel as well.
*/
public class HttpClientReadableByteChannel implements ReadableByteChannel, BodyConsumer<List<ByteBuffer>> {
private final LinkedList<ByteBuffer> buffers = new LinkedList<>();
private Throwable failed;
private boolean closed;
private boolean done;
private AsyncBody asyncBody;
private ByteBuffer currentBuffer;
private boolean consumeRequested;
@Override
public synchronized void consume(List<ByteBuffer> value, AsyncBody asyncBody) throws Exception {
this.buffers.addAll(value);
// could proactively consume based up some byte limit
this.notifyAll();
this.consumeRequested = false;
}
protected synchronized void onResponse(HttpResponse<AsyncBody> response) {
asyncBody = response.body();
asyncBody.done().whenComplete(this::onBodyDone);
asyncBody.consume(); // pre-fetch the first results
this.notifyAll();
}
private synchronized void onBodyDone(Void v, Throwable t) {
if (t != null) {
failed = t;
}
done = true;
this.notifyAll();
}
@Override
public synchronized void close() {
if (this.closed) {
return;
}
if (asyncBody != null) {
asyncBody.cancel();
}
this.closed = true;
this.notifyAll();
}
@Override
public synchronized boolean isOpen() {
return !closed;
}
@Override
public synchronized int read(ByteBuffer arg0) throws IOException {
if (closed) {
throw new ClosedChannelException();
}
int read = 0;
while (arg0.hasRemaining()) {
while (currentBuffer == null || !currentBuffer.hasRemaining()) {
if (buffers.isEmpty()) {
if (failed != null) {
throw new IOException("channel already closed with exception", failed);
}
if (read > 0) {
return read;
}
if (done) {
return -1;
}
if (!consumeRequested && this.asyncBody != null) {
consumeRequested = true;
this.asyncBody.consume();
// the consume call may actually trigger result deliver
// if it did, then just start the loop over or be done
if (!consumeRequested) {
continue;
}
if (done) {
return -1;
}
}
try {
this.wait(); // block until more buffers are delivered
} catch (InterruptedException e) {
close();
Thread.currentThread().interrupt();
throw new ClosedByInterruptException();
}
}
currentBuffer = buffers.poll();
}
int remaining = Math.min(arg0.remaining(), currentBuffer.remaining());
for (int i = 0; i < remaining; i++) {
arg0.put(currentBuffer.get());
}
read += remaining;
}
return read;
}
}