查看本类的 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  
17  package io.netty.handler.codec.socksx.v5;
18  
19  import io.netty.util.internal.ObjectUtil;
20  
21  /**
22   * The status of {@link Socks5CommandResponse}.
23   */
24  public class Socks5CommandStatus implements Comparable<Socks5CommandStatus> {
25  
26      public static final Socks5CommandStatus SUCCESS = new Socks5CommandStatus(0x00, "SUCCESS");
27      public static final Socks5CommandStatus FAILURE = new Socks5CommandStatus(0x01, "FAILURE");
28      public static final Socks5CommandStatus FORBIDDEN = new Socks5CommandStatus(0x02, "FORBIDDEN");
29      public static final Socks5CommandStatus NETWORK_UNREACHABLE = new Socks5CommandStatus(0x03, "NETWORK_UNREACHABLE");
30      public static final Socks5CommandStatus HOST_UNREACHABLE = new Socks5CommandStatus(0x04, "HOST_UNREACHABLE");
31      public static final Socks5CommandStatus CONNECTION_REFUSED = new Socks5CommandStatus(0x05, "CONNECTION_REFUSED");
32      public static final Socks5CommandStatus TTL_EXPIRED = new Socks5CommandStatus(0x06, "TTL_EXPIRED");
33      public static final Socks5CommandStatus COMMAND_UNSUPPORTED = new Socks5CommandStatus(0x07, "COMMAND_UNSUPPORTED");
34      public static final Socks5CommandStatus ADDRESS_UNSUPPORTED = new Socks5CommandStatus(0x08, "ADDRESS_UNSUPPORTED");
35  
36      public static Socks5CommandStatus valueOf(byte b) {
37          switch (b) {
38          case 0x00:
39              return SUCCESS;
40          case 0x01:
41              return FAILURE;
42          case 0x02:
43              return FORBIDDEN;
44          case 0x03:
45              return NETWORK_UNREACHABLE;
46          case 0x04:
47              return HOST_UNREACHABLE;
48          case 0x05:
49              return CONNECTION_REFUSED;
50          case 0x06:
51              return TTL_EXPIRED;
52          case 0x07:
53              return COMMAND_UNSUPPORTED;
54          case 0x08:
55              return ADDRESS_UNSUPPORTED;
56          }
57  
58          return new Socks5CommandStatus(b);
59      }
60  
61      private final byte byteValue;
62      private final String name;
63      private String text;
64  
65      public Socks5CommandStatus(int byteValue) {
66          this(byteValue, "UNKNOWN");
67      }
68  
69      public Socks5CommandStatus(int byteValue, String name) {
70          this.name = ObjectUtil.checkNotNull(name, "name");
71          this.byteValue = (byte) byteValue;
72      }
73  
74      public byte byteValue() {
75          return byteValue;
76      }
77  
78      public boolean isSuccess() {
79          return byteValue == 0;
80      }
81  
82      @Override
83      public int hashCode() {
84          return byteValue;
85      }
86  
87      @Override
88      public boolean equals(Object obj) {
89          if (!(obj instanceof Socks5CommandStatus)) {
90              return false;
91          }
92  
93          return byteValue == ((Socks5CommandStatus) obj).byteValue;
94      }
95  
96      @Override
97      public int compareTo(Socks5CommandStatus o) {
98          return byteValue - o.byteValue;
99      }
100 
101     @Override
102     public String toString() {
103         String text = this.text;
104         if (text == null) {
105             this.text = text = name + '(' + (byteValue & 0xFF) + ')';
106         }
107         return text;
108     }
109 }