diff options
Diffstat (limited to 'moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java')
-rw-r--r-- | moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java index 25a19bc..2c9b4c0 100644 --- a/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java +++ b/moaSig/common/src/main/java/at/gv/egovernment/moaspss/util/Base64Utils.java @@ -21,7 +21,6 @@ * that you distribute must include a readable copy of the "NOTICE" text file. */ - package at.gv.egovernment.moaspss.util; import java.io.ByteArrayInputStream; @@ -35,81 +34,81 @@ import iaik.utils.Base64OutputStream; /** * Utitility functions for encoding/decoding Base64 strings. - * + * * @author Patrick Peck * @version $Id$ */ public class Base64Utils { /** - * Read the bytes encoded in a Base64 encoded <code>String</code>. - * - * @param base64String The <code>String</code> containing the Base64 encoded - * bytes. + * Read the bytes encoded in a Base64 encoded <code>String</code>. + * + * @param base64String The <code>String</code> containing the Base64 + * encoded bytes. * @param ignoreInvalidChars Whether to ignore invalid Base64 characters. * @return byte[] The raw bytes contained in the <code>base64String</code>. * @throws IOException Failed to read the Base64 data. */ public static byte[] decode(String base64String, boolean ignoreInvalidChars, String encoding) - throws IOException { - - Base64InputStream in = - new Base64InputStream( - new ByteArrayInputStream(base64String.getBytes(encoding)), - ignoreInvalidChars); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] bytes = new byte[256]; + throws IOException { + + final Base64InputStream in = + new Base64InputStream( + new ByteArrayInputStream(base64String.getBytes(encoding)), + ignoreInvalidChars); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final byte[] bytes = new byte[256]; int bytesRead; while ((bytesRead = in.read(bytes)) > 0) { out.write(bytes, 0, bytesRead); } in.close(); - + return out.toByteArray(); } public static byte[] decode(String base64String, boolean ignoreInvalidChars) throws IOException { - return decode(base64String, ignoreInvalidChars, "UTF-8"); + return decode(base64String, ignoreInvalidChars, "UTF-8"); } - + /** * Read the bytes encoded in a Base64 encoded <code>String</code> and provide * them via an <code>InputStream</code>. - * - * @param base64String The <code>String</code> containing the Base64 encoded - * bytes. + * + * @param base64String The <code>String</code> containing the Base64 + * encoded bytes. * @param ignoreInvalidChars Whether to ignore invalid Base64 characters. * @return The <code>InputStream</code> from which the binary content of the - * <code>base64String</code> can be read. + * <code>base64String</code> can be read. */ public static InputStream decodeToStream( - String base64String, - boolean ignoreInvalidChars, - String encoding) { + String base64String, + boolean ignoreInvalidChars, + String encoding) { try { - ByteArrayInputStream bin = - new ByteArrayInputStream(base64String.getBytes(encoding)); - Base64InputStream in = new Base64InputStream(bin, ignoreInvalidChars); - + final ByteArrayInputStream bin = + new ByteArrayInputStream(base64String.getBytes(encoding)); + final Base64InputStream in = new Base64InputStream(bin, ignoreInvalidChars); + return in; - } catch (UnsupportedEncodingException e) { + } catch (final UnsupportedEncodingException e) { // cannot occur, since UTF-8 is required to be supported by every JRE - return null; + return null; } } public static InputStream decodeToStream( - String base64String, - boolean ignoreInvalidChars) { - return decodeToStream(base64String, ignoreInvalidChars, "UTF-8"); - + String base64String, + boolean ignoreInvalidChars) { + return decodeToStream(base64String, ignoreInvalidChars, "UTF-8"); + } - + /** * Convert a byte array to a Base64 encoded <code>String</code>. - * + * * @param bytes The bytes to encode. * @return String The Base64 encoded representation of the <code>bytes</code>. * @throws IOException Failed to write the bytes as Base64 data. @@ -119,25 +118,26 @@ public class Base64Utils { } public static String encode(byte[] bytes, String encoding) throws IOException { - return encode(new ByteArrayInputStream(bytes), encoding); - } - + return encode(new ByteArrayInputStream(bytes), encoding); + } + public static String encode(InputStream inputStream) throws IOException { - return encode(inputStream, "UTF-8"); + return encode(inputStream, "UTF-8"); } + /** * Convert the data contained in the given stream to a Base64 encoded * <code>String</code>. - * + * * @param inputStream The stream containing the data to encode. - * @return The Base64 encoded data of <code>inputStream</code>, as a - * <code>String</code>. + * @return The Base64 encoded data of <code>inputStream</code>, as a + * <code>String</code>. * @throws IOException Failed to convert the data in the stream. */ public static String encode(InputStream inputStream, String encoding) throws IOException { - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - Base64OutputStream base64Stream = new Base64OutputStream(byteStream, "\n".getBytes()); - byte[] bytes = new byte[256]; + final ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + final Base64OutputStream base64Stream = new Base64OutputStream(byteStream, "\n".getBytes()); + final byte[] bytes = new byte[256]; int bytesRead; while ((bytesRead = inputStream.read(bytes)) > 0) { |