author | chegar |
Fri, 16 Feb 2018 15:06:29 +0000 | |
branch | http-client-branch |
changeset 56138 | 4f92b988600e |
parent 56092 | fd85b2bf2b0d |
child 56257 | 82a9340bdda6 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http; |
48083 | 27 |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.UncheckedIOException; |
|
33 |
import java.nio.ByteBuffer; |
|
34 |
import java.nio.charset.Charset; |
|
35 |
import java.nio.file.Path; |
|
36 |
import java.security.AccessControlContext; |
|
37 |
import java.security.AccessController; |
|
38 |
import java.security.PrivilegedAction; |
|
39 |
import java.security.PrivilegedActionException; |
|
40 |
import java.security.PrivilegedExceptionAction; |
|
41 |
import java.util.ArrayList; |
|
42 |
import java.util.Collections; |
|
43 |
import java.util.Iterator; |
|
44 |
import java.util.List; |
|
45 |
import java.util.NoSuchElementException; |
|
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
46 |
import java.util.Objects; |
48083 | 47 |
import java.util.concurrent.ConcurrentLinkedQueue; |
48 |
import java.util.concurrent.Flow; |
|
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
49 |
import java.util.concurrent.Flow.Publisher; |
48083 | 50 |
import java.util.function.Supplier; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56080
diff
changeset
|
51 |
import java.net.http.HttpRequest.BodyPublisher; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
52 |
import jdk.internal.net.http.common.Utils; |
48083 | 53 |
|
56080
64846522c0d5
http-client-branch: add several private <init> methods
chegar
parents:
56079
diff
changeset
|
54 |
public final class RequestPublishers { |
64846522c0d5
http-client-branch: add several private <init> methods
chegar
parents:
56079
diff
changeset
|
55 |
|
64846522c0d5
http-client-branch: add several private <init> methods
chegar
parents:
56079
diff
changeset
|
56 |
private RequestPublishers() { } |
48083 | 57 |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
58 |
public static class ByteArrayPublisher implements BodyPublisher { |
48083 | 59 |
private volatile Flow.Publisher<ByteBuffer> delegate; |
60 |
private final int length; |
|
61 |
private final byte[] content; |
|
62 |
private final int offset; |
|
63 |
private final int bufSize; |
|
64 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
65 |
public ByteArrayPublisher(byte[] content) { |
48083 | 66 |
this(content, 0, content.length); |
67 |
} |
|
68 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
69 |
public ByteArrayPublisher(byte[] content, int offset, int length) { |
48083 | 70 |
this(content, offset, length, Utils.BUFSIZE); |
71 |
} |
|
72 |
||
73 |
/* bufSize exposed for testing purposes */ |
|
74 |
ByteArrayPublisher(byte[] content, int offset, int length, int bufSize) { |
|
75 |
this.content = content; |
|
76 |
this.offset = offset; |
|
77 |
this.length = length; |
|
78 |
this.bufSize = bufSize; |
|
79 |
} |
|
80 |
||
81 |
List<ByteBuffer> copy(byte[] content, int offset, int length) { |
|
82 |
List<ByteBuffer> bufs = new ArrayList<>(); |
|
83 |
while (length > 0) { |
|
84 |
ByteBuffer b = ByteBuffer.allocate(Math.min(bufSize, length)); |
|
85 |
int max = b.capacity(); |
|
86 |
int tocopy = Math.min(max, length); |
|
87 |
b.put(content, offset, tocopy); |
|
88 |
offset += tocopy; |
|
89 |
length -= tocopy; |
|
90 |
b.flip(); |
|
91 |
bufs.add(b); |
|
92 |
} |
|
93 |
return bufs; |
|
94 |
} |
|
95 |
||
96 |
@Override |
|
97 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
|
98 |
List<ByteBuffer> copy = copy(content, offset, length); |
|
99 |
this.delegate = new PullPublisher<>(copy); |
|
100 |
delegate.subscribe(subscriber); |
|
101 |
} |
|
102 |
||
103 |
@Override |
|
104 |
public long contentLength() { |
|
105 |
return length; |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
// This implementation has lots of room for improvement. |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
110 |
public static class IterablePublisher implements BodyPublisher { |
48083 | 111 |
private volatile Flow.Publisher<ByteBuffer> delegate; |
112 |
private final Iterable<byte[]> content; |
|
113 |
private volatile long contentLength; |
|
114 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
115 |
public IterablePublisher(Iterable<byte[]> content) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
116 |
this.content = Objects.requireNonNull(content); |
48083 | 117 |
} |
118 |
||
119 |
// The ByteBufferIterator will iterate over the byte[] arrays in |
|
120 |
// the content one at the time. |
|
121 |
// |
|
122 |
class ByteBufferIterator implements Iterator<ByteBuffer> { |
|
123 |
final ConcurrentLinkedQueue<ByteBuffer> buffers = new ConcurrentLinkedQueue<>(); |
|
124 |
final Iterator<byte[]> iterator = content.iterator(); |
|
125 |
@Override |
|
126 |
public boolean hasNext() { |
|
127 |
return !buffers.isEmpty() || iterator.hasNext(); |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public ByteBuffer next() { |
|
132 |
ByteBuffer buffer = buffers.poll(); |
|
133 |
while (buffer == null) { |
|
134 |
copy(); |
|
135 |
buffer = buffers.poll(); |
|
136 |
} |
|
137 |
return buffer; |
|
138 |
} |
|
139 |
||
140 |
ByteBuffer getBuffer() { |
|
141 |
return Utils.getBuffer(); |
|
142 |
} |
|
143 |
||
144 |
void copy() { |
|
145 |
byte[] bytes = iterator.next(); |
|
146 |
int length = bytes.length; |
|
147 |
if (length == 0 && iterator.hasNext()) { |
|
148 |
// avoid inserting empty buffers, except |
|
149 |
// if that's the last. |
|
150 |
return; |
|
151 |
} |
|
152 |
int offset = 0; |
|
153 |
do { |
|
154 |
ByteBuffer b = getBuffer(); |
|
155 |
int max = b.capacity(); |
|
156 |
||
157 |
int tocopy = Math.min(max, length); |
|
158 |
b.put(bytes, offset, tocopy); |
|
159 |
offset += tocopy; |
|
160 |
length -= tocopy; |
|
161 |
b.flip(); |
|
162 |
buffers.add(b); |
|
163 |
} while (length > 0); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
public Iterator<ByteBuffer> iterator() { |
|
168 |
return new ByteBufferIterator(); |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
|
173 |
Iterable<ByteBuffer> iterable = this::iterator; |
|
174 |
this.delegate = new PullPublisher<>(iterable); |
|
175 |
delegate.subscribe(subscriber); |
|
176 |
} |
|
177 |
||
178 |
static long computeLength(Iterable<byte[]> bytes) { |
|
179 |
long len = 0; |
|
180 |
for (byte[] b : bytes) { |
|
181 |
len = Math.addExact(len, (long)b.length); |
|
182 |
} |
|
183 |
return len; |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
187 |
public long contentLength() { |
|
188 |
if (contentLength == 0) { |
|
189 |
synchronized(this) { |
|
190 |
if (contentLength == 0) { |
|
191 |
contentLength = computeLength(content); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
return contentLength; |
|
196 |
} |
|
197 |
} |
|
198 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
199 |
public static class StringPublisher extends ByteArrayPublisher { |
48083 | 200 |
public StringPublisher(String content, Charset charset) { |
201 |
super(content.getBytes(charset)); |
|
202 |
} |
|
203 |
} |
|
204 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
205 |
public static class EmptyPublisher implements BodyPublisher { |
48083 | 206 |
private final Flow.Publisher<ByteBuffer> delegate = |
207 |
new PullPublisher<ByteBuffer>(Collections.emptyList(), null); |
|
208 |
||
209 |
@Override |
|
210 |
public long contentLength() { |
|
211 |
return 0; |
|
212 |
} |
|
213 |
||
214 |
@Override |
|
215 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
|
216 |
delegate.subscribe(subscriber); |
|
217 |
} |
|
218 |
} |
|
219 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
220 |
public static class FilePublisher implements BodyPublisher { |
48083 | 221 |
private final File file; |
222 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
223 |
public FilePublisher(Path name) { |
48083 | 224 |
file = name.toFile(); |
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
|
229 |
InputStream is; |
|
230 |
try { |
|
231 |
PrivilegedExceptionAction<FileInputStream> pa = |
|
232 |
() -> new FileInputStream(file); |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56092
diff
changeset
|
233 |
is = AccessController.doPrivileged(pa); |
48083 | 234 |
} catch (PrivilegedActionException pae) { |
235 |
throw new UncheckedIOException((IOException)pae.getCause()); |
|
236 |
} |
|
237 |
PullPublisher<ByteBuffer> publisher = |
|
238 |
new PullPublisher<>(() -> new StreamIterator(is)); |
|
239 |
publisher.subscribe(subscriber); |
|
240 |
} |
|
241 |
||
242 |
@Override |
|
243 |
public long contentLength() { |
|
244 |
PrivilegedAction<Long> pa = () -> file.length(); |
|
56138
4f92b988600e
http-client-branch: HTTP Client file publishers, handlers, and subscribers as capability objects
chegar
parents:
56092
diff
changeset
|
245 |
return AccessController.doPrivileged(pa); |
48083 | 246 |
} |
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Reads one buffer ahead all the time, blocking in hasNext() |
|
251 |
*/ |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
252 |
public static class StreamIterator implements Iterator<ByteBuffer> { |
48083 | 253 |
final InputStream is; |
254 |
final Supplier<? extends ByteBuffer> bufSupplier; |
|
255 |
volatile ByteBuffer nextBuffer; |
|
256 |
volatile boolean need2Read = true; |
|
257 |
volatile boolean haveNext; |
|
258 |
||
259 |
StreamIterator(InputStream is) { |
|
260 |
this(is, Utils::getBuffer); |
|
261 |
} |
|
262 |
||
263 |
StreamIterator(InputStream is, Supplier<? extends ByteBuffer> bufSupplier) { |
|
264 |
this.is = is; |
|
265 |
this.bufSupplier = bufSupplier; |
|
266 |
} |
|
267 |
||
268 |
// Throwable error() { |
|
269 |
// return error; |
|
270 |
// } |
|
271 |
||
272 |
private int read() { |
|
273 |
nextBuffer = bufSupplier.get(); |
|
274 |
nextBuffer.clear(); |
|
275 |
byte[] buf = nextBuffer.array(); |
|
276 |
int offset = nextBuffer.arrayOffset(); |
|
277 |
int cap = nextBuffer.capacity(); |
|
278 |
try { |
|
279 |
int n = is.read(buf, offset, cap); |
|
280 |
if (n == -1) { |
|
281 |
is.close(); |
|
282 |
return -1; |
|
283 |
} |
|
284 |
//flip |
|
285 |
nextBuffer.limit(n); |
|
286 |
nextBuffer.position(0); |
|
287 |
return n; |
|
288 |
} catch (IOException ex) { |
|
289 |
return -1; |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
@Override |
|
294 |
public synchronized boolean hasNext() { |
|
295 |
if (need2Read) { |
|
296 |
haveNext = read() != -1; |
|
297 |
if (haveNext) { |
|
298 |
need2Read = false; |
|
299 |
} |
|
300 |
return haveNext; |
|
301 |
} |
|
302 |
return haveNext; |
|
303 |
} |
|
304 |
||
305 |
@Override |
|
306 |
public synchronized ByteBuffer next() { |
|
307 |
if (!hasNext()) { |
|
308 |
throw new NoSuchElementException(); |
|
309 |
} |
|
310 |
need2Read = true; |
|
311 |
return nextBuffer; |
|
312 |
} |
|
313 |
||
314 |
} |
|
315 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
316 |
public static class InputStreamPublisher implements BodyPublisher { |
48083 | 317 |
private final Supplier<? extends InputStream> streamSupplier; |
318 |
||
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
319 |
public InputStreamPublisher(Supplier<? extends InputStream> streamSupplier) { |
48379
5382baab8371
8193698: Null handling in BodyPublisher, BodyHandler, and BodySubscriber convenience static factory methods
chegar
parents:
48083
diff
changeset
|
320 |
this.streamSupplier = Objects.requireNonNull(streamSupplier); |
48083 | 321 |
} |
322 |
||
323 |
@Override |
|
324 |
public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
|
325 |
PullPublisher<ByteBuffer> publisher; |
|
326 |
InputStream is = streamSupplier.get(); |
|
327 |
if (is == null) { |
|
328 |
Throwable t = new IOException("streamSupplier returned null"); |
|
329 |
publisher = new PullPublisher<>(null, t); |
|
330 |
} else { |
|
331 |
publisher = new PullPublisher<>(iterableOf(is), null); |
|
332 |
} |
|
333 |
publisher.subscribe(subscriber); |
|
334 |
} |
|
335 |
||
336 |
protected Iterable<ByteBuffer> iterableOf(InputStream is) { |
|
337 |
return () -> new StreamIterator(is); |
|
338 |
} |
|
339 |
||
340 |
@Override |
|
341 |
public long contentLength() { |
|
342 |
return -1; |
|
343 |
} |
|
344 |
} |
|
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
345 |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
346 |
public static final class PublisherAdapter implements BodyPublisher { |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
347 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
348 |
private final Publisher<? extends ByteBuffer> publisher; |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
349 |
private final long contentLength; |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
350 |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56008
diff
changeset
|
351 |
public PublisherAdapter(Publisher<? extends ByteBuffer> publisher, |
48408
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
352 |
long contentLength) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
353 |
this.publisher = Objects.requireNonNull(publisher); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
354 |
this.contentLength = contentLength; |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
355 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
356 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
357 |
@Override |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
358 |
public final long contentLength() { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
359 |
return contentLength; |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
360 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
361 |
|
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
362 |
@Override |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
363 |
public final void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) { |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
364 |
publisher.subscribe(subscriber); |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
365 |
} |
4f830b447edf
8193365: Improve interoperability between HTTP Client's BodyPublisher/BodySubscriber and Flow.Subscriber/Publisher
chegar
parents:
48379
diff
changeset
|
366 |
} |
48083 | 367 |
} |