diff options
Diffstat (limited to 'libjava/gnu/java/io/Base64InputStream.java')
-rw-r--r-- | libjava/gnu/java/io/Base64InputStream.java | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/libjava/gnu/java/io/Base64InputStream.java b/libjava/gnu/java/io/Base64InputStream.java index 3780e952837..617e8315102 100644 --- a/libjava/gnu/java/io/Base64InputStream.java +++ b/libjava/gnu/java/io/Base64InputStream.java @@ -1,5 +1,5 @@ /* Base64InputStream.java -- base-64 input stream. - Copyright (C) 2003 Free Software Foundation, Inc. + Copyright (C) 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -38,6 +38,8 @@ exception statement from your version. */ package gnu.java.io; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; @@ -90,6 +92,30 @@ public class Base64InputStream extends FilterInputStream eof = false; } + // Class method. + // ------------------------------------------------------------------------ + + /** + * Decode a single Base-64 string to a byte array. + * + * @param base64 The Base-64 encoded data. + * @return The decoded bytes. + * @throws IOException If the given data do not compose a valid Base-64 + * sequence. + */ + public static byte[] decode(String base64) throws IOException + { + Base64InputStream in = + new Base64InputStream(new ByteArrayInputStream(base64.getBytes())); + ByteArrayOutputStream out = + new ByteArrayOutputStream((int) (base64.length() / 0.666)); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) != -1) + out.write(buf, 0, len); + return out.toByteArray(); + } + // Instance methods. // ------------------------------------------------------------------------ |