/** * Returns the numeric value of the character <code>b</code> in radix 16. * * @param b * The byte to be converted. * @return The numeric value represented by the character in radix 16. * * @throws DecoderException * Thrown when the byte is not valid per {@link Character#digit(char,int)} */ static int digit16(final byte b) throws DecoderException { final int i = Character.digit((char) b, RADIX); if (i == -1) { throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + RADIX + "): " + b); } return i; }
/** * Converts a hexadecimal character to an integer. * * @param ch * 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(final char ch, final int index) throws DecoderException { final int digit = Character.digit(ch, 16); if (digit == -1) { throw new DecoderException("Illegal hexadecimal character " + ch + " at index " + index); } return digit; }
/** * Converts a hexadecimal character to an integer. * * @param ch 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 charcter " + ch + " at index " + index); } return digit; }
/** * Decodes a Base64 string into its original form. Escaped characters are converted back to their original * representation. * * @param value * Base64 string to convert into its original form * @return original string * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ @Override public String decode(final String value) throws DecoderException { if (value == null) { return null; } try { return this.decodeText(value); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } }
/** * Decodes a quoted-printable string into its original form. Escaped characters are converted back to their original * representation. * * @param str * quoted-printable string to convert into its original form * @return original string * @throws DecoderException * A decoder exception is thrown if a failure condition is encountered during the decode process. */ @Override public String decode(final String str) throws DecoderException { if (str == null) { return null; } try { return decodeText(str); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } }
/** * Decodes a Base64 object into its original form. Escaped characters are converted back to their original * representation. * * @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. */ @Override public Object decode(final 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"); } }
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable 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. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec"); } }
/** * 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 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); }
/** * Decodes a URL safe object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * URL safe object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code> or <code>byte[]</code>. Thrown if a failure * condition is encountered during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof byte[]) { return decode((byte[]) obj); } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be URL decoded"); } }
buffer.write((char) ((u << 4) + l)); } catch (final ArrayIndexOutOfBoundsException e) { throw new DecoderException("Invalid URL encoding: ", e);
/** * Decodes a quoted-printable object into its original form. Escaped characters are converted back to their original * representation. * * @param obj * quoted-printable object to convert into its original form * @return original object * @throws DecoderException * Thrown if the argument is not a <code>String</code> or <code>byte[]</code>. Thrown if a failure * condition is encountered during the decode process. */ @Override public Object decode(final Object obj) throws DecoderException { if (obj == null) { return null; } else if (obj instanceof byte[]) { return decode((byte[]) obj); } else if (obj instanceof String) { return decode((String) obj); } else { throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be quoted-printable decoded"); } }
/** * Decodes a URL safe string into its original form using the default string charset. Escaped characters are * converted back to their original representation. * * @param str * URL safe string to convert into its original form * @return original string * @throws DecoderException * Thrown if URL decoding is unsuccessful * @see #getDefaultCharset() */ @Override public String decode(final String str) throws DecoderException { if (str == null) { return null; } try { return decode(str, getDefaultCharset()); } catch (final UnsupportedEncodingException e) { throw new DecoderException(e.getMessage(), e); } }
/** * 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 * @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"); } }
/** * Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those * same values. The returned array will be half the length of the passed String or 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 object * A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digits * @return A byte array containing binary data decoded from the supplied byte array (representing characters). * @throws DecoderException * Thrown if an odd number of characters is supplied to this function or the object is not a String or * char[] * @see #decodeHex(char[]) */ @Override public Object decode(final Object object) throws DecoderException { if (object instanceof String) { return decode(((String) object).toCharArray()); } else if (object instanceof byte[]) { return decode((byte[]) object); } else if (object instanceof ByteBuffer) { return decode((ByteBuffer) object); } else { try { return decodeHex((char[]) object); } catch (final ClassCastException e) { throw new DecoderException(e.getMessage(), e); } } }
/** * 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 * 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(final char[] data) throws DecoderException { final int len = data.length; if ((len & 0x01) != 0) { throw new DecoderException("Odd number of characters."); } final 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; }
/** * Decodes a byte array where each byte represents an ASCII '0' or '1'. * * @param ascii * 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"); }
@Test public void testConstructorThrowable() { final DecoderException e = new DecoderException(t); assertEquals(t.getClass().getName(), e.getMessage()); assertEquals(t, e.getCause()); }
@Test public void testConstructorStringThrowable() { final DecoderException e = new DecoderException(MSG, t); assertEquals(MSG, e.getMessage()); assertEquals(t, e.getCause()); }
@Test public void testConstructor0() { final DecoderException e = new DecoderException(); assertNull(e.getMessage()); assertNull(e.getCause()); }
@Test public void testConstructorString() { final DecoderException e = new DecoderException(MSG); assertEquals(MSG, e.getMessage()); assertNull(e.getCause()); }