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    *   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.socks;
17  
18  import io.netty.util.internal.StringUtil;
19  
20  final class SocksCommonUtils {
21      public static final SocksRequest UNKNOWN_SOCKS_REQUEST = new UnknownSocksRequest();
22      public static final SocksResponse UNKNOWN_SOCKS_RESPONSE = new UnknownSocksResponse();
23  
24      /**
25       * A constructor to stop this class being constructed.
26       */
27      private SocksCommonUtils() {
28          // NOOP
29      }
30  
31      private static final char ipv6hextetSeparator = ':';
32  
33      /**
34       * Converts numeric IPv6 to standard (non-compressed) format.
35       */
36      public static String ipv6toStr(byte[] src) {
37          assert src.length == 16;
38          StringBuilder sb = new StringBuilder(39);
39          ipv6toStr(sb, src, 0, 8);
40          return sb.toString();
41      }
42  
43      private static void ipv6toStr(StringBuilder sb, byte[] src, int fromHextet, int toHextet) {
44          int i;
45          toHextet --;
46          for (i = fromHextet; i < toHextet; i++) {
47              appendHextet(sb, src, i);
48              sb.append(ipv6hextetSeparator);
49          }
50  
51          appendHextet(sb, src, i);
52      }
53  
54      private static void appendHextet(StringBuilder sb, byte[] src, int i) {
55          StringUtil.toHexString(sb, src, i << 1, 2);
56      }
57  
58  }