/** * Decodes the given encoded data using the given char-to-byte decoder. * * @param decoder Decoder to perform char-to-byte conversion. * @param encoded Encoded character data. * * @return Decoded data as raw bytes. * * @throws EncodingException on decoding errors. */ public static byte[] decode(final Decoder decoder, final CharSequence encoded) throws EncodingException { final ByteBuffer output = ByteBuffer.allocate(decoder.outputSize(encoded.length())); decoder.decode(CharBuffer.wrap(encoded), output); decoder.finalize(output); output.flip(); return ByteUtil.toArray(output); } }
return ByteUtil.toArray(output);
/** * Decodes an input string into a byte array using the configured decoder. * * @param input Input string to decode. * @param length Desired output size in bytes. * * @return Input decoded as a byte array. */ private static byte[] decode(final String input, final int length) { final Decoder decoder = new Base64Decoder.Builder().setAlphabet(ALPHABET).setPadding(false).build(); final ByteBuffer output = ByteBuffer.allocate(decoder.outputSize(input.length())); decoder.decode(CharBuffer.wrap(input), output); decoder.finalize(output); output.flip(); if (output.limit() != length) { throw new IllegalArgumentException("Input is not of the expected size: " + output.limit() + "!=" + length); } return ByteUtil.toArray(output); }