查看本类的 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 static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
19  
20  import io.netty.util.internal.StringUtil;
21  
22  /**
23   * The default {@link SpdySynStreamFrame} implementation.
24   */
25  public class DefaultSpdySynStreamFrame extends DefaultSpdyHeadersFrame
26          implements SpdySynStreamFrame {
27  
28      private int associatedStreamId;
29      private byte priority;
30      private boolean unidirectional;
31  
32      /**
33       * Creates a new instance.
34       *
35       * @param streamId           the Stream-ID of this frame
36       * @param associatedStreamId the Associated-To-Stream-ID of this frame
37       * @param priority           the priority of the stream
38       */
39      public DefaultSpdySynStreamFrame(int streamId, int associatedStreamId, byte priority) {
40          this(streamId, associatedStreamId, priority, true);
41      }
42  
43      /**
44       * Creates a new instance.
45       *
46       * @param streamId           the Stream-ID of this frame
47       * @param associatedStreamId the Associated-To-Stream-ID of this frame
48       * @param priority           the priority of the stream
49       * @param validateHeaders    validate the header names and values when adding them to the {@link SpdyHeaders}
50       */
51      public DefaultSpdySynStreamFrame(int streamId, int associatedStreamId, byte priority, boolean validateHeaders) {
52          super(streamId, validateHeaders);
53          setAssociatedStreamId(associatedStreamId);
54          setPriority(priority);
55      }
56  
57      @Override
58      public SpdySynStreamFrame setStreamId(int streamId) {
59          super.setStreamId(streamId);
60          return this;
61      }
62  
63      @Override
64      public SpdySynStreamFrame setLast(boolean last) {
65          super.setLast(last);
66          return this;
67      }
68  
69      @Override
70      public SpdySynStreamFrame setInvalid() {
71          super.setInvalid();
72          return this;
73      }
74  
75      @Override
76      public int associatedStreamId() {
77          return associatedStreamId;
78      }
79  
80      @Override
81      public SpdySynStreamFrame setAssociatedStreamId(int associatedStreamId) {
82          checkPositiveOrZero(associatedStreamId, "associatedStreamId");
83          this.associatedStreamId = associatedStreamId;
84          return this;
85      }
86  
87      @Override
88      public byte priority() {
89          return priority;
90      }
91  
92      @Override
93      public SpdySynStreamFrame setPriority(byte priority) {
94          if (priority < 0 || priority > 7) {
95              throw new IllegalArgumentException(
96                      "Priority must be between 0 and 7 inclusive: " + priority);
97          }
98          this.priority = priority;
99          return this;
100     }
101 
102     @Override
103     public boolean isUnidirectional() {
104         return unidirectional;
105     }
106 
107     @Override
108     public SpdySynStreamFrame setUnidirectional(boolean unidirectional) {
109         this.unidirectional = unidirectional;
110         return this;
111     }
112 
113     @Override
114     public String toString() {
115         StringBuilder buf = new StringBuilder()
116             .append(StringUtil.simpleClassName(this))
117             .append("(last: ")
118             .append(isLast())
119             .append("; unidirectional: ")
120             .append(isUnidirectional())
121             .append(')')
122             .append(StringUtil.NEWLINE)
123             .append("--> Stream-ID = ")
124             .append(streamId())
125             .append(StringUtil.NEWLINE);
126         if (associatedStreamId != 0) {
127             buf.append("--> Associated-To-Stream-ID = ")
128                .append(associatedStreamId())
129                .append(StringUtil.NEWLINE);
130         }
131         buf.append("--> Priority = ")
132            .append(priority())
133            .append(StringUtil.NEWLINE)
134            .append("--> Headers:")
135            .append(StringUtil.NEWLINE);
136         appendHeaders(buf);
137 
138         // Remove the last newline.
139         buf.setLength(buf.length() - StringUtil.NEWLINE.length());
140         return buf.toString();
141     }
142 }