/** * 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); } } }
@Override public void process(InputStream in, OutputStream out) throws IOException { int len; byte[] inBuf = new byte[8192]; Hex h = new Hex(); while ((len = in.read(inBuf)) > 0) { // If the input buffer is of odd length, try to get another byte if (len % 2 != 0) { int b = in.read(); if (b != -1) { inBuf[len] = (byte) b; len++; } } // Construct a new buffer bounded to len byte[] slice = Arrays.copyOfRange(inBuf, 0, len); try { out.write(h.decode(slice)); } catch (DecoderException ex) { throw new IOException(ex); } } out.flush(); } }
@Test public void testEncodeDecodeHexCharArrayRandom() throws DecoderException, EncoderException { final Random random = new Random(); final Hex hex = new Hex(); for (int i = 5; i > 0; i--) { final byte[] data = new byte[random.nextInt(10000) + 1]; random.nextBytes(data); // static API final char[] encodedChars = Hex.encodeHex(data); byte[] decodedBytes = Hex.decodeHex(encodedChars); assertTrue(Arrays.equals(data, decodedBytes)); // instance API with array parameter final byte[] encodedStringBytes = hex.encode(data); decodedBytes = hex.decode(encodedStringBytes); assertTrue(Arrays.equals(data, decodedBytes)); // instance API with char[] (Object) parameter String dataString = new String(encodedChars); char[] encodedStringChars = (char[]) hex.encode(dataString); decodedBytes = (byte[]) hex.decode(encodedStringChars); assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes)); // instance API with String (Object) parameter dataString = new String(encodedChars); encodedStringChars = (char[]) hex.encode(dataString); decodedBytes = (byte[]) hex.decode(new String(encodedStringChars)); assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), decodedBytes)); } }
@Test public void testDecodeByteBufferOddCharacters() { final ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put((byte) 65); try { new Hex().decode(buffer); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { // Expected exception } }
@Test public void testDecodeByteBufferEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], new Hex().decode(ByteBuffer.allocate(0)))); }
@Test public void testDecodeByteBufferObjectEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) ByteBuffer.allocate(0)))); }
@Test public void testDecodeByteArrayObjectEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) new byte[0]))); }
@Test public void testDecodeStringEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode(""))); }
@Test public void testDecodeByteArrayEmpty() throws DecoderException { assertTrue(Arrays.equals(new byte[0], new Hex().decode(new byte[0]))); }
@Test public void testDecodeBadCharacterPos1() { try { new Hex().decode("0q"); fail("An exception wasn't thrown when trying to decode an illegal character"); } catch (final DecoderException e) { // Expected exception } }
@Test public void testDecodeBadCharacterPos0() { try { new Hex().decode("q0"); fail("An exception wasn't thrown when trying to decode an illegal character"); } catch (final DecoderException e) { // Expected exception } }
@Test public void testDecodeClassCastException() { try { new Hex().decode(new int[] { 65 }); fail("An exception wasn't thrown when trying to decode."); } catch (final DecoderException e) { // Expected exception } }
@Test public void testDecodeHexStringOddCharacters() { try { new Hex().decode("6"); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { // Expected exception } }
@Test public void testDecodeByteArrayOddCharacters() { try { new Hex().decode(new byte[] { 65 }); fail("An exception wasn't thrown when trying to decode an odd number of characters"); } catch (final DecoderException e) { // Expected exception } }
final byte[] decodedUtf8Bytes = (byte[]) utf8Codec.decode(expectedHexString); actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharset()); final byte[] decodedCustomBytes = customCodec.decode(actualEncodedBytes); actualStringFromBytes = new String(decodedCustomBytes, name); assertEquals(name, sourceString, actualStringFromBytes);
Hex hex = new Hex(); ByteArrayInputStream bais = new ByteArrayInputStream( hex.decode(tokens[1].split("=")[1].getBytes(UTF_8))); DataInputStream dis = new DataInputStream(bais);
private static byte[] getBytesInternal(final String hexString) throws DecoderException { final Hex codec = new Hex(); final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString; final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString); return codec.decode(encodedBytes); }
private static byte[] getBytesInternal(final String hexString) throws DecoderException { final Hex codec = new Hex(); final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString; final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString); return codec.decode(encodedBytes); }
public static byte[] load(File file) throws IOException, DecoderException { Charset charset = Charset.forName("UTF-8"); return (byte[]) new Hex(charset).decode(FileUtils.readFileToString(file, charset)); }
public Encoder unhex() { try { data = new Hex().decode(data); return this; } catch (DecoderException e) { throw new BaseException(e, "Failed to convert data from hex: %s", e.getMessage()); } }