1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.spdy;
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.ObjectUtil;
22 import io.netty.util.internal.StringUtil;
23
24
25
26
27 public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements SpdyDataFrame {
28
29 private final ByteBuf data;
30
31
32
33
34
35
36 public DefaultSpdyDataFrame(int streamId) {
37 this(streamId, Unpooled.buffer(0));
38 }
39
40
41
42
43
44
45
46 public DefaultSpdyDataFrame(int streamId, ByteBuf data) {
47 super(streamId);
48 this.data = validate(
49 ObjectUtil.checkNotNull(data, "data"));
50 }
51
52 private static ByteBuf validate(ByteBuf data) {
53 if (data.readableBytes() > SpdyCodecUtil.SPDY_MAX_LENGTH) {
54 throw new IllegalArgumentException("data payload cannot exceed "
55 + SpdyCodecUtil.SPDY_MAX_LENGTH + " bytes");
56 }
57 return data;
58 }
59
60 @Override
61 public SpdyDataFrame setStreamId(int streamId) {
62 super.setStreamId(streamId);
63 return this;
64 }
65
66 @Override
67 public SpdyDataFrame setLast(boolean last) {
68 super.setLast(last);
69 return this;
70 }
71
72 @Override
73 public ByteBuf content() {
74 return ByteBufUtil.ensureAccessible(data);
75 }
76
77 @Override
78 public SpdyDataFrame copy() {
79 return replace(content().copy());
80 }
81
82 @Override
83 public SpdyDataFrame duplicate() {
84 return replace(content().duplicate());
85 }
86
87 @Override
88 public SpdyDataFrame retainedDuplicate() {
89 return replace(content().retainedDuplicate());
90 }
91
92 @Override
93 public SpdyDataFrame replace(ByteBuf content) {
94 SpdyDataFrame frame = new DefaultSpdyDataFrame(streamId(), content);
95 frame.setLast(isLast());
96 return frame;
97 }
98
99 @Override
100 public int refCnt() {
101 return data.refCnt();
102 }
103
104 @Override
105 public SpdyDataFrame retain() {
106 data.retain();
107 return this;
108 }
109
110 @Override
111 public SpdyDataFrame retain(int increment) {
112 data.retain(increment);
113 return this;
114 }
115
116 @Override
117 public SpdyDataFrame touch() {
118 data.touch();
119 return this;
120 }
121
122 @Override
123 public SpdyDataFrame touch(Object hint) {
124 data.touch(hint);
125 return this;
126 }
127
128 @Override
129 public boolean release() {
130 return data.release();
131 }
132
133 @Override
134 public boolean release(int decrement) {
135 return data.release(decrement);
136 }
137
138 @Override
139 public String toString() {
140 StringBuilder buf = new StringBuilder()
141 .append(StringUtil.simpleClassName(this))
142 .append("(last: ")
143 .append(isLast())
144 .append(')')
145 .append(StringUtil.NEWLINE)
146 .append("--> Stream-ID = ")
147 .append(streamId())
148 .append(StringUtil.NEWLINE)
149 .append("--> Size = ");
150 if (refCnt() == 0) {
151 buf.append("(freed)");
152 } else {
153 buf.append(content().readableBytes());
154 }
155 return buf.toString();
156 }
157 }