1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.handler.codec.http2;
16
17 import io.netty5.buffer.api.Buffer;
18 import io.netty5.buffer.api.BufferAllocator;
19 import io.netty5.handler.stream.ChunkedInput;
20
21 import static java.util.Objects.requireNonNull;
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 public final class Http2DataChunkedInput implements ChunkedInput<Http2DataFrame> {
47
48 private final ChunkedInput<Buffer> input;
49 private final Http2FrameStream stream;
50 private boolean endStreamSent;
51
52
53
54
55
56
57
58 public Http2DataChunkedInput(ChunkedInput<Buffer> input, Http2FrameStream stream) {
59 this.input = requireNonNull(input, "input");
60 this.stream = requireNonNull(stream, "stream");
61 }
62
63 @Override
64 public boolean isEndOfInput() throws Exception {
65 if (input.isEndOfInput()) {
66
67 return endStreamSent;
68 }
69 return false;
70 }
71
72 @Override
73 public void close() throws Exception {
74 input.close();
75 }
76
77 @Override
78 public Http2DataFrame readChunk(BufferAllocator allocator) throws Exception {
79 if (endStreamSent) {
80 return null;
81 }
82
83 if (input.isEndOfInput()) {
84 endStreamSent = true;
85 return new DefaultHttp2DataFrame(true).stream(stream);
86 }
87
88 Buffer buf = input.readChunk(allocator);
89 if (buf == null) {
90 return null;
91 }
92
93 final Http2DataFrame dataFrame = new DefaultHttp2DataFrame(buf.send(), input.isEndOfInput()).stream(stream);
94 if (dataFrame.isEndStream()) {
95 endStreamSent = true;
96 }
97
98 return dataFrame;
99 }
100
101 @Override
102 public long length() {
103 return input.length();
104 }
105
106 @Override
107 public long progress() {
108 return input.progress();
109 }
110 }