1
1
package com .google .firebase .internal ;
2
2
3
+ import com .google .api .client .util .StreamingContent ;
4
+
3
5
import java .io .ByteArrayOutputStream ;
4
6
import java .io .IOException ;
5
7
import java .nio .ByteBuffer ;
11
13
import org .apache .hc .core5 .http .nio .AsyncEntityProducer ;
12
14
import org .apache .hc .core5 .http .nio .DataStreamChannel ;
13
15
14
- import com .google .api .client .util .StreamingContent ;
15
-
16
16
@ SuppressWarnings ("deprecation" )
17
17
public class ApacheHttp2AsyncEntityProducer implements AsyncEntityProducer {
18
- private final ByteBuffer bytebuf ;
19
- private ByteArrayOutputStream baos = new ByteArrayOutputStream ();
20
- private final ContentType contentType ;
21
- private final long contentLength ;
22
- private final String contentEncoding ;
23
- private final CompletableFuture <Void > writeFuture ;
24
- private final AtomicReference <Exception > exception ;
25
-
26
- public ApacheHttp2AsyncEntityProducer (final StreamingContent content , final ContentType contentType , String contentEncoding , long contentLength , CompletableFuture <Void > writeFuture ) {
27
- this .writeFuture = writeFuture ;
28
-
29
- if (content != null ) {
30
- try {
31
- content .writeTo (baos );
32
- } catch (IOException e ) {
33
- writeFuture .completeExceptionally (e );
34
- }
35
- }
36
- this .bytebuf = ByteBuffer .wrap (baos .toByteArray ());
37
- this .contentType = contentType ;
38
- this .contentLength = contentLength ;
39
- this .contentEncoding = contentEncoding ;
40
- this .exception = new AtomicReference <>();
41
- }
42
-
43
- public ApacheHttp2AsyncEntityProducer (ApacheHttp2Request request , CompletableFuture <Void > writeFuture ) {
44
- this (
45
- request .getStreamingContent (),
46
- ContentType .parse (request .getContentType ()),
47
- request .getContentEncoding (),
48
- request .getContentLength (),
49
- writeFuture
50
- );
51
- }
52
-
53
- @ Override
54
- public boolean isRepeatable () {
55
- return false ;
56
- }
57
-
58
- @ Override
59
- public String getContentType () {
60
- return contentType != null ? contentType .toString () : null ;
61
- }
62
-
63
- @ Override
64
- public long getContentLength () {
65
- return contentLength ;
66
- }
67
-
68
- @ Override
69
- public int available () {
70
- return Integer .MAX_VALUE ;
18
+ private final ByteBuffer bytebuf ;
19
+ private ByteArrayOutputStream baos = new ByteArrayOutputStream ();
20
+ private final ContentType contentType ;
21
+ private final long contentLength ;
22
+ private final String contentEncoding ;
23
+ private final CompletableFuture <Void > writeFuture ;
24
+ private final AtomicReference <Exception > exception ;
25
+
26
+ public ApacheHttp2AsyncEntityProducer (StreamingContent content , ContentType contentType ,
27
+ String contentEncoding , long contentLength , CompletableFuture <Void > writeFuture ) {
28
+ this .writeFuture = writeFuture ;
29
+
30
+ if (content != null ) {
31
+ try {
32
+ content .writeTo (baos );
33
+ } catch (IOException e ) {
34
+ writeFuture .completeExceptionally (e );
35
+ }
71
36
}
72
-
73
- @ Override
74
- public String getContentEncoding () {
75
- return contentEncoding ;
37
+ this .bytebuf = ByteBuffer .wrap (baos .toByteArray ());
38
+ this .contentType = contentType ;
39
+ this .contentLength = contentLength ;
40
+ this .contentEncoding = contentEncoding ;
41
+ this .exception = new AtomicReference <>();
42
+ }
43
+
44
+ public ApacheHttp2AsyncEntityProducer (ApacheHttp2Request request ,
45
+ CompletableFuture <Void > writeFuture ) {
46
+ this (
47
+ request .getStreamingContent (),
48
+ ContentType .parse (request .getContentType ()),
49
+ request .getContentEncoding (),
50
+ request .getContentLength (),
51
+ writeFuture );
52
+ }
53
+
54
+ @ Override
55
+ public boolean isRepeatable () {
56
+ return false ;
57
+ }
58
+
59
+ @ Override
60
+ public String getContentType () {
61
+ return contentType != null ? contentType .toString () : null ;
62
+ }
63
+
64
+ @ Override
65
+ public long getContentLength () {
66
+ return contentLength ;
67
+ }
68
+
69
+ @ Override
70
+ public int available () {
71
+ return Integer .MAX_VALUE ;
72
+ }
73
+
74
+ @ Override
75
+ public String getContentEncoding () {
76
+ return contentEncoding ;
77
+ }
78
+
79
+ @ Override
80
+ public boolean isChunked () {
81
+ return false ;
82
+ }
83
+
84
+ @ Override
85
+ public Set <String > getTrailerNames () {
86
+ return null ;
87
+ }
88
+
89
+ @ Override
90
+ public void produce (DataStreamChannel channel ) throws IOException {
91
+ if (bytebuf .hasRemaining ()) {
92
+ channel .write (bytebuf );
76
93
}
77
-
78
- @ Override
79
- public boolean isChunked () {
80
- return false ;
94
+ if (!bytebuf .hasRemaining ()) {
95
+ channel .endStream ();
96
+ writeFuture .complete (null );
81
97
}
98
+ }
82
99
83
- @ Override
84
- public Set <String > getTrailerNames () {
85
- return null ;
100
+ @ Override
101
+ public void failed (Exception cause ) {
102
+ if (exception .compareAndSet (null , cause )) {
103
+ releaseResources ();
104
+ writeFuture .completeExceptionally (cause );
86
105
}
106
+ }
87
107
88
- @ Override
89
- public void produce (DataStreamChannel channel ) throws IOException {
90
- if (bytebuf .hasRemaining ()) {
91
- channel .write (bytebuf );
92
- }
93
- if (!bytebuf .hasRemaining ()) {
94
- channel .endStream ();
95
- writeFuture .complete (null );
96
- }
97
- }
108
+ public final Exception getException () {
109
+ return exception .get ();
110
+ }
98
111
99
- @ Override
100
- public void failed (Exception cause ) {
101
- if (exception .compareAndSet (null , cause )) {
102
- releaseResources ();
103
- writeFuture .completeExceptionally (cause );
104
- }
105
- }
106
-
107
- public final Exception getException () {
108
- return exception .get ();
109
- }
110
-
111
- @ Override
112
- public void releaseResources () {
113
- bytebuf .clear ();
114
- }
112
+ @ Override
113
+ public void releaseResources () {
114
+ bytebuf .clear ();
115
+ }
115
116
}
0 commit comments