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