1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
37
38
39
40 public DefaultHttp2DataFrame(ByteBuf content) {
41 this(content, false);
42 }
43
44
45
46
47
48
49 public DefaultHttp2DataFrame(boolean endStream) {
50 this(Unpooled.EMPTY_BUFFER, endStream);
51 }
52
53
54
55
56
57
58
59 public DefaultHttp2DataFrame(ByteBuf content, boolean endStream) {
60 this(content, endStream, 0);
61 }
62
63
64
65
66
67
68
69
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 }