Example usage for org.apache.commons.codec EncoderException EncoderException

List of usage examples for org.apache.commons.codec EncoderException EncoderException

Introduction

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

Prototype

public EncoderException(final String message, final Throwable cause) 

Source Link

Document

Constructs a new exception with the specified detail message and cause.

Usage

From source file:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java

/**
 * Encodes a byte array and return the encoded data as a byte array.
 *
 * @param source Data to be encoded//from ww w. ja  va 2  s .  co  m
 * @return ?byte.source, null
 * @throws org.apache.commons.codec.EncoderException thrown if the Encoder encounters a failure condition during the encoding process.
 */
@Override
public byte[] encode(byte[] source) throws EncoderException {
    if (source == null) {
        return null;
    }
    try {

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME);
        SecretKey secretKey = keyFactory.generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(CIPHER_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameters);
        return cipher.doFinal(source);

    } catch (Exception e) {
        LOG.error(":" + key + ":" + source, e);
        throw new EncoderException(":" + key + ":" + source, e);
    }
}

From source file:com.vinted.ab.security.Hex.java

/**
 * Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
 * byte in order. The returned array will be double the length of the passed String or array, as it takes two
 * characters to represent any given byte.
 * <p>/*from   www  . jav  a  2s  . co  m*/
 * The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
 * {@link #getCharset()}.
 * </p>
 *
 * @param object
 *            a String, or byte[] to convert to Hex characters
 * @return A char[] containing hexadecimal characters
 * @throws EncoderException
 *             Thrown if the given object is not a String or byte[]
 * @see #encodeHex(byte[])
 */
@Override
public Object encode(Object object) throws EncoderException {
    try {
        byte[] byteArray = object instanceof String ? ((String) object).getBytes(this.getCharset())
                : (byte[]) object;
        return encodeHex(byteArray);
    } catch (ClassCastException e) {
        throw new EncoderException(e.getMessage(), e);
    }
}

From source file:sample.tsc.util.Hex.java

/**
 * Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
 * byte in order. The returned array will be double the length of the passed String or array, as it takes two
 * characters to represent any given byte.
 * <p>/*from ww w.ja v a2  s .  c  om*/
 * The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
 * {@link #getCharsetName()}.
 * </p>
 * 
 * @param object
 *            a String, or byte[] to convert to Hex characters
 * @return A char[] containing hexadecimal characters
 * @throws EncoderException
 *             Thrown if the given object is not a String or byte[]
 * @see #encodeHex(byte[])
 */
public Object encode(Object object) throws EncoderException {
    try {
        byte[] byteArray = object instanceof String ? ((String) object).getBytes(getCharsetName())
                : (byte[]) object;
        return encodeHex(byteArray);
    } catch (ClassCastException e) {
        throw new EncoderException(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new EncoderException(e.getMessage(), e);
    }
}