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