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