Log a byte array as a hex dump.

@param data byte array
518993
public static void log(byte[] data)
   {
      if (LOG != null)
      {
         LOG.println(ByteArrayHelper.hexdump(data, true, 16, ""));
         LOG.flush();
      }
   }
1034910
@Override
    public int writeCData(String data) throws IOException
    {
        writeAscii("<![CDATA[");
        int ix = writeCDataContent(data);
        if (ix >= 0) {
            return ix;
        }
        writeAscii("]]>");
        return -1;
    }
199775
public static void dumpHex(StringBuilder s, ByteBuffer buffer) {
		int savePos = buffer.position();
		int line = 0;
		while (buffer.hasRemaining()) {
			s.append(StringUtil.encodeHexaPadding(16L * line)).append(' ');
			int l = buffer.remaining() >= 16 ? 16 : buffer.remaining();
			byte[] b = new byte[l];
			buffer.get(b);
			for (int i = 0; i < 16; ++i) {
				if (b.length <= i) s.append("   ");
				else s.append(StringUtil.encodeHexa(b[i])).append(' ');
			}
			s.append("  ");
			for (int i = 0; i < 16; ++i) {
				if (b.length <= i) s.append(' ');
				else {
					char c = (char)(b[i] & 0xFF);
					if (c < 0x20 || c > 0x7F) c = '.';
					s.append(c);
				}
			}
			s.append("\r\n");
			line++;
		}
		buffer.position(savePos);
	}
351909
static public String getHexDump(byte[] data, int length) {
        // boundary checks....
        if (null == data || 0 > length) {
            return null;
        }
        // if we have less than the target amount, just print what we have
        if (data.length < length) {
            length = data.length;
        }
        int numlines = (length / 16) + (((length % 16) > 0) ? 1 : 0);
        StringBuilder buffer = new StringBuilder(73 * numlines);
        for (int i = 0, line = 0; line < numlines; line++, i += 16) {
            buffer = formatLineId(buffer, i);
            buffer.append(": ");
            // format the first 8 bytes as hex data
            buffer = formatHexData(buffer, data, i);
            buffer.append("  ");
            // format the second 8 bytes as hex data
            buffer = formatHexData(buffer, data, i + 8);
            buffer.append("  ");
            // now print the ascii version, filtering out non-ascii chars
            buffer = formatTextData(buffer, data, i);
            buffer.append('\n');
        }
        return buffer.toString();
    }
665150
public static byte[] decode(byte[] data)
	{
		if (data.length == 0)
		{
			return data;
		}
		int lastRealDataIndex = data.length - 1;
		while (data[lastRealDataIndex] == equalSign)
		{
			lastRealDataIndex--;
		}
		// original data digit is 8 bits long, but base64 digit is 6 bits long
		int padBytes = data.length - 1 - lastRealDataIndex;
		int byteLength = data.length * 6 / 8 - padBytes;
		byte[] result = new byte[byteLength];
		// Each 4 bytes of input (encoded) we end up with 3 bytes of output
		int dataIndex = 0;
		int resultIndex = 0;
		int allBits = 0;
		// how many result chunks we can process before getting to pad bytes
		int resultChunks = (lastRealDataIndex + 1) / 4;
		for (int i = 0; i < resultChunks; i++)
		{
			allBits = 0;
			// Loop 4 times gathering input bits (4 * 6 = 24)
			for (int j = 0; j < 4; j++)
			{
				allBits = allBits << 6 | decodeDigit(data[dataIndex++]);
			}
			// Loop 3 times generating output bits (3 * 8 = 24)
			for (int j = resultIndex + 2; j >= resultIndex; j--)
			{
				result[j] = (byte) (allBits & 0xff); // Bottom 8 bits
				allBits = allBits >>> 8;
			}
			resultIndex += 3; // processed 3 result bytes
		}
		// Now we do the extra bytes in case the original (non-encoded) data
		// was not multiple of 3 bytes
		switch (padBytes)
		{
			case 1:
				// 1 pad byte means 3 (4-1) extra Base64 bytes of input, 18
				// bits, of which only 16 are meaningful
				// Or: 2 bytes of result data
				allBits = 0;
				// Loop 3 times gathering input bits
				for (int j = 0; j < 3; j++)
				{
					allBits = allBits << 6 | decodeDigit(data[dataIndex++]);
				}
				// NOTE - The code below ends up being equivalent to allBits =
				// allBits>>>2
				// But we code it in a non-optimized way for clarity
				// The 4th, missing 6 bits are all 0
				allBits = allBits << 6;
				// The 3rd, missing 8 bits are all 0
				allBits = allBits >>> 8;
				// Loop 2 times generating output bits
				for (int j = resultIndex + 1; j >= resultIndex; j--)
				{
					result[j] = (byte) (allBits & 0xff); // Bottom 8
					// bits
					allBits = allBits >>> 8;
				}
				break;
			case 2:
				// 2 pad bytes mean 2 (4-2) extra Base64 bytes of input, 12 bits
				// of data, of which only 8 are meaningful
				// Or: 1 byte of result data
				allBits = 0;
				// Loop 2 times gathering input bits
				for (int j = 0; j < 2; j++)
				{
					allBits = allBits << 6 | decodeDigit(data[dataIndex++]);
				}
				// NOTE - The code below ends up being equivalent to allBits =
				// allBits>>>4
				// But we code it in a non-optimized way for clarity
				// The 3rd and 4th, missing 6 bits are all 0
				allBits = allBits << 6;
				allBits = allBits << 6;
				// The 3rd and 4th, missing 8 bits are all 0
				allBits = allBits >>> 8;
				allBits = allBits >>> 8;
				result[resultIndex] = (byte) (allBits & 0xff); // Bottom
				// 8
				// bits
				break;
		}
		return result;
	}
724861
public static String encodeHexString(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
771846
@Deprecated
    public static String encodeHex(byte[] data, char delimiter) {
        // the result
        StringBuilder result = new StringBuilder();

        short val = 0;

        // encode each byte into a hex dump
        for (int i = 0; i < data.length; i++) {
            val = decodeUnsigned(data[i]);
            result.append(padLeading(Integer.toHexString((int) val), 2));
            result.append(delimiter);
        }

        // return encoded text
        return result.toString();
    }
1110619
public void parse(byte[] data) throws ParseException {
        Text text = new Text();
        text.strain(data, 0, data.length);
        init(text);
    }
21209
static String toHexString(byte[] data) {
    char[] chArray = new char[data.length * 2];
    int pos = 0;
    for (byte b : data) {
      chArray[pos++] = HEX_CHARS[(b >> 4) & 0x0f];
      chArray[pos++] = HEX_CHARS[b & 0x0f];
    }
    return new String(chArray);
  }
201393
private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            int v = bytes[i] & 0xFF;
            hexChars[i * 2] = hexArray[v >>> 4];
            hexChars[i * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
1290832
public static void writeHexBytes(byte[] o, int from, byte[] b) {

        int len = b.length;

        for (int i = 0; i < len; i++) {
            int c = ((int) b[i]) & 0xff;

            o[from++] = HEXBYTES[c >> 4 & 0xf];
            o[from++] = HEXBYTES[c & 0xf];
        }
    }
557985
public static byte[] sha1X16(String data, String encoding) {
		byte[] bytes = sha1(data, encoding);
		StringBuilder sha1StrBuff = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			if (Integer.toHexString(0xFF & bytes[i]).length() == 1) {
				sha1StrBuff.append("0").append(
						Integer.toHexString(0xFF & bytes[i]));
			} else {
				sha1StrBuff.append(Integer.toHexString(0xFF & bytes[i]));
			}
		}
		try {
			return sha1StrBuff.toString().getBytes(encoding);
		} catch (UnsupportedEncodingException e) {
			LogUtil.writeErrorLog(e.getMessage(), e);
			return null;
		}
	}
1203713
private static String base64Encode(byte[] data) {
        char c64[] = new char[data.length * 4 / 3 + 5];
        for (int i = 0, k = 0; i < data.length; i += 3, k += 4) {
            c64[k + 0] = (char) (data[i] >> 2);
            c64[k + 1] = (char) ((data[i] & 0x03) << 4);
            c64[k + 2] = c64[k + 3] = 64;
            if ((i + i) < data.length) {
                c64[k + 1] |= data[i + 1] >> 4;
                c64[k + 2] = (char) ((data[i + 1] & 0x0f) << 2);
            }
            if ((i + 2) < data.length) {
                c64[k + 2] |= data[i + 2] >> 6;
                c64[k + 3] = (char) (data[i + 2] & 0x3f);
            }
            for (int j = 0; j < 4; ++j) {
                c64[k + j] = BASE64_LUT.charAt(c64[k + j]);
            }
        }
        return new String(c64);
    }
464468
private void parseData(byte[] data)
	{
		int bytesRead = 0;

		if (grouped)
		{
			group = data[bytesRead];
			bytesRead ++;
		}
		if (encrypted)
		{
			encrType = data[bytesRead];
			bytesRead ++;
		}
		if (lengthIndicator)
		{
			dataLength = Helpers.convertDWordToInt(data, bytesRead);
			bytesRead += 4;
		}

		frameData = new byte[data.length - bytesRead];
		System.arraycopy(data, bytesRead, frameData, 0, frameData.length);
	}
293238
public static StringBuffer encode(byte[] data)
	{
		int rem = data.length % 3;
		int num = data.length / 3;
		StringBuffer result = new StringBuffer();
		int p = 0;
		// int c=0;

		for (int i = 0; i < num; i++)
		{
			int b1 = (data[p] & 0xfc) >> 2;
			int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
			int b3 = (data[p + 1] & 0x0f) << 2 | (data[p + 2] & 0xc0) >> 6;
			int b4 = data[p + 2] & 0x3f;

			result.append(b64encode(b1));
			result.append(b64encode(b2));
			result.append(b64encode(b3));
			result.append(b64encode(b4));

			p += 3;
			// c += 4;
		}

		switch (rem)
		{
			case 0:
				break;

			case 1:
			{
				int b1 = (data[p] & 0xfc) >> 2;
				int b2 = (data[p] & 0x03) << 4;

				result.append(b64encode(b1));
				result.append(b64encode(b2));
				result.append('=');
				result.append('=');
				break;
			}

			case 2:
			{
				int b1 = (data[p] & 0xfc) >> 2;
				int b2 = (data[p] & 0x03) << 4 | (data[p + 1] & 0xf0) >> 4;
				int b3 = (data[p + 1] & 0x0f) << 2;

				result.append(b64encode(b1));
				result.append(b64encode(b2));
				result.append(b64encode(b3));
				result.append('=');
				break;
			}
		}

		return result;
	}
588971
public static String hexEncode(final byte[] data) {
        try {
            val result = Hex.encodeHex(data);
            return new String(result);
        } catch (final Exception e) {
            return null;
        }
    }
379944
public void writeBinaryString(byte[] data) throws IOException {
		if (data == null)
			writeInt(0);
		else
			writeBinaryString(data, 0, data.length);
	}