Example usage for org.apache.commons.codec.net BCodec BCodec

List of usage examples for org.apache.commons.codec.net BCodec BCodec

Introduction

In this page you can find the example usage for org.apache.commons.codec.net BCodec BCodec.

Prototype

public BCodec(final String charset) 

Source Link

Document

Constructor which allows for the selection of a default charset

Usage

From source file:io.datenwelt.cargo.rest.utils.Rfc2047.java

public static String decodeHeader(String input) {
    StringTokenizer tokenizer = new StringTokenizer(input, " \t", true);
    StringBuilder decoded = new StringBuilder();
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if (!token.startsWith("=?") || !token.endsWith("?=")) {
            decoded.append(token);// w ww  .  j a  v  a  2s.com
            continue;
        }
        String encodedWord = token;
        String[] parts = encodedWord.substring(2, encodedWord.length() - 2).split("\\?", 3);
        if (parts.length != 3) {
            decoded.append(token);
            continue;
        }
        Charset charset;
        try {
            charset = Charset.forName(parts[0]);
        } catch (Exception ex) {
            LOG.debug("Skipping the decoding of a header value due to an unknown charset \"{}\" found in: ",
                    parts[1], input, ex);
            decoded.append(token);
            continue;
        }
        String encoding = parts[1].toUpperCase();
        switch (encoding) {
        case "B":
            BCodec bcodec = new BCodec(charset);
            try {
                decoded.append(bcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of BASE64 value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        case "Q":
            QCodec qcodec = new QCodec(charset);
            try {
                decoded.append(qcodec.decode(encodedWord));
            } catch (Exception ex) {
                LOG.debug("Skipping the decoding of Q encoded value from string \"{}\" found in: ", encodedWord,
                        input, ex);
                decoded.append(token);
            }
            break;
        default:
            LOG.debug("Skipping the decoding of value from unknown encoding \"{}\" found in: ", encodedWord,
                    input);
            decoded.append(token);
        }
    }
    return decoded.toString();
}

From source file:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 * Alternative tobase64 method.Encodes the String(byte[]) instead of the array
 *
 * @param digest byte array for encoding in base 64,
 * @param cset String with name of charset
 * @return String base 64 the encoded base64 of digest
 *//*ww w  .ja  v a  2 s  .  c o  m*/
public static String tobase641(byte[] digest, String cset) {
    byte[] revdigest = changeByteOrder(digest, ByteOrder.nativeOrder());
    String str = null;

    str = new String(revdigest);
    ByteArrayOutputStream btstream = new ByteArrayOutputStream();
    //this make sure is written in big-endian

    DataOutputStream stream = new DataOutputStream(btstream);
    String tobase64 = null;
    //use a charset for encoding
    if (cset == null) {
        cset = DEFAULT_CHAR_ENCODING;
    }
    BCodec bc = new BCodec(cset);
    try {

        tobase64 = (String) bc.encode(str);

    } catch (EncoderException err) {
        mLog.info("base64Encoding: exception" + err.getMessage());
    }
    return tobase64;
}

From source file:com.zimbra.cs.db.DbMailItem.java

public static String encodeMetadata(String metadata) throws ServiceException {
    if (Db.supports(Db.Capability.NON_BMP_CHARACTERS) || !StringUtil.containsSurrogates(metadata)) {
        return metadata;
    }/*from www . j a  va 2 s  .co  m*/

    try {
        return new BCodec("utf-8").encode(metadata);
    } catch (EncoderException ee) {
        throw ServiceException.FAILURE("encoding non-BMP metadata", ee);
    }
}

From source file:com.zimbra.cs.db.DbMailItem.java

public static String decodeMetadata(String metadata) throws ServiceException {
    if (StringUtil.isNullOrEmpty(metadata) || !metadata.startsWith("=?")) {
        return metadata;
    } else {//  ww w  .ja  va2 s  .  c  o m
        try {
            return new BCodec("utf-8").decode(metadata);
        } catch (DecoderException de) {
            throw ServiceException.FAILURE("encoding non-BMP metadata", de);
        }
    }
}

From source file:org.apache.abdera.i18n.text.Rfc2047Helper.java

/**
 * Used to encode a string as specified by RFC 2047
 * /*from www.j  a  va  2 s .  c  o m*/
 * @param value The string to encode
 * @param charset The character set to use for the encoding
 */
public static String encode(String value, String charset, Codec codec) {
    if (value == null)
        return null;
    try {
        switch (codec) {
        case Q:
            return (new QCodec(charset)).encode(value);
        case B:
        default:
            return (new BCodec(charset)).encode(value);
        }
    } catch (Exception e) {
        return value;
    }
}

From source file:org.apache.abdera2.common.text.Codec.java

private String _encode(String value, String charset) {
    if (value == null)
        return null;
    try {//from   ww w .  ja v a  2  s .c  o  m
        StringEncoder e = null;
        switch (this) {
        case Q:
            e = new QCodec(charset);
            break;
        case B:
            e = new BCodec(charset);
            break;
        default:
            e = new StarCodec(charset);
            break;
        }
        return e.encode(value);
    } catch (Exception e) {
        return value;
    }
}

From source file:org.beangle.web.util.RequestUtils.java

public static String encodeAttachName(HttpServletRequest request, String attach_name) {
    String agent = request.getHeader("USER-AGENT");
    String newName = attach_name;
    try {/*  w w  w .j  a  va  2s .co  m*/
        if (null != agent && -1 != agent.indexOf("MSIE")) {
            newName = URLEncoder.encode(attach_name, "UTF-8");
        } else {
            newName = new BCodec("UTF-8").encode(attach_name);
        }
    } catch (Exception e) {
        logger.error("cannot encode " + attach_name, e);
        return attach_name;
    }
    return newName;
}