public static void writeBinaryCodedLengthBytes(byte[] data, ByteArrayOutputStream out) throws IOException {
        // 1. write length byte/bytes
        if (data.length < 252) {
            out.write((byte) data.length);
        } else if (data.length < (1 << 16L)) {
            out.write((byte) 252);
            writeUnsignedShortLittleEndian(data.length, out);
        } else if (data.length < (1 << 24L)) {
            out.write((byte) 253);
            writeUnsignedMediumLittleEndian(data.length, out);
        } else {
            out.write((byte) 254);
            writeUnsignedIntLittleEndian(data.length, out);
        }
        // 2. write real data followed length byte/bytes
        out.write(data);
    }
@Test
	public void testWriteBinaryCodedLengthBytes2() throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteHelper.writeBinaryCodedLengthBytes(new byte[252], out);

		byte[] expected = new byte[255];
		expected[0] = -4;
		expected[1] = -4;

		Assert.assertArrayEquals(expected, (out.toByteArray()));
	}