View Javadoc
1   /*
2    * Copyright 2022 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License, version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at:
7    *
8    * https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
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   * A {@link ChunkedInput} that fetches data chunk by chunk for use with HTTP/2 Data Frames.
25   * <p>
26   * Each chunk from the input data will be wrapped within a {@link Http2DataFrame}. At the end of the input data,
27   * {@link Http2DataFrame#isEndStream()} will be set to true and will be written.
28   * <p>
29   * <p>
30   * <pre>
31   *
32   *     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
33   *         if (msg instanceof Http2HeadersFrame) {
34   *             Http2HeadersFrame http2HeadersFrame = (Http2HeadersFrame) msg;
35   *
36   *             Http2HeadersFrame response = new DefaultHttp2HeadersFrame(new DefaultHttp2Headers().status("200"));
37   *             response.stream(http2HeadersFrame.stream());
38   *             ctx.write(response);
39   *
40   *             ChannelFuture sendFileFuture = ctx.writeAndFlush(new Http2DataChunkedInput(
41   *                     new ChunkedFile(new File(("/home/meow/cats.mp4"))), http2HeadersFrame.stream()));
42   *         }
43   *     }
44   * </pre>
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       * Creates a new instance using the specified input.
54       *
55       * @param input  {@link ChunkedInput} containing data to write
56       * @param stream {@link Http2FrameStream} holding stream info
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              // Only end of input after last HTTP chunk has been sent
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 }