/** * Decodes a byte array where each byte represents an ASCII '0' or '1'. * * @param ascii * each byte represents an ASCII '0' or '1' * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument * @see org.apache.commons.codec.Decoder#decode(Object) */ @Override public byte[] decode(final byte[] ascii) { return fromAscii(ascii); }
/** * Converts an array of raw binary data into a String of ASCII 0 and 1 characters. * * @param raw * the raw binary data to convert * @return a String of 0 and 1 characters representing the binary data * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static String toAsciiString(final byte[] raw) { return new String(toAsciiChars(raw)); } }
/** * Converts an array of raw binary data into an array of ASCII 0 and 1 characters. * * @param raw * the raw binary data to convert * @return 0 and 1 ASCII character bytes one for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ @Override public byte[] encode(final byte[] raw) { return toAsciiBytes(raw); }
/** * Decodes a byte array where each byte represents an ASCII '0' or '1'. * * @param ascii * each byte represents an ASCII '0' or '1' * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument */ public static byte[] fromAscii(final byte[] ascii) { if (isEmpty(ascii)) { return EMPTY_BYTE_ARRAY; } // get length/8 times bytes with 3 bit shifts to the right of the length final byte[] l_raw = new byte[ascii.length >> 3]; /* * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the * loop. */ 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 encoder= new BinaryCodec(); return encoder.encode(b.toByteArray()); } else if(MQTTPublisherGui.BASE64.equals(format)){ return Base64.encodeBase64(b.toByteArray());
@Before public void setUp() throws Exception { this.instance = new BinaryCodec(); }
/** * Converts an array of raw binary data into an array of ASCII 0 and 1 characters. * * @param raw * the raw binary data to convert * @return an array of 0 and 1 characters for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static char[] toAsciiChars(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_CHAR_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length final char[] l_ascii = new char[raw.length << 3]; /* * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the * loop. */ for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }
BinaryCodec encoder= new BinaryCodec(); return encoder.encode(b.toByteArray()); } else if(MQTTPublisherGui.BASE64.equals(format)){ return Base64.encodeBase64(b.toByteArray());
/** * Decodes a byte array where each byte represents an ascii '0' or '1'. * * @param ascii * each byte represents an ascii '0' or '1' * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument * @see org.apache.commons.codec.Decoder#decode(Object) */ public byte[] decode(byte[] ascii) { return fromAscii(ascii); }
/** * Converts an array of raw binary data into a String of ascii 0 and 1 characters. * * @param raw * the raw binary data to convert * @return a String of 0 and 1 characters representing the binary data * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static String toAsciiString(byte[] raw) { return new String(toAsciiChars(raw)); } }
/** * Converts an array of raw binary data into an array of ascii 0 and 1 characters. * * @param raw * the raw binary data to convert * @return 0 and 1 ascii character bytes one for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public byte[] encode(byte[] raw) { return toAsciiBytes(raw); }
/** * Converts an array of raw binary data into an array of ASCII 0 and 1 character bytes - each byte is a truncated * char. * * @param raw * the raw binary data to convert * @return an array of 0 and 1 character bytes for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length final byte[] l_ascii = new byte[raw.length << 3]; /* * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the * loop. */ for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }
BinaryCodec encoder= new BinaryCodec(); return encoder.encode(b.toByteArray()); } else if(MQTTPublisherGui.BASE64.equals(format)){ return Base64.encodeBase64(b.toByteArray());
/** * Decodes a String where each char of the String represents an ASCII '0' or '1'. * * @param ascii * String of '0' and '1' characters * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument * @see org.apache.commons.codec.Decoder#decode(Object) */ public byte[] toByteArray(final String ascii) { if (ascii == null) { return EMPTY_BYTE_ARRAY; } return fromAscii(ascii.toCharArray()); }
/** * Converts an array of raw binary data into an array of ASCII 0 and 1 chars. * * @param raw * the raw binary data to convert * @return 0 and 1 ASCII character chars one for each bit of the argument * @throws EncoderException * if the argument is not a byte[] * @see org.apache.commons.codec.Encoder#encode(Object) */ @Override public Object encode(final Object raw) throws EncoderException { if (!(raw instanceof byte[])) { throw new EncoderException("argument not a byte array"); } return toAsciiChars((byte[]) raw); }
String l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000000", l_encoded); bits = new byte[1]; bits[0] = BIT_0; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000001", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000011", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00001111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00011111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00111111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; l_encoded = new String(BinaryCodec.toAsciiBytes(bits));
/** * Decodes a byte array where each byte represents an ASCII '0' or '1'. * * @param ascii * each byte represents an ASCII '0' or '1' * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument */ public static byte[] fromAscii(final byte[] ascii) { if (isEmpty(ascii)) { return EMPTY_BYTE_ARRAY; } // get length/8 times bytes with 3 bit shifts to the right of the length final byte[] l_raw = new byte[ascii.length >> 3]; /* * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the * loop. */ 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; }
/** * Decodes a String where each char of the String represents an ascii '0' or '1'. * * @param ascii * String of '0' and '1' characters * @return the raw encoded binary where each bit corresponds to a byte in the byte array argument * @see org.apache.commons.codec.Decoder#decode(Object) */ public byte[] toByteArray(String ascii) { if (ascii == null) { return EMPTY_BYTE_ARRAY; } return fromAscii(ascii.toCharArray()); }
/** * Converts an array of raw binary data into an array of ascii 0 and 1 chars. * * @param raw * the raw binary data to convert * @return 0 and 1 ascii character chars one for each bit of the argument * @throws EncoderException * if the argument is not a byte[] * @see org.apache.commons.codec.Encoder#encode(java.lang.Object) */ public Object encode(Object raw) throws EncoderException { if (!(raw instanceof byte[])) { throw new EncoderException("argument not a byte array"); } return toAsciiChars((byte[]) raw); }
/** * Converts an array of raw binary data into an array of ascii 0 and 1 characters. * * @param raw * the raw binary data to convert * @return 0 and 1 ascii character bytes one for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public byte[] encode(byte[] raw) { return toAsciiBytes(raw); }