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 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 }