1 /* |
1 /* |
2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.IOException; |
|
25 import java.io.InputStream; |
24 import java.io.InputStream; |
26 import java.io.InputStreamReader; |
25 import java.io.InputStreamReader; |
27 import java.io.Reader; |
26 import java.io.Reader; |
28 import java.net.URI; |
27 import java.net.URI; |
29 import jdk.incubator.http.HttpClient; |
28 import java.net.http.HttpClient; |
30 import jdk.incubator.http.HttpHeaders; |
29 import java.net.http.HttpHeaders; |
31 import jdk.incubator.http.HttpRequest; |
30 import java.net.http.HttpRequest; |
32 import jdk.incubator.http.HttpResponse; |
31 import java.net.http.HttpResponse; |
33 import java.nio.ByteBuffer; |
32 import java.net.http.HttpResponse.BodyHandlers; |
34 import java.nio.charset.Charset; |
33 import java.nio.charset.Charset; |
35 import java.util.ArrayList; |
|
36 import java.util.Iterator; |
|
37 import java.util.List; |
|
38 import java.util.Locale; |
34 import java.util.Locale; |
39 import java.util.Optional; |
35 import java.util.Optional; |
40 import java.util.concurrent.ArrayBlockingQueue; |
|
41 import java.util.concurrent.BlockingQueue; |
|
42 import java.util.concurrent.CompletableFuture; |
36 import java.util.concurrent.CompletableFuture; |
43 import java.util.concurrent.CompletionStage; |
|
44 import java.util.concurrent.Flow; |
|
45 import java.util.stream.Stream; |
37 import java.util.stream.Stream; |
46 import static java.lang.System.err; |
38 import static java.lang.System.err; |
47 |
39 |
48 /* |
40 /* |
49 * @test |
41 * @test |
93 .build(); |
85 .build(); |
94 |
86 |
95 // This example shows how to return an InputStream that can be used to |
87 // This example shows how to return an InputStream that can be used to |
96 // start reading the response body before the response is fully received. |
88 // start reading the response body before the response is fully received. |
97 // In comparison, the snipet below (which uses |
89 // In comparison, the snipet below (which uses |
98 // HttpResponse.BodyHandler.asString()) obviously will not return before the |
90 // HttpResponse.BodyHandlers.ofString()) obviously will not return before the |
99 // response body is fully read: |
91 // response body is fully read: |
100 // |
92 // |
101 // System.out.println( |
93 // System.out.println( |
102 // client.sendAsync(request, HttpResponse.BodyHandler.asString()).get().body()); |
94 // client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).get().body()); |
103 |
95 |
104 CompletableFuture<HttpResponse<InputStream>> handle = |
96 CompletableFuture<HttpResponse<InputStream>> handle = |
105 client.sendAsync(request, HttpResponse.BodyHandler.asInputStream()); |
97 client.sendAsync(request, BodyHandlers.ofInputStream()); |
106 if (DEBUG) err.println("Request sent"); |
98 if (DEBUG) err.println("Request sent"); |
107 |
99 |
108 HttpResponse<InputStream> pending = handle.get(); |
100 HttpResponse<InputStream> pending = handle.get(); |
109 |
101 |
110 // At this point, the response headers have been received, but the |
102 // At this point, the response headers have been received, but the |