1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29 private SocksCommonUtils() {
30
31 }
32
33 private static final char ipv6hextetSeparator = ':';
34
35
36
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 }