/** * Encodes an integer into up to 4 bytes in network byte order. * * @param num the int to convert to a byte array * @param count the number of reserved bytes for the write operation * @return the resulting byte array */ public static byte[] intToNetworkByteOrder(int num, int count) { byte[] buf = new byte[count]; intToNetworkByteOrder(num, buf, 0, count); return buf; }
/** * Returns a hexadecimal representation of the given byte array. * * @param bytes the array to output to an hex string * @return the hex representation as a string */ public static String asHex(byte[] bytes) { return asHex(bytes, null); }
/** * Try to force proxy connection to be kept alive. * * @param headers the request headers */ public static void addKeepAliveHeaders(Map<String, List<String>> headers) { StringUtilities.addValueToHeader(headers, "Keep-Alive", HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true); StringUtilities.addValueToHeader(headers, "Proxy-Connection", "keep-Alive", true); }
/** * Reads an int from 4 bytes of the given array at offset 0. * * @param b the byte array to read * @return the integer value */ public static final int makeIntFromByte4(byte[] b) { return makeIntFromByte4(b, 0); }
/** * Write a 16 bit short as LITTLE_ENDIAN. * * @param v the short to write * @return the Short in a byte[] */ public static final byte[] writeShort(short v) { return writeShort(v, new byte[2], 0); }
/** * Creates a new instance that uses specified <tt>delimiter</tt> byte array as a * message delimiter. * * @param delimiter an array of characters which delimits messages */ public IoBufferDecoder(byte[] delimiter) { setDelimiter(delimiter, true); }
/** * Write a 32 bit int as LITTLE_ENDIAN. * * @param v the int to write * @return the Int in a byte[] */ public static final byte[] writeInt(int v) { return writeInt(v, new byte[4], 0); }
/** * Reads an int from 2 bytes of the given array at offset 0. * * @param b the byte array to read * @return the int value */ public static final int makeIntFromByte2(byte[] b) { return makeIntFromByte2(b, 0); }
/** * Creates a new instance that will read messages of <tt>contentLength</tt> bytes. * * @param contentLength the exact length to read */ public IoBufferDecoder(int contentLength) { setContentLength(contentLength, false); }
/** * {@inheritDoc} */ protected void engineUpdate(byte b) { int pos = (int) (msgLength % BYTE_BLOCK_LENGTH); buffer[pos] = b; msgLength++; // If buffer contains enough data then process it. if (pos == (BYTE_BLOCK_LENGTH - 1)) { process(buffer, 0); } }
/** * Reads an int from 4 bytes of the given array at offset 0. * * @param b the byte array to read * @return the integer value */ public static final int makeIntFromByte4(byte[] b) { return makeIntFromByte4(b, 0); }
/** * Encodes an integer into up to 4 bytes in network byte order. * * @param num the int to convert to a byte array * @param count the number of reserved bytes for the write operation * @return the resulting byte array */ public static byte[] intToNetworkByteOrder(int num, int count) { byte[] buf = new byte[count]; intToNetworkByteOrder(num, buf, 0, count); return buf; }
/** * Returns a hexadecimal representation of the given byte array. * * @param bytes the array to output to an hex string * @return the hex representation as a string */ public static String asHex(byte[] bytes) { return asHex(bytes, null); }
/** * Try to force proxy connection to be kept alive. * * @param headers the request headers */ public static void addKeepAliveHeaders(Map<String, List<String>> headers) { StringUtilities.addValueToHeader(headers, "Keep-Alive", HttpProxyConstants.DEFAULT_KEEP_ALIVE_TIME, true); StringUtilities.addValueToHeader(headers, "Proxy-Connection", "keep-Alive", true); }
/** * Write a 16 bit short as LITTLE_ENDIAN. * * @param v the short to write * @return the Short in a byte[] */ public static final byte[] writeShort(short v) { return writeShort(v, new byte[2], 0); }
/** * Creates a new instance that uses specified <tt>delimiter</tt> byte array as a * message delimiter. * * @param delimiter an array of characters which delimits messages */ public IoBufferDecoder(byte[] delimiter) { setDelimiter(delimiter, true); }
/** * Write a 32 bit int as LITTLE_ENDIAN. * * @param v the int to write * @return the Int in a byte[] */ public static final byte[] writeInt(int v) { return writeInt(v, new byte[4], 0); }
/** * Reads an int from 2 bytes of the given array at offset 0. * * @param b the byte array to read * @return the int value */ public static final int makeIntFromByte2(byte[] b) { return makeIntFromByte2(b, 0); }
/** * Creates a new instance that will read messages of <tt>contentLength</tt> bytes. * * @param contentLength the exact length to read */ public IoBufferDecoder(int contentLength) { setContentLength(contentLength, false); }
/** * Reads an int from 4 bytes of the given array at offset 0. * * @param b the byte array to read * @param offset the offset at which to start * @return the int value */ public static int makeIntFromByte4(byte[] b) { return makeIntFromByte4(b, 0); }