解压缩zlib

@param in 数据流
@param length 预估长度
@return 解压后的bytes
@since 4.1.19
551948
public static byte[] unZlib(InputStream in, int length) {
		final ByteArrayOutputStream out = new ByteArrayOutputStream(length);
		inflater(in, out);
		return out.toByteArray();
	}
843416
public static ChunkHeader read(IoBuffer in) {
        int remaining = in.remaining();
        if (remaining > 0) {
            byte headerByte = in.get();
            ChunkHeader h = new ChunkHeader();
            // going to check highest 2 bits
            h.format = (byte) ((0b11000000 & headerByte) >> 6);
            int fmt = headerByte & 0x3f;
            switch (fmt) {
                case 0:
                    // two byte header
                    h.size = 2;
                    if (remaining < 2) {
                        throw new ProtocolException("Bad chunk header, at least 2 bytes are expected");
                    }
                    h.channelId = 64 + (in.get() & 0xff);
                    break;
                case 1:
                    // three byte header
                    h.size = 3;
                    if (remaining < 3) {
                        throw new ProtocolException("Bad chunk header, at least 3 bytes are expected");
                    }
                    byte b1 = in.get();
                    byte b2 = in.get();
                    h.channelId = 64 + ((b2 & 0xff) << 8 | (b1 & 0xff));
                    break;
                default:
                    // single byte header
                    h.size = 1;
                    h.channelId = 0x3f & headerByte;
                    break;
            }
            // check channel id is valid
            if (h.channelId < 0) {
                throw new ProtocolException("Bad channel id: " + h.channelId);
            }
            log.trace("CHUNK header byte {}, count {}, header {}, channel {}", String.format("%02x", headerByte), h.size, 0, h.channelId);
            return h;
        } else {
            // at least one byte for valid decode
            throw new ProtocolException("Bad chunk header, at least 1 byte is expected");
        }
    }
638821
public static byte[] decode(byte[] in) {
		if (ArrayUtil.isEmpty(in)) {
			return in;
		}
		return decode(in, 0, in.length);
	}
214404
private static byte[] streamToBytes(InputStream in, int length) throws IOException {
        byte[] bytes = new byte[length];
        int count;
        int pos = 0;
        while (pos < length && ((count = in.read(bytes, pos, length - pos)) != -1)) {
            pos += count;
        }
        if (pos != length) {
            throw new IOException("Expected " + length + " bytes, read " + pos + " bytes");
        }
        return bytes;
    }
203653
public static String encode(byte[] in) {
        if (in == null) {
            return null;
        }
        return encode(in, 0, in.length);
    }
6015
public static BinaryString blankString(int length) {
		byte[] spaces = new byte[length];
		Arrays.fill(spaces, (byte) ' ');
		return fromBytes(spaces);
	}
1123155
public Object readLengthList(AbstractHessianInput in, int length)
        throws IOException
    {
        Object[] data = createArray(length);

        in.addRef(data);

        if (_componentType != null) {
            for (int i = 0; i < data.length; i++)
                data[i] = in.readObject(_componentType);
        }
        else {
            for (int i = 0; i < data.length; i++)
                data[i] = in.readObject();
        }

        return data;
    }
1358333
public static byte[] integerToBytes(BigInteger s, int length) {
      byte[] bytes = s.toByteArray();
      if (length < bytes.length) {
         // The length is smaller than the byte representation. Truncate by
         // copying over the least significant bytes
         byte[] tmp = new byte[length];
         System.arraycopy(bytes, bytes.length - tmp.length, tmp, 0, tmp.length);
         return tmp;
      } else if (length > bytes.length) {
         // The length is larger than the byte representation. Copy over all
         // bytes and leave it prefixed by zeros.
         byte[] tmp = new byte[length];
         System.arraycopy(bytes, 0, tmp, tmp.length - bytes.length, bytes.length);
         return tmp;
      }
      return bytes;
   }
383095
public static int longToIntWithBitMixing(long in) {
		in = (in ^ (in >>> 30)) * 0xbf58476d1ce4e5b9L;
		in = (in ^ (in >>> 27)) * 0x94d049bb133111ebL;
		in = in ^ (in >>> 31);
		return (int) in;
	}
278707
private int extractNextTlvElementLength(IoBuffer in) {
        if (!hasRemainingData(in, 2)) {
            return NOT_ENOUGH_DATA;
        }
        try {
            in.mark();
            int firstByte = in.getUnsigned();
            boolean tlv8 = (firstByte & TLV16_MASK) == 0;
            if (tlv8) {
                // 8 bit length. NB! Reads one unsigned byte as an integer
                return in.getUnsigned() + TLV8_HEADER_LENGTH;
            }
            // skip tlv16 LSB byte
            in.skip(1);
            if (!hasRemainingData(in, 2)) {
                return NOT_ENOUGH_DATA;
            }
            // 16 bit length. NB! Reads two bytes unsigned integer
            return in.getUnsignedShort() + TLV16_HEADER_LENGTH;
        } finally {
            in.reset();
        }
    }
417517
protected String readStringImpl(int length)
        throws IOException
    {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < length; i++) {
            int ch = is.read();

            if (ch < 0x80)
                sb.append((char) ch);
            else if ((ch & 0xe0) == 0xc0) {
                int ch1 = is.read();
                int v = ((ch & 0x1f) << 6) + (ch1 & 0x3f);

                sb.append((char) v);
            }
            else if ((ch & 0xf0) == 0xe0) {
                int ch1 = is.read();
                int ch2 = is.read();
                int v = ((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f);

                sb.append((char) v);
            }
            else
                throw new IOException("bad utf-8 encoding");
        }

        return sb.toString();
    }
1016096
public static String hexDump(ByteBuf buffer, int fromIndex, int length) {
        return HexUtil.hexDump(buffer, fromIndex, length);
    }
609997
public static byte[] decode(byte[] in, int pos, int length) {
		if (ArrayUtil.isEmpty(in)) {
			return in;
		}

		final IntWrapper offset = new IntWrapper(pos);

		byte sestet0;
		byte sestet1;
		byte sestet2;
		byte sestet3;
		int maxPos = pos + length - 1;
		int octetId = 0;
		byte[] octet = new byte[length * 3 / 4];// over-estimated if non-base64 characters present
		while (offset.value <= maxPos) {
			sestet0 = getNextValidDecodeByte(in, offset, maxPos);
			sestet1 = getNextValidDecodeByte(in, offset, maxPos);
			sestet2 = getNextValidDecodeByte(in, offset, maxPos);
			sestet3 = getNextValidDecodeByte(in, offset, maxPos);

			if (PADDING != sestet1) {
				octet[octetId++] = (byte) ((sestet0 << 2) | (sestet1 >>> 4));
			}
			if (PADDING != sestet2) {
				octet[octetId++] = (byte) (((sestet1 & 0xf) << 4) | (sestet2 >>> 2));
			}
			if (PADDING != sestet3) {
				octet[octetId++] = (byte) (((sestet2 & 3) << 6) | sestet3);
			}
		}

		if (octetId == octet.length) {
			return octet;
		} else {
			// 如果有非Base64字符混入，则实际结果比解析的要短，截取之
			return (byte[]) ArrayUtil.copy(octet, new byte[octetId], octetId);
		}
	}
1263541
public static String encode(Byte[] in) {
		byte[] tmp = new byte[in.length];
		for(int i = 0; i < tmp.length; i++) {
			tmp[i] = in[i];
		}
		return encode(tmp);
	}
77464
public static final int readUTF(InputStream in,
                                    char[] chars, int offset, int length)
        throws IOException
    {
        if (length == 0) {
            return 0;
        }

        int c, c2, c3;

        int charCount = 0;
        while (charCount < length) {
            c = in.read();
            if (c < 0) {
                break;
            }
            c &= 0xff;
            switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                chars[offset + charCount++] = (char)c;
                break;
            case 12: case 13:
                // 110x xxxx  10xx xxxx
                c2 = in.read();
                if (c2 < 0) {
                    throw new EOFException();
                }
                if ((c2 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] =
                    (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                c2 = in.read();
                if (c2 < 0) {
                    throw new EOFException();
                }
                c3 = in.read();
                if (c3 < 0) {
                    throw new EOFException();
                }
                if ((c2 & 0xc0) != 0x80 || (c3 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] = (char)(((c & 0x0f) << 12) |
                                                     ((c2 & 0x3f) << 6) |
                                                     (c3 & 0x3f));
                break;
            default:
                // 10xx xxxx,  1111 xxxx
                throw new UTFDataFormatException();
            }
        }

        return (charCount == 0) ? -1 : charCount;
    }
1225193
public static final int readUTF(DataInput in,
                                    char[] chars, int offset, int length,
                                    int bytesExpected)
        throws IOException
    {
        int c, c2, c3;

        int byteCount = 0;
        int charCount = 0;
        while (byteCount < bytesExpected && charCount < length) {
            c = in.readByte() & 0xff;
            switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                byteCount++;
                chars[offset + charCount++] = (char)c;
                break;
            case 12: case 13:
                // 110x xxxx  10xx xxxx
                if ((byteCount += 2) > bytesExpected) {
                    throw new UTFDataFormatException();
                }
                c2 = in.readByte();
                if ((c2 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] =
                    (char)(((c & 0x1f) << 6) | (c2 & 0x3f));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                if ((byteCount += 3) > bytesExpected) {
                    throw new UTFDataFormatException();
                }
                c2 = in.readByte();
                c3 = in.readByte();
                if ((c2 & 0xc0) != 0x80 || (c3 & 0xc0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                chars[offset + charCount++] = (char)(((c & 0x0f) << 12) |
                                                     ((c2 & 0x3f) << 6) |
                                                     (c3 & 0x3f));
                break;
            default:
                // 10xx xxxx,  1111 xxxx
                throw new UTFDataFormatException();
            }
        }

        return charCount;
    }
593457
function(length) {
      var bytes, hex;
      if (length == null) {
        length = 32;
      }
      length /= 2;
      bytes = this.randomBytes(length);
      return hex = this.toHex(bytes).toUpperCase();
    }