private static byte[] getPayload(int closeCode, String closeReason) { if (closeCode == -1) { return EMPTY_BYTES; } final byte[] bytes = Utils.toArray(closeCode); final byte[] reasonBytes = closeReason == null ? EMPTY_BYTES : closeReason.getBytes(new StrictUtf8()); final byte[] frameBytes = new byte[2 + reasonBytes.length]; System.arraycopy(bytes, bytes.length - 2, frameBytes, 0, 2); System.arraycopy(reasonBytes, 0, frameBytes, 2, reasonBytes.length); return frameBytes; } }
private static byte[] getPayload(int closeCode, String closeReason) { if (closeCode == -1) { return EMPTY_BYTES; } final byte[] bytes = Utils.toArray(closeCode); final byte[] reasonBytes = closeReason == null ? EMPTY_BYTES : closeReason.getBytes(new StrictUtf8()); final byte[] frameBytes = new byte[2 + reasonBytes.length]; System.arraycopy(bytes, bytes.length - 2, frameBytes, 0, 2); System.arraycopy(reasonBytes, 0, frameBytes, 2, reasonBytes.length); return frameBytes; } }
/** * Converts the length given to the appropriate framing data: <ol> <li>0-125 one element that is the payload * length. * <li>up to 0xFFFF, 3 element array starting with 126 with the following 2 bytes interpreted as a 16 bit unsigned * integer showing the payload length. <li>else 9 element array starting with 127 with the following 8 bytes * interpreted as a 64-bit unsigned integer (the high bit must be 0) showing the payload length. </ol> * * @param length the payload size * @return the array */ private byte[] encodeLength(final long length) { byte[] lengthBytes; if (length <= 125) { lengthBytes = new byte[1]; lengthBytes[0] = (byte) length; } else { byte[] b = Utils.toArray(length); if (length <= 0xFFFF) { lengthBytes = new byte[3]; lengthBytes[0] = 126; System.arraycopy(b, 6, lengthBytes, 1, 2); } else { lengthBytes = new byte[9]; lengthBytes[0] = 127; System.arraycopy(b, 0, lengthBytes, 1, 8); } } return lengthBytes; }
/** * Converts the length given to the appropriate framing data: <ol> <li>0-125 one element that is the payload * length. * <li>up to 0xFFFF, 3 element array starting with 126 with the following 2 bytes interpreted as a 16 bit unsigned * integer showing the payload length. <li>else 9 element array starting with 127 with the following 8 bytes * interpreted as a 64-bit unsigned integer (the high bit must be 0) showing the payload length. </ol> * * @param length the payload size * @return the array */ private byte[] encodeLength(final long length) { byte[] lengthBytes; if (length <= 125) { lengthBytes = new byte[1]; lengthBytes[0] = (byte) length; } else { byte[] b = Utils.toArray(length); if (length <= 0xFFFF) { lengthBytes = new byte[3]; lengthBytes[0] = 126; System.arraycopy(b, 6, lengthBytes, 1, 2); } else { lengthBytes = new byte[9]; lengthBytes[0] = 127; System.arraycopy(b, 0, lengthBytes, 1, 8); } } return lengthBytes; }