查看本类的 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.util.internal.ObjectUtil;
19  
20  /**
21   * The SPDY stream status code and its description.
22   */
23  public class SpdyStreamStatus implements Comparable<SpdyStreamStatus> {
24  
25      /**
26       * 1 Protocol Error
27       */
28      public static final SpdyStreamStatus PROTOCOL_ERROR =
29          new SpdyStreamStatus(1, "PROTOCOL_ERROR");
30  
31      /**
32       * 2 Invalid Stream
33       */
34      public static final SpdyStreamStatus INVALID_STREAM =
35          new SpdyStreamStatus(2, "INVALID_STREAM");
36  
37      /**
38       * 3 Refused Stream
39       */
40      public static final SpdyStreamStatus REFUSED_STREAM =
41          new SpdyStreamStatus(3, "REFUSED_STREAM");
42  
43      /**
44       * 4 Unsupported Version
45       */
46      public static final SpdyStreamStatus UNSUPPORTED_VERSION =
47          new SpdyStreamStatus(4, "UNSUPPORTED_VERSION");
48  
49      /**
50       * 5 Cancel
51       */
52      public static final SpdyStreamStatus CANCEL =
53          new SpdyStreamStatus(5, "CANCEL");
54  
55      /**
56       * 6 Internal Error
57       */
58      public static final SpdyStreamStatus INTERNAL_ERROR =
59          new SpdyStreamStatus(6, "INTERNAL_ERROR");
60  
61      /**
62       * 7 Flow Control Error
63       */
64      public static final SpdyStreamStatus FLOW_CONTROL_ERROR =
65          new SpdyStreamStatus(7, "FLOW_CONTROL_ERROR");
66  
67      /**
68       * 8 Stream In Use
69       */
70      public static final SpdyStreamStatus STREAM_IN_USE =
71          new SpdyStreamStatus(8, "STREAM_IN_USE");
72  
73      /**
74       * 9 Stream Already Closed
75       */
76      public static final SpdyStreamStatus STREAM_ALREADY_CLOSED =
77          new SpdyStreamStatus(9, "STREAM_ALREADY_CLOSED");
78  
79      /**
80       * 10 Invalid Credentials
81       */
82      public static final SpdyStreamStatus INVALID_CREDENTIALS =
83          new SpdyStreamStatus(10, "INVALID_CREDENTIALS");
84  
85      /**
86       * 11 Frame Too Large
87       */
88      public static final SpdyStreamStatus FRAME_TOO_LARGE =
89          new SpdyStreamStatus(11, "FRAME_TOO_LARGE");
90  
91      /**
92       * Returns the {@link SpdyStreamStatus} represented by the specified code.
93       * If the specified code is a defined SPDY status code, a cached instance
94       * will be returned.  Otherwise, a new instance will be returned.
95       */
96      public static SpdyStreamStatus valueOf(int code) {
97          if (code == 0) {
98              throw new IllegalArgumentException(
99                      "0 is not a valid status code for a RST_STREAM");
100         }
101 
102         switch (code) {
103         case 1:
104             return PROTOCOL_ERROR;
105         case 2:
106             return INVALID_STREAM;
107         case 3:
108             return REFUSED_STREAM;
109         case 4:
110             return UNSUPPORTED_VERSION;
111         case 5:
112             return CANCEL;
113         case 6:
114             return INTERNAL_ERROR;
115         case 7:
116             return FLOW_CONTROL_ERROR;
117         case 8:
118             return STREAM_IN_USE;
119         case 9:
120             return STREAM_ALREADY_CLOSED;
121         case 10:
122             return INVALID_CREDENTIALS;
123         case 11:
124             return FRAME_TOO_LARGE;
125         }
126 
127         return new SpdyStreamStatus(code, "UNKNOWN (" + code + ')');
128     }
129 
130     private final int code;
131 
132     private final String statusPhrase;
133 
134     /**
135      * Creates a new instance with the specified {@code code} and its
136      * {@code statusPhrase}.
137      */
138     public SpdyStreamStatus(int code, String statusPhrase) {
139         if (code == 0) {
140             throw new IllegalArgumentException(
141                     "0 is not a valid status code for a RST_STREAM");
142         }
143 
144         this.statusPhrase = ObjectUtil.checkNotNull(statusPhrase, "statusPhrase");
145         this.code = code;
146     }
147 
148     /**
149      * Returns the code of this status.
150      */
151     public int code() {
152         return code;
153     }
154 
155     /**
156      * Returns the status phrase of this status.
157      */
158     public String statusPhrase() {
159         return statusPhrase;
160     }
161 
162     @Override
163     public int hashCode() {
164         return code();
165     }
166 
167     @Override
168     public boolean equals(Object o) {
169         if (!(o instanceof SpdyStreamStatus)) {
170             return false;
171         }
172 
173         return code() == ((SpdyStreamStatus) o).code();
174     }
175 
176     @Override
177     public String toString() {
178         return statusPhrase();
179     }
180 
181     @Override
182     public int compareTo(SpdyStreamStatus o) {
183         return code() - o.code();
184     }
185 }