View Javadoc
1   /*
2    * Copyright 2013 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    *   http://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.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   * The default {@link SpdyDataFrame} implementation.
25   */
26  public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements SpdyDataFrame {
27  
28      private final ByteBuf data;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param streamId the Stream-ID of this frame
34       */
35      public DefaultSpdyDataFrame(int streamId) {
36          this(streamId, Unpooled.buffer(0));
37      }
38  
39      /**
40       * Creates a new instance.
41       *
42       * @param streamId  the Stream-ID of this frame
43       * @param data      the payload of the frame. Can not exceed {@link SpdyCodecUtil#SPDY_MAX_LENGTH}
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 SpdyDataFrame touch() {
114         data.touch();
115         return this;
116     }
117 
118     @Override
119     public SpdyDataFrame touch(Object hint) {
120         data.touch(hint);
121         return this;
122     }
123 
124     @Override
125     public boolean release() {
126         return data.release();
127     }
128 
129     @Override
130     public boolean release(int decrement) {
131         return data.release(decrement);
132     }
133 
134     @Override
135     public String toString() {
136         StringBuilder buf = new StringBuilder()
137             .append(StringUtil.simpleClassName(this))
138             .append("(last: ")
139             .append(isLast())
140             .append(')')
141             .append(StringUtil.NEWLINE)
142             .append("--> Stream-ID = ")
143             .append(streamId())
144             .append(StringUtil.NEWLINE)
145             .append("--> Size = ");
146         if (refCnt() == 0) {
147             buf.append("(freed)");
148         } else {
149             buf.append(content().readableBytes());
150         }
151         return buf.toString();
152     }
153 }