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.netty.handler.codec.http2;
16  
17  import io.netty.buffer.ByteBuf;
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.handler.stream.ChunkedInput;
21  import io.netty.util.internal.ObjectUtil;
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<ByteBuf> 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<ByteBuf> input, Http2FrameStream stream) {
59          this.input = ObjectUtil.checkNotNull(input, "input");
60          this.stream = ObjectUtil.checkNotNull(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      @Deprecated
78      @Override
79      public Http2DataFrame readChunk(ChannelHandlerContext ctx) throws Exception {
80          return readChunk(ctx.alloc());
81      }
82  
83      @Override
84      public Http2DataFrame readChunk(ByteBufAllocator allocator) throws Exception {
85          if (endStreamSent) {
86              return null;
87          }
88  
89          if (input.isEndOfInput()) {
90              endStreamSent = true;
91              return new DefaultHttp2DataFrame(true).stream(stream);
92          }
93  
94          ByteBuf buf = input.readChunk(allocator);
95          if (buf == null) {
96              return null;
97          }
98  
99          final Http2DataFrame dataFrame = new DefaultHttp2DataFrame(buf, input.isEndOfInput()).stream(stream);
100         if (dataFrame.isEndStream()) {
101             endStreamSent = true;
102         }
103 
104         return dataFrame;
105     }
106 
107     @Override
108     public long length() {
109         return input.length();
110     }
111 
112     @Override
113     public long progress() {
114         return input.progress();
115     }
116 }