View Javadoc

1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.websocketx;
17  
18  import io.netty.buffer.ByteBuf;
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 WebSocket chunked transfers.
25   * <p>
26   * Each chunk from the input data will be wrapped within a {@link ContinuationWebSocketFrame}.
27   * At the end of the input data, {@link ContinuationWebSocketFrame} with finalFragment will be written.
28   * <p>
29   */
30  public final class WebSocketChunkedInput implements ChunkedInput<WebSocketFrame> {
31      private final ChunkedInput<ByteBuf> input;
32      private final int rsv;
33  
34      /**
35       * Creates a new instance using the specified input.
36       * @param input {@link ChunkedInput} containing data to write
37       */
38      public WebSocketChunkedInput(ChunkedInput<ByteBuf> input) {
39          this(input, 0);
40      }
41  
42      /**
43       * Creates a new instance using the specified input.
44       * @param input {@link ChunkedInput} containing data to write
45       * @param rsv RSV1, RSV2, RSV3 used for extensions
46       *
47       * @throws  NullPointerException if {@code input} is null
48       */
49      public WebSocketChunkedInput(ChunkedInput<ByteBuf> input, int rsv) {
50          this.input = ObjectUtil.checkNotNull(input, "input");
51          this.rsv = rsv;
52      }
53  
54      /**
55       * @return {@code true} if and only if there is no data left in the stream
56       * and the stream has reached at its end.
57       */
58      @Override
59      public boolean isEndOfInput() throws Exception {
60          return input.isEndOfInput();
61      }
62  
63      /**
64       * Releases the resources associated with the input.
65       */
66      @Override
67      public void close() throws Exception {
68          input.close();
69      }
70  
71      /**
72       * Fetches a chunked data from the stream. Once this method returns the last chunk
73       * and thus the stream has reached at its end, any subsequent {@link #isEndOfInput()}
74       * call must return {@code true}.
75       *
76       * @param ctx {@link ChannelHandlerContext} context of channelHandler
77       * @return {@link WebSocketFrame} contain chunk of data
78       */
79      @Override
80      public WebSocketFrame readChunk(ChannelHandlerContext ctx) throws Exception {
81          ByteBuf buf = input.readChunk(ctx);
82          if (buf == null) {
83              return null;
84          }
85          return new ContinuationWebSocketFrame(input.isEndOfInput(), rsv, buf);
86      }
87  }