Ipv4 String 转换到byte[]
512782
private static byte[] ip4StringToBytes(String ipv4Str) {
		if (ipv4Str == null) {
			return null;
		}

		List<String> it = MoreStringUtil.split(ipv4Str, '.', 4);
		if (it.size() != 4) {
			return null;
		}

		byte[] byteAddress = new byte[4];
		for (int i = 0; i < 4; i++) {
			int tempInt = Integer.parseInt(it.get(i));
			if (tempInt > 255) {
				return null;
			}
			byteAddress[i] = (byte) tempInt;
		}
		return byteAddress;
	}
969276
public static long bytesToLong(byte[] b)
    {
        long l = ((long) b[0] << 56) & 0xFF00000000000000L;
        // 如果不强制转换为long，那么默认会当作int，导致最高32位丢失
        l |= ((long) b[1] << 48) & 0xFF000000000000L;
        l |= ((long) b[2] << 40) & 0xFF0000000000L;
        l |= ((long) b[3] << 32) & 0xFF00000000L;
        l |= ((long) b[4] << 24) & 0xFF000000L;
        l |= ((long) b[5] << 16) & 0xFF0000L;
        l |= ((long) b[6] << 8) & 0xFF00L;
        l |= (long) b[7] & 0xFFL;
        return l;
    }
100775
public static int readLen(byte[] data) {
        if (data.length < Datagram.HEADER_LEN) {
            log.warn("要读取长度的数据报报头不完整");
            return -1;
        } else {
            return convert(data, Datagram.LEN_OFFSET);
        }
    }
6751
public static String longToIpv4(long longIP) {
		final StringBuilder sb = new StringBuilder();
		// 直接右移24位
		sb.append(String.valueOf(longIP >>> 24));
		sb.append(".");
		// 将高8位置0，然后右移16位
		sb.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16));
		sb.append(".");
		sb.append(String.valueOf((longIP & 0x0000FFFF) >>> 8));
		sb.append(".");
		sb.append(String.valueOf(longIP & 0x000000FF));
		return sb.toString();
	}
284504
private void parseIPv4(String name) throws IOException {

        // Parse name into byte-value address components
        int slashNdx = name.indexOf('/');
        if (slashNdx == -1) {
            address = InetAddress.getByName(name).getAddress();
        } else {
            address = new byte[8];

            // parse mask
            byte[] mask = InetAddress.getByName
                (name.substring(slashNdx+1)).getAddress();

            // parse base address
            byte[] host = InetAddress.getByName
                (name.substring(0, slashNdx)).getAddress();

            System.arraycopy(host, 0, address, 0, 4);
            System.arraycopy(mask, 0, address, 4, 4);
        }
    }
461953
@Nullable
    static byte[] ipv6ToIpv4Address(Inet6Address address) {
        final byte[] addr = address.getAddress();
        assert addr.length == 16
                : "the length of " + address.getClass().getSimpleName() + ": " + addr.length;
        // The first 10 bytes should be 0.
        for (int i = 0; i < 10; i++) {
            if (addr[i] != 0x00) {
                return null;
            }
        }

        if (addr[10] == 0x00 && addr[11] == 0x00) {
            if (addr[12] == 0x00 && addr[13] == 0x00 && addr[14] == 0x00 && addr[15] == 0x01) {
                return localhost;
            }
            return new byte[] { addr[12], addr[13], addr[14], addr[15] };
        }

        if (addr[10] == (byte) 0xFF && addr[11] == (byte) 0xFF) {
            return new byte[] { addr[12], addr[13], addr[14], addr[15] };
        }

        return null;
    }
442811
public static byte[] bits2Bytes(String bits) {
        int lenMod  = bits.length() % 8;
        int byteLen = bits.length() / 8;
        // 不是8的倍数前面补0
        if (lenMod != 0) {
            for (int i = lenMod; i < 8; i++) {
                bits = "0" + bits;
            }
            byteLen++;
        }
        byte[] bytes = new byte[byteLen];
        for (int i = 0; i < byteLen; ++i) {
            for (int j = 0; j < 8; ++j) {
                bytes[i] <<= 1;
                bytes[i] |= bits.charAt(i * 8 + j) - '0';
            }
        }
        return bytes;
    }
1210632
function (byteArray) {
                var strArray = [];
                var i = 0;
                var l = byteArray.length;

                while (i < l) {
                    var strLength = byteArray[i++];
                    var str = '';

                    while (strLength-- > 0 && i < l) {
                        str += String.fromCharCode(byteArray[i++]);
                    }
                    // 这里需要将unicode转换成js编码
                    str = stringify(str);
                    strArray.push(str);
                }

                return strArray;
            }
70771
function innerDecode(byteBuffer, readBody, deCompressBody) {
  const msgExt = new Message();
  // 消息ID
  byteBufferMsgId.reset();
  // 1 TOTALSIZE
  msgExt.storeSize = byteBuffer.getInt();
  // 2 MAGICCODE
  byteBuffer.getInt();
  // 3 BODYCRC
  msgExt.bodyCRC = byteBuffer.getInt();
  // 4 QUEUEID
  msgExt.queueId = byteBuffer.getInt();
  // 5 FLAG
  msgExt.flag = byteBuffer.getInt();
  // 6 QUEUEOFFSET
  msgExt.queueOffset = byteBuffer.getLong().toNumber();
  // 7 PHYSICALOFFSET
  msgExt.commitLogOffset = byteBuffer.getLong().toNumber();
  // 8 SYSFLAG
  const sysFlag = byteBuffer.getInt();
  msgExt.sysFlag = sysFlag;
  // 9 BORNTIMESTAMP
  msgExt.bornTimestamp = byteBuffer.getLong().toNumber();
  // 10 BORNHOST
  const host = Buffer.alloc(4);
  byteBuffer.get(host);
  let port = byteBuffer.getInt();
  msgExt.bornHost = host[0] + '.' + host[1] + '.' + host[2] + '.' + host[3] + ':' + port;
  // 11 STORETIMESTAMP
  msgExt.storeTimestamp = byteBuffer.getLong().toNumber();
  // 12 STOREHOST
  host.fill(0);
  byteBuffer.get(host);
  port = byteBuffer.getInt();
  msgExt.storeHost = host[0] + '.' + host[1] + '.' + host[2] + '.' + host[3] + ':' + port;
  byteBufferMsgId.put(host);
  byteBufferMsgId.putInt(port);
  byteBufferMsgId.putLong(msgExt.commitLogOffset);
  // 13 RECONSUMETIMES
  msgExt.reconsumeTimes = byteBuffer.getInt();
  // 14 Prepared Transaction Offset
  msgExt.preparedTransactionOffset = byteBuffer.getLong().toNumber();
  // 15 BODY
  const bodyLen = byteBuffer.getInt();
  if (bodyLen > 0) {
    if (readBody) {
      let body = Buffer.alloc(bodyLen);
      byteBuffer.get(body);

      // uncompress body
      if (deCompressBody && (sysFlag & MessageSysFlag.CompressedFlag) === MessageSysFlag.CompressedFlag) {
        body = utils.uncompress(body);
      }
      msgExt.body = body;
    } else {
      byteBuffer.position(byteBuffer.position() + bodyLen);
    }
  }

  // 16 TOPIC
  const topicLen = byteBuffer.get();
  const topic = Buffer.alloc(topicLen);
  byteBuffer.get(topic);
  msgExt.topic = topic.toString();

  // 17 properties
  const propertiesLength = byteBuffer.getShort();
  if (propertiesLength > 0) {
    const properties = Buffer.alloc(propertiesLength);
    byteBuffer.get(properties);
    const propertiesString = properties.toString();
    msgExt.properties = string2messageProperties(propertiesString);
  }

  msgExt.msgId = bytes2string(byteBufferMsgId.copy());
  return msgExt;
}
457314
static byte[] ip2ByteArray(String ip) {
        boolean ipv6Expected = false;
        if (ip.charAt(0) == '[') {
            // This is supposed to be an IPv6 literal
            if (ip.length() > 2 && ip.charAt(ip.length() - 1) == ']') {
                ip = ip.substring(1, ip.length() - 1);
                ipv6Expected = true;
            } else {
                // This was supposed to be a IPv6 address, but it's not!
                throw new IllegalArgumentException(ip + ": invalid IPv6 address");
            }
        }

        if (Character.digit(ip.charAt(0), 16) != -1 || (ip.charAt(0) == ':')) {
            // see if it is IPv4 address
            byte[] address = IPAddressUtil.textToNumericFormatV4(ip);
            if (address != null) return address;

            // see if it is IPv6 address
            // Check if a numeric or string zone id is present
            address = IPAddressUtil.textToNumericFormatV6(ip);
            if (address != null) return address;


            if (ipv6Expected) {
                throw new IllegalArgumentException(ip + ": invalid IPv6 address");
            } else {
                throw new IllegalArgumentException(ip + ": invalid IP address");
            }
        } else {
            throw new IllegalArgumentException(ip + ": invalid IP address");
        }
    }
654064
public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
        SimplePacket helloPacket  = (SimplePacket) packet;
        byte[] body = helloPacket.getBody();
        int bodyLen = 0;
        if(body != null){
            bodyLen = body.length;
        }

        //Bytebuffer总长度 = 消息头+消息体的长度
        int allLen = SimplePacket.HEADER_LENGTH+bodyLen;
        //创建新的Bytebuffer
        ByteBuffer buf = ByteBuffer.allocate(allLen);
        //设置字节序
        buf.order(groupContext.getByteOrder());
        //写入消息头
        buf.putInt(bodyLen);
        //写入消息体
        if(body != null){
            buf.put(body);
        }
        return buf;
    }
1075706
private static byte[] getBytes(String string, Charset charset) {
        if (string == null) {
            return null;
        }
        return string.getBytes(charset);
    }
1113651
private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request)
            throws UnsupportedEncodingException {
        int len = 6;
        InetSocketAddress adr = request.getEndpointAddress();
        byte addressType = 0;
        byte[] host = null;
        
        if (adr != null && !adr.isUnresolved()) {
            if (adr.getAddress() instanceof Inet6Address) {
                len += 16;
                addressType = SocksProxyConstants.IPV6_ADDRESS_TYPE;
            } else if (adr.getAddress() instanceof Inet4Address) {
                len += 4;
                addressType = SocksProxyConstants.IPV4_ADDRESS_TYPE;
            }
        } else {
            host = request.getHost() != null ? 
                    request.getHost().getBytes("ASCII") : null;

            if (host != null) {
                len += 1 + host.length;
                addressType = SocksProxyConstants.DOMAIN_NAME_ADDRESS_TYPE;
            } else {
                throw new IllegalArgumentException("SocksProxyRequest object " +
                        "has no suitable endpoint information");
            }
        }
        
        IoBuffer buf = IoBuffer.allocate(len);

        buf.put(request.getProtocolVersion());
        buf.put(request.getCommandCode());
        buf.put((byte) 0x00); // Reserved
        buf.put(addressType);

        if (host == null) {
            buf.put(request.getIpAddress());
        } else {
            buf.put((byte) host.length);
            buf.put(host);            
        }

        buf.put(request.getPort());

        return buf;
    }
602863
private byte[] encrypt(byte[] in, int inOff, int inLen) {
		// 加密数据
		byte[] c2 = new byte[inLen];
		System.arraycopy(in, inOff, c2, 0, c2.length);

		final ECMultiplier multiplier = createBasePointMultiplier();

		byte[] c1;
		ECPoint kPB;
		BigInteger k;
		do {
			k = nextK();
			// 产生随机数计算出曲线点C1
			c1 = multiplier.multiply(ecParams.getG(), k).normalize().getEncoded(false);
			kPB = ((ECPublicKeyParameters) ecKey).getQ().multiply(k).normalize();
			kdf(kPB, c2);
		} while (notEncrypted(c2, in, inOff));

		// 杂凑值，效验数据
		byte[] c3 = new byte[digest.getDigestSize()];

		addFieldElement(kPB.getAffineXCoord());
		this.digest.update(in, inOff, inLen);
		addFieldElement(kPB.getAffineYCoord());

		this.digest.doFinal(c3, 0);

		// 按照对应模式输出结果
		switch (mode) {
		case C1C3C2:
			return Arrays.concatenate(c1, c3, c2);
		default:
			return Arrays.concatenate(c1, c2, c3);
		}
	}
1282654
public static byte[] decodeHex(final char[] hexDatas) {
        if (hexDatas.length % 2 != 0) {
            throw new IllegalArgumentException("非法的16进制字符");
        }
        byte[] data = new byte[hexDatas.length >> 1];
        for (int i = 0; i < hexDatas.length; i += 2) {
            data[i >> 1] = (byte) ((indexAt((byte) hexDatas[i]) << 4)
                                   | indexAt((byte) hexDatas[i + 1]));
        }
        return data;
    }
840592
function  WriteIpAddr (buffer, offset, ipstring) {
    var addr= ipstring.split('.');
    buffer.writeUInt8(parseInt(addr[3]), offset);
    buffer.writeUInt8(parseInt(addr[2]), offset + 1);
    buffer.writeUInt8(parseInt(addr[1]), offset + 2);
    buffer.writeUInt8(parseInt(addr[0]), offset + 3);
}
1344246
public static byte[] readBytes(String path)
    {
        try
        {
            if (IOAdapter == null) return readBytesFromFileInputStream(new FileInputStream(path));

            InputStream is = IOAdapter.open(path);
            if (is instanceof FileInputStream)
                return readBytesFromFileInputStream((FileInputStream) is);
            else
                return readBytesFromOtherInputStream(is);
        }
        catch (Exception e)
        {
            logger.warning("读取" + path + "时发生异常" + e);
        }

        return null;
    }