查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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    *   https://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.ByteBufUtil;
20  import io.netty.buffer.Unpooled;
21  import io.netty.util.internal.ObjectUtil;
22  import io.netty.util.internal.StringUtil;
23  
24  /**
25   * The default {@link SpdyDataFrame} implementation.
26   */
27  public class DefaultSpdyDataFrame extends DefaultSpdyStreamFrame implements SpdyDataFrame {
28  
29      private final ByteBuf data;
30  
31      /**
32       * Creates a new instance.
33       *
34       * @param streamId the Stream-ID of this frame
35       */
36      public DefaultSpdyDataFrame(int streamId) {
37          this(streamId, Unpooled.buffer(0));
38      }
39  
40      /**
41       * Creates a new instance.
42       *
43       * @param streamId  the Stream-ID of this frame
44       * @param data      the payload of the frame. Can not exceed {@link SpdyCodecUtil#SPDY_MAX_LENGTH}
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 }