org.apache.commons.codec.binary.BinaryCodec Java Examples

The following examples show how to use org.apache.commons.codec.binary.BinaryCodec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ByteStringUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
public static String toString(byte[] data, int type) throws Exception {
    switch (type) {
        case BASE64:
            return Base64.encodeBase64String(data);
        case HEX:
            return Hex.encodeHexString(data);
        case ASCII:
            return BinaryCodec.toAsciiString(data);
        case BASE64_URLSAFE:
            return Base64.encodeBase64URLSafeString(data);
        default:
            return Base64.encodeBase64String(data);
    }
}
 
Example #2
Source File: ByteStringUtil.java    From littleca with Apache License 2.0 5 votes vote down vote up
public static byte[] toBytes(String data, int type) throws Exception {
    switch (type) {
        case BASE64:
        case BASE64_URLSAFE:
            return Base64.decodeBase64(data);
        case HEX:
            return Hex.decodeHex(data);
        case ASCII:
            return BinaryCodec.fromAscii(data.getBytes());
        default:
            return Base64.decodeBase64(data);
    }
}
 
Example #3
Source File: CodecUtil.java    From common_gui_tools with Apache License 2.0 3 votes vote down vote up
/**
 * Encode string to ascii.
 *
 * @param string  String
 * @param charSet CharSet
 * @return <code>String</code> ascii string
 * @throws UnsupportedEncodingException unsupported encoding exception
 */
public static String encodeAscii(String string, String charSet) throws UnsupportedEncodingException {
    if (string == null) {
        return null;
    }
    return BinaryCodec.toAsciiString(string.getBytes(charSet));
}
 
Example #4
Source File: CodecUtil.java    From common_gui_tools with Apache License 2.0 3 votes vote down vote up
/**
 * Decode ascii to string.
 *
 * @param string  String
 * @param charSet CharSet
 * @return <code>String</code> string
 * @throws UnsupportedEncodingException unsupported encoding exception
 */
public static String decodeAscii(String string, String charSet) throws UnsupportedEncodingException {
    if (string == null) {
        return null;
    }
    return new String(BinaryCodec.fromAscii(string.toCharArray()), charSet);
}