/** * Translate all bytes of given array by appending each to octet buffer. If buffer contains 3 * octets its content will be encoded to 4 sextet byte values which are converted to a base64 * character each. All characters are appended to a StringBuffer. * * @param bytes The byte array to be encoded. */ public void translate(final byte[] bytes) { for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; if (_octets == 0) { _buffer = (_buffer & OCTET_1_MASK) | ((b & OCTET_MASK) << SHIFT_2_OCTET); } else if (_octets == 1) { _buffer = (_buffer & OCTET_2_MASK) | ((b & OCTET_MASK) << SHIFT_1_OCTET); } else { _buffer = (_buffer & OCTET_3_MASK) | (b & OCTET_MASK); } if ((++_octets) == OCTET_3) { encode(); } } }
/** * Encode given byte array into a encoded character array. * * @param bytes The byte array to be encoded. * @return Base64 encoded characters as an array. */ public static char[] encode(final byte[] bytes) { Base64Encoder enc = new Base64Encoder(); enc.translate(bytes); return enc.getCharArray(); }
/** * Get Base64 encoded characters as an array. * * @return Base64 encoded characters as an array. */ public char[] getCharArray() { if (_octets > 0) { encodeWithPadding(); } char[] chars = new char[_stream.length()]; if (_stream.length() > 0) { _stream.getChars(0, _stream.length(), chars, 0); } return chars; } }
/** * {@inheritDoc} */ public Object convert(final Object object) { return new String(Base64Encoder.encode((byte[]) object)); }
/** * Encode binary data. * * @param valueToEncode The binary data to encode. * @param componentType The XML schema component type. * @return Encoded binary data in {@link String} form. */ private Object encodeBinaryData(final Object valueToEncode, final String componentType) { String encodedValue; if (HexDecoder.DATA_TYPE.equals(componentType)) { encodedValue = HexDecoder.encode((byte[]) valueToEncode); } else { encodedValue = new String(Base64Encoder.encode((byte[]) valueToEncode)); } return encodedValue; }
chars = Base64Encoder.encode(bytes); attrList.addAttribute(XML.Entries.Attributes.ENCODING, "NMTOKEN", XML.Entries.Attributes.Encodings.BASE64);
chars[i] = (char) value[i]; } else { chars = Base64Encoder.encode(value); attrList.addAttribute(XML.Entries.Attributes.ENCODING, "NMTOKEN", XML.Entries.Attributes.Encodings.BASE64);