@Test public void testFromAsciiByteArray() { assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length); assertEquals(0, BinaryCodec.fromAscii(new byte[0]).length); byte[] bits = new byte[1]; byte[] decoded = BinaryCodec.fromAscii("00000000".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0; decoded = BinaryCodec.fromAscii("00000001".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1; decoded = BinaryCodec.fromAscii("00000011".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2; decoded = BinaryCodec.fromAscii("00000111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; decoded = BinaryCodec.fromAscii("00001111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; decoded = BinaryCodec.fromAscii("00011111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; decoded = BinaryCodec.fromAscii("00111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; decoded = BinaryCodec.fromAscii("01111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[1]; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("11111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0000000011111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0000000111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0000001111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0000011111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0000111111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0001111111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0011111111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("0111111111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); bits = new byte[2]; bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); decoded = BinaryCodec.fromAscii("1111111111111111".getBytes(Charsets.UTF_8)); assertEquals(new String(bits), new String(decoded)); assertEquals(0, BinaryCodec.fromAscii((byte[]) null).length); }
public static byte[] fromAscii(final char[] ascii) { if (ascii == null || ascii.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] l_raw = new byte[ascii.length >> 3]; for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if (ascii[jj - bits] == '1') { l_raw[ii] |= BITS[bits]; } } } return l_raw; }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] fromAscii(final char[] ascii) { if (ascii == null || ascii.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] l_raw = new byte[ascii.length >> 3]; for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if (ascii[jj - bits] == '1') { l_raw[ii] |= BITS[bits]; } } } return l_raw; } }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] fromAscii(final char[] ascii) { if (ascii == null || ascii.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] l_raw = new byte[ascii.length >> 3]; for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if (ascii[jj - bits] == '1') { l_raw[ii] |= BITS[bits]; } } } return l_raw; }  }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] fromAscii(final char[] ascii) { if (ascii == null || ascii.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] l_raw = new byte[ascii.length >> 3]; for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if (ascii[jj - bits] == '1') { l_raw[ii] |= BITS[bits]; } } } return l_raw; }  @Override byte[] encode(final byte[] raw); @Override Object encode(final Object raw); @Override Object decode(final Object ascii); @Override byte[] decode(final byte[] ascii); byte[] toByteArray(final String ascii); static byte[] fromAscii(final char[] ascii); static byte[] fromAscii(final byte[] ascii); static byte[] toAsciiBytes(final byte[] raw); static char[] toAsciiChars(final byte[] raw); static String toAsciiString(final byte[] raw); }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] fromAscii(final char[] ascii) { if (ascii == null || ascii.length == 0) { return EMPTY_BYTE_ARRAY; } final byte[] l_raw = new byte[ascii.length >> 3]; for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if (ascii[jj - bits] == '1') { l_raw[ii] |= BITS[bits]; } } } return l_raw; }  @Override byte[] encode(final byte[] raw); @Override Object encode(final Object raw); @Override Object decode(final Object ascii); @Override byte[] decode(final byte[] ascii); byte[] toByteArray(final String ascii); static byte[] fromAscii(final char[] ascii); static byte[] fromAscii(final byte[] ascii); static byte[] toAsciiBytes(final byte[] raw); static char[] toAsciiChars(final byte[] raw); static String toAsciiString(final byte[] raw);  }