List of usage examples for org.apache.commons.codec DecoderException DecoderException
public DecoderException(final Throwable cause)
(cause==null ? Usage
From source file:com.google.gerrit.server.account.HashedPassword.java
/**
* decodes a hashed password encoded with {@link #encode}.
*
* @throws DecoderException if input is malformed.
*///from w ww .j a v a 2 s. co m
public static HashedPassword decode(String encoded) throws DecoderException {
if (!encoded.startsWith(ALGORITHM_PREFIX)) {
throw new DecoderException("unrecognized algorithm");
}
String[] fields = encoded.split(":");
if (fields.length != 4) {
throw new DecoderException("want 4 fields");
}
Integer cost = Ints.tryParse(fields[1]);
if (cost == null) {
throw new DecoderException("cost parse failed");
}
if (!(cost >= 4 && cost < 32)) {
throw new DecoderException("cost should be 4..31 inclusive, got " + cost);
}
byte[] salt = codec.decode(fields[2]);
if (salt.length != 16) {
throw new DecoderException("salt should be 16 bytes, got " + salt.length);
}
return new HashedPassword(codec.decode(fields[3]), salt, cost);
}
From source file:com.vinted.ab.security.Hex.java
/**
* Converts an array of characters representing hexadecimal values into an array of bytes of those same values. The
* returned array will be half the length of the passed array, as it takes two characters to represent any given
* byte. An exception is thrown if the passed char array has an odd number of elements.
*
* @param data/* ww w . j ava2 s . c o m*/
* An array of characters containing hexadecimal digits
* @return A byte array containing binary data decoded from the supplied char array.
* @throws DecoderException
* Thrown if an odd number or illegal of characters is supplied
*/
public static byte[] decodeHex(char[] data) throws DecoderException {
int len = data.length;
if ((len & 0x01) != 0) {
throw new DecoderException("Odd number of characters.");
}
byte[] out = new byte[len >> 1];
// two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(data[j], j) << 4;
j++;
f = f | toDigit(data[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
From source file:imgb64.BinaryCodec.java
/**
* Decodes a byte array where each byte represents an ASCII '0' or '1'.
*
* @param ascii//ww w . jav a 2 s . co m
* each byte represents an ASCII '0' or '1'
* @return the raw encoded binary where each bit corresponds to a byte in the byte array argument
* @throws DecoderException
* if argument is not a byte[], char[] or String
* @see org.apache.commons.codec.Decoder#decode(Object)
*/
@Override
public Object decode(final Object ascii) throws DecoderException {
if (ascii == null) {
return EMPTY_BYTE_ARRAY;
}
if (ascii instanceof byte[]) {
return fromAscii((byte[]) ascii);
}
if (ascii instanceof char[]) {
return fromAscii((char[]) ascii);
}
if (ascii instanceof String) {
return fromAscii(((String) ascii).toCharArray());
}
throw new DecoderException("argument not a byte array");
}
From source file:com.fatwire.dta.sscrawler.util.SSUriHelper.java
protected String decode(String value) throws DecoderException {
try {//from w w w . j a v a 2 s. c o m
return urlCodec.decode(value, getCharSet());
} catch (UnsupportedEncodingException e) {
DecoderException t = new DecoderException(e.getMessage());
t.initCause(e);
throw t;
}
}
From source file:com.vinted.ab.security.Hex.java
/**
* Converts a hexadecimal character to an integer.
*
* @param ch//from w ww . j av a 2 s. c om
* A character to convert to an integer digit
* @param index
* The index of the character in the source
* @return An integer
* @throws DecoderException
* Thrown if ch is an illegal hex character
*/
protected static int toDigit(char ch, int index) throws DecoderException {
int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
From source file:es.jamisoft.comun.codec.B64Codec.java
/**
* Decodes a Base64 object into its original form. Escaped characters are converted back to their original
* representation.//from w w w.ja va 2 s . c om
*
* @param value
* Base64 object to convert into its original form
*
* @return original object
*
* @throws DecoderException
* Thrown if the argument is not a <code>String</code>. Thrown if a failure condition is
* encountered during the decode process.
*/
public Object decode(Object value) throws DecoderException {
if (value == null) {
return null;
} else if (value instanceof String) {
return decode((String) value);
} else {
throw new DecoderException(
"Objects of type " + value.getClass().getName() + " cannot be decoded using BCodec");
}
}
From source file:com.app.framework.utilities.map.BaseNCodec.java
/**
* Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of
* the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.
*
* @param obj Object to decode//from w w w . java2 s . co m
* @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String
* supplied.
* @throws DecoderException if the parameter supplied is not of type byte[]
*/
@Override
public Object decode(final Object obj) throws DecoderException {
if (obj instanceof byte[]) {
return decode((byte[]) obj);
} else if (obj instanceof String) {
return decode((String) obj);
} else {
throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
}
}
From source file:com.ibc.util.BaseNCodec.java
/**
* Decodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of the
* Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.
*
* @param pObject//from www.jav a 2s .c o m
* Object to decode
* @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
* @throws DecoderException
* if the parameter supplied is not of type byte[]
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject instanceof byte[]) {
return decode((byte[]) pObject);
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Parameter supplied to Base-N decode is not a byte[] or a String");
}
}
From source file:com.android.email.codec.binary.Base64.java
/**
* Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the
* Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[].
*
* @param pObject/*www. j a v a2 s . c o m*/
* Object to decode
* @return An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied.
* @throws DecoderException
* if the parameter supplied is not of type byte[]
*/
public Object decode(Object pObject) throws DecoderException {
if (!(pObject instanceof byte[])) {
throw new DecoderException("Parameter supplied to Base64 decode is not a byte[]");
}
return decode((byte[]) pObject);
}
From source file:com.bfd.util.Base64.java
/**
* Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the
* Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String.
* //from w w w . j a v a 2s . co m
* @param pObject
* Object to decode
* @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
* @throws DecoderException
* if the parameter supplied is not of type byte[]
*/
public Object decode(Object pObject) throws DecoderException {
if (pObject instanceof byte[]) {
return decode((byte[]) pObject);
} else if (pObject instanceof String) {
return decode((String) pObject);
} else {
throw new DecoderException("Parameter supplied to Base64 decode is not a byte[] or a String");
}
}