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.netty5.handler.codec.http.websocketx;
17
18 import io.netty5.buffer.api.Buffer;
19 import io.netty5.buffer.api.BufferAllocator;
20 import io.netty5.handler.stream.ChunkedInput;
21
22 import static java.util.Objects.requireNonNull;
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<Buffer> 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<Buffer> 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<Buffer> input, int rsv) {
51 this.input = requireNonNull(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 * Fetches a chunked data from the stream. Once this method returns the last chunk
74 * and thus the stream has reached at its end, any subsequent {@link #isEndOfInput()}
75 * call must return {@code true}.
76 *
77 * @param allocator {@link BufferAllocator}
78 * @return {@link WebSocketFrame} contain chunk of data
79 */
80 @Override
81 public WebSocketFrame readChunk(BufferAllocator allocator) throws Exception {
82 Buffer buf = input.readChunk(allocator);
83 if (buf == null) {
84 return null;
85 }
86 return new ContinuationWebSocketFrame(input.isEndOfInput(), rsv, buf);
87 }
88
89 @Override
90 public long length() {
91 return input.length();
92 }
93
94 @Override
95 public long progress() {
96 return input.progress();
97 }
98 }