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    *   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.http2;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufUtil;
20  import io.netty.buffer.Unpooled;
21  import io.netty.util.internal.StringUtil;
22  
23  import static io.netty.handler.codec.http2.Http2CodecUtil.verifyPadding;
24  import static io.netty.util.internal.ObjectUtil.checkNotNull;
25  
26  /**
27   * The default {@link Http2DataFrame} implementation.
28   */
29  public final class DefaultHttp2DataFrame extends AbstractHttp2StreamFrame implements Http2DataFrame {
30      private final ByteBuf content;
31      private final boolean endStream;
32      private final int padding;
33      private final int initialFlowControlledBytes;
34  
35      /**
36       * Equivalent to {@code new DefaultHttp2DataFrame(content, false)}.
37       *
38       * @param content non-{@code null} payload
39       */
40      public DefaultHttp2DataFrame(ByteBuf content) {
41          this(content, false);
42      }
43  
44      /**
45       * Equivalent to {@code new DefaultHttp2DataFrame(Unpooled.EMPTY_BUFFER, endStream)}.
46       *
47       * @param endStream whether this data should terminate the stream
48       */
49      public DefaultHttp2DataFrame(boolean endStream) {
50          this(Unpooled.EMPTY_BUFFER, endStream);
51      }
52  
53      /**
54       * Equivalent to {@code new DefaultHttp2DataFrame(content, endStream, 0)}.
55       *
56       * @param content non-{@code null} payload
57       * @param endStream whether this data should terminate the stream
58       */
59      public DefaultHttp2DataFrame(ByteBuf content, boolean endStream) {
60          this(content, endStream, 0);
61      }
62  
63      /**
64       * Construct a new data message.
65       *
66       * @param content non-{@code null} payload
67       * @param endStream whether this data should terminate the stream
68       * @param padding additional bytes that should be added to obscure the true content size. Must be between 0 and
69       *                256 (inclusive).
70       */
71      public DefaultHttp2DataFrame(ByteBuf content, boolean endStream, int padding) {
72          this.content = checkNotNull(content, "content");
73          this.endStream = endStream;
74          verifyPadding(padding);
75          this.padding = padding;
76          if (content().readableBytes() + (long) padding > Integer.MAX_VALUE) {
77              throw new IllegalArgumentException("content + padding must be <= Integer.MAX_VALUE");
78          }
79          initialFlowControlledBytes = content().readableBytes() + padding;
80      }
81  
82      @Override
83      public DefaultHttp2DataFrame stream(Http2FrameStream stream) {
84          super.stream(stream);
85          return this;
86      }
87  
88      @Override
89      public String name() {
90          return "DATA";
91      }
92  
93      @Override
94      public boolean isEndStream() {
95          return endStream;
96      }
97  
98      @Override
99      public int padding() {
100         return padding;
101     }
102 
103     @Override
104     public ByteBuf content() {
105         return ByteBufUtil.ensureAccessible(content);
106     }
107 
108     @Override
109     public int initialFlowControlledBytes() {
110         return initialFlowControlledBytes;
111     }
112 
113     @Override
114     public DefaultHttp2DataFrame copy() {
115         return replace(content().copy());
116     }
117 
118     @Override
119     public DefaultHttp2DataFrame duplicate() {
120         return replace(content().duplicate());
121     }
122 
123     @Override
124     public DefaultHttp2DataFrame retainedDuplicate() {
125         return replace(content().retainedDuplicate());
126     }
127 
128     @Override
129     public DefaultHttp2DataFrame replace(ByteBuf content) {
130         return new DefaultHttp2DataFrame(content, endStream, padding);
131     }
132 
133     @Override
134     public int refCnt() {
135         return content.refCnt();
136     }
137 
138     @Override
139     public boolean release() {
140         return content.release();
141     }
142 
143     @Override
144     public boolean release(int decrement) {
145         return content.release(decrement);
146     }
147 
148     @Override
149     public DefaultHttp2DataFrame retain() {
150         content.retain();
151         return this;
152     }
153 
154     @Override
155     public DefaultHttp2DataFrame retain(int increment) {
156         content.retain(increment);
157         return this;
158     }
159 
160     @Override
161     public String toString() {
162         return StringUtil.simpleClassName(this) + "(stream=" + stream() + ", content=" + content
163                + ", endStream=" + endStream + ", padding=" + padding + ')';
164     }
165 
166     @Override
167     public DefaultHttp2DataFrame touch() {
168         content.touch();
169         return this;
170     }
171 
172     @Override
173     public DefaultHttp2DataFrame touch(Object hint) {
174         content.touch(hint);
175         return this;
176     }
177 
178     @Override
179     public boolean equals(Object o) {
180         if (!(o instanceof DefaultHttp2DataFrame)) {
181             return false;
182         }
183         DefaultHttp2DataFrame other = (DefaultHttp2DataFrame) o;
184         return super.equals(other) && content.equals(other.content())
185             && endStream == other.endStream && padding == other.padding;
186     }
187 
188     @Override
189     public int hashCode() {
190         int hash = super.hashCode();
191         hash = hash * 31 + content.hashCode();
192         hash = hash * 31 + (endStream ? 0 : 1);
193         hash = hash * 31 + padding;
194         return hash;
195     }
196 }