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() 

Source Link

Document

Default constructor.

Usage

From source file:com.kwoksys.framework.util.StringUtils.java

/**
 * Encode a string to base64. Supports UTF8.
 * @return//from   ww w .j  a  v a 2s.com
 */
public static String encodeBase64Codec(String input) {
    BCodec codec = new BCodec();
    try {
        return codec.encode(input);

    } catch (EncoderException e) {
        logger.warning("Problem with base64 encoding.");
        return "";
    }
}

From source file:com.kwoksys.framework.util.StringUtils.java

/**
 * Decode base64-encoded string. Supports UTF8.
 * @return//from w  w w.  j a v a 2 s  . c  o m
 */
public static String decodeBase64Codec(String input) {
    if (input.isEmpty()) {
        return "";
    }
    BCodec codec = new BCodec();
    try {
        return codec.decode(input);

    } catch (DecoderException e) {
        logger.warning("Problem with base64 decoding.");
        return "";
    }
}

From source file:com.zimbra.cs.imap.ImapMessage.java

private static void nstring2047(PrintStream ps, String value) {
    if (value == null) {
        ps.write(NIL, 0, 3);//  ww w. j  av a 2s .com
        return;
    }

    boolean encoded = false;
    for (int i = 0, length = value.length(); i < length; i++) {
        char c = value.charAt(i);
        if (c == '"' || c == '\\' || c >= 0x7f || c < 0x20) {
            encoded = true;
        }
    }
    if (!encoded) {
        ps.write('"');
        ps.print(value);
        ps.write('"');
    } else {
        try {
            // can't use QCodec because it doesn't encode '"', which results in bad quoted-strings
            ps.write('"');
            ps.print(new BCodec().encode(value, "utf-8"));
            ps.write('"');
        } catch (EncoderException ee) {
            ps.write(NIL, 0, 3);
        }
    }
}

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

/**
 * Used to decode a string as specified by RFC 2047
 * //from   ww  w.java2s  . c o m
 * @param value The encoded string
 */
public static String decode(String value) {
    if (value == null)
        return null;
    try {
        // try BCodec first
        return (new BCodec()).decode(value);
    } catch (DecoderException de) {
        // try QCodec next
        try {
            return (new QCodec()).decode(value);
        } catch (Exception ex) {
            return value;
        }
    } catch (Exception e) {
        return value;
    }
}

From source file:org.chenillekit.tapestry.core.components.Kaptcha.java

private Link getImageLink() {
    BCodec bCodec = new BCodec();
    try {/*  ww w.  j ava  2s  .c  o  m*/
        return resources.createEventLink(EVENT_NAME, bCodec.encode(kaptchaProducer.createText()));
    } catch (EncoderException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.chenillekit.tapestry.core.components.Kaptcha.java

@OnEvent(value = EVENT_NAME)
public StreamResponse onKaptchaImage(String kaptchaValue) {
    BCodec bCodec = new BCodec();
    try {// w ww  .  j  av a2 s .c om
        this.kaptchaValue = bCodec.decode(kaptchaValue);
    } catch (DecoderException e) {
        throw new RuntimeException(e);
    }
    BufferedImage kapatchImage = kaptchaProducer.createImage(this.kaptchaValue);

    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(kapatchImage, "jpg", byteArrayOutputStream);
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        return new JPEGInline(bais, (String[]) null);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.chenillekit.tapestry.core.tests.TestStringCoding.java

@BeforeClass
public void initTest() {
    bCodec = new BCodec();
}

From source file:org.xwiki.velocity.tools.EscapeTool.java

/**
 * Encode a text using the B encoding specified in <a href="http://www.ietf.org/rfc/rfc2047.txt">RFC 2047</a>. UTF-8
 * is used as the character encoding, and no line breaking is performed. The resulting text is already wrapped with
 * the encoded word markers, starting with {@code =?UTF-8?B?} and ending with {@code ?=}.
 *
 * @param content the text to encode/*from w  ww  .j  a  v a 2  s . c om*/
 * @return the text converted into an encoded word using the B encoding
 */
public String b(Object content) {
    if (content != null) {
        try {
            return new BCodec().encode(String.valueOf(content));
        } catch (EncoderException ex) {
            // Just return null
        }
    }
    return null;
}