View Javadoc
1   /*
2    * Copyright 2012 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  package io.netty.handler.codec.socksx.v4;
17  
18  /**
19   * The status of {@link Socks4CommandResponse}.
20   */
21  public class Socks4CommandStatus implements Comparable<Socks4CommandStatus> {
22  
23      public static final Socks4CommandStatus SUCCESS = new Socks4CommandStatus(0x5a, "SUCCESS");
24      public static final Socks4CommandStatus REJECTED_OR_FAILED = new Socks4CommandStatus(0x5b, "REJECTED_OR_FAILED");
25      public static final Socks4CommandStatus IDENTD_UNREACHABLE = new Socks4CommandStatus(0x5c, "IDENTD_UNREACHABLE");
26      public static final Socks4CommandStatus IDENTD_AUTH_FAILURE = new Socks4CommandStatus(0x5d, "IDENTD_AUTH_FAILURE");
27  
28      public static Socks4CommandStatus valueOf(byte b) {
29          switch (b) {
30          case 0x5a:
31              return SUCCESS;
32          case 0x5b:
33              return REJECTED_OR_FAILED;
34          case 0x5c:
35              return IDENTD_UNREACHABLE;
36          case 0x5d:
37              return IDENTD_AUTH_FAILURE;
38          }
39  
40          return new Socks4CommandStatus(b);
41      }
42  
43      private final byte byteValue;
44      private final String name;
45      private String text;
46  
47      public Socks4CommandStatus(int byteValue) {
48          this(byteValue, "UNKNOWN");
49      }
50  
51      public Socks4CommandStatus(int byteValue, String name) {
52          if (name == null) {
53              throw new NullPointerException("name");
54          }
55  
56          this.byteValue = (byte) byteValue;
57          this.name = name;
58      }
59  
60      public byte byteValue() {
61          return byteValue;
62      }
63  
64      public boolean isSuccess() {
65          return byteValue == 0x5a;
66      }
67  
68      @Override
69      public int hashCode() {
70          return byteValue;
71      }
72  
73      @Override
74      public boolean equals(Object obj) {
75          if (!(obj instanceof Socks4CommandStatus)) {
76              return false;
77          }
78  
79          return byteValue == ((Socks4CommandStatus) obj).byteValue;
80      }
81  
82      @Override
83      public int compareTo(Socks4CommandStatus o) {
84          return byteValue - o.byteValue;
85      }
86  
87      @Override
88      public String toString() {
89          String text = this.text;
90          if (text == null) {
91              this.text = text = name + '(' + (byteValue & 0xFF) + ')';
92          }
93          return text;
94      }
95  }