查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.buffer.ByteBufAllocator;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.handler.stream.ChunkedInput;
22  import io.netty.util.internal.ObjectUtil;
23  
24  /**
25   * A {@link ChunkedInput} that fetches data chunk by chunk for use with WebSocket chunked transfers.
26   * <p>
27   * Each chunk from the input data will be wrapped within a {@link ContinuationWebSocketFrame}.
28   * At the end of the input data, {@link ContinuationWebSocketFrame} with finalFragment will be written.
29   * <p>
30   */
31  public final class WebSocketChunkedInput implements ChunkedInput<WebSocketFrame> {
32      private final ChunkedInput<ByteBuf> input;
33      private final int rsv;
34  
35      /**
36       * Creates a new instance using the specified input.
37       * @param input {@link ChunkedInput} containing data to write
38       */
39      public WebSocketChunkedInput(ChunkedInput<ByteBuf> input) {
40          this(input, 0);
41      }
42  
43      /**
44       * Creates a new instance using the specified input.
45       * @param input {@link ChunkedInput} containing data to write
46       * @param rsv RSV1, RSV2, RSV3 used for extensions
47       *
48       * @throws  NullPointerException if {@code input} is null
49       */
50      public WebSocketChunkedInput(ChunkedInput<ByteBuf> input, int rsv) {
51          this.input = ObjectUtil.checkNotNull(input, "input");
52          this.rsv = rsv;
53      }
54  
55      /**
56       * @return {@code true} if and only if there is no data left in the stream
57       * and the stream has reached at its end.
58       */
59      @Override
60      public boolean isEndOfInput() throws Exception {
61          return input.isEndOfInput();
62      }
63  
64      /**
65       * Releases the resources associated with the input.
66       */
67      @Override
68      public void close() throws Exception {
69          input.close();
70      }
71  
72      /**
73       * @deprecated Use {@link #readChunk(ByteBufAllocator)}.
74       *
75       * Fetches a chunked data from the stream. Once this method returns the last chunk
76       * and thus the stream has reached at its end, any subsequent {@link #isEndOfInput()}
77       * call must return {@code true}.
78       *
79       * @param ctx {@link ChannelHandlerContext} context of channelHandler
80       * @return {@link WebSocketFrame} contain chunk of data
81       */
82      @Deprecated
83      @Override
84      public WebSocketFrame readChunk(ChannelHandlerContext ctx) throws Exception {
85          return readChunk(ctx.alloc());
86      }
87  
88      /**
89       * Fetches a chunked data from the stream. Once this method returns the last chunk
90       * and thus the stream has reached at its end, any subsequent {@link #isEndOfInput()}
91       * call must return {@code true}.
92       *
93       * @param allocator {@link ByteBufAllocator}
94       * @return {@link WebSocketFrame} contain chunk of data
95       */
96      @Override
97      public WebSocketFrame readChunk(ByteBufAllocator allocator) throws Exception {
98          ByteBuf buf = input.readChunk(allocator);
99          if (buf == null) {
100             return null;
101         }
102         return new ContinuationWebSocketFrame(input.isEndOfInput(), rsv, buf);
103     }
104 
105     @Override
106     public long length() {
107         return input.length();
108     }
109 
110     @Override
111     public long progress() {
112         return input.progress();
113     }
114 }