-/*\r
- * Java Base64 - A pure Java library for reading and writing Base64\r
- * encoded streams.\r
- * \r
- * Copyright (C) 2007-2009 Carlo Pelliccia (www.sauronsoftware.it)\r
- * \r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU Lesser General Public License version\r
- * 2.1, as published by the Free Software Foundation.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU Lesser General Public\r
- * License version 2.1 along with this program.\r
- * If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package com.yuji.em.base64;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.OutputStream;\r
-import java.io.UnsupportedEncodingException;\r
-\r
-/**\r
- * <p>\r
- * Base64 encoding and decoding utility methods, both for binary and textual\r
- * informations.\r
- * </p>\r
- * \r
- * @author Carlo Pelliccia\r
- * @since 1.1\r
- * @version 1.3\r
- */\r
-public class Base64 {\r
-\r
- /**\r
- * <p>\r
- * Encodes a string.\r
- * </p>\r
- * <p>\r
- * Before the string is encoded in Base64, it is converted in a binary\r
- * sequence using the system default charset.\r
- * </p>\r
- * \r
- * @param str\r
- * The source string.\r
- * @return The encoded string.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- */\r
- public static String encode(String str) throws RuntimeException {\r
- byte[] bytes = str.getBytes();\r
- byte[] encoded = encode(bytes);\r
- try {\r
- return new String(encoded, "ASCII");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("ASCII is not supported!", e);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes a string.\r
- * </p>\r
- * <p>\r
- * Before the string is encoded in Base64, it is converted in a binary\r
- * sequence using the supplied charset.\r
- * </p>\r
- * \r
- * @param str\r
- * The source string\r
- * @param charset\r
- * The charset name.\r
- * @return The encoded string.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- * @since 1.2\r
- */\r
- public static String encode(String str, String charset)\r
- throws RuntimeException {\r
- byte[] bytes;\r
- try {\r
- bytes = str.getBytes(charset);\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("Unsupported charset: " + charset, e);\r
- }\r
- byte[] encoded = encode(bytes);\r
- try {\r
- return new String(encoded, "ASCII");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("ASCII is not supported!", e);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Decodes the supplied string.\r
- * </p>\r
- * <p>\r
- * The supplied string is decoded into a binary sequence, and then the\r
- * sequence is encoded with the system default charset and returned.\r
- * </p>\r
- * \r
- * @param str\r
- * The encoded string.\r
- * @return The decoded string.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- */\r
- public static String decode(String str) throws RuntimeException {\r
- byte[] bytes;\r
- try {\r
- bytes = str.getBytes("ASCII");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("ASCII is not supported!", e);\r
- }\r
- byte[] decoded = decode(bytes);\r
- return new String(decoded);\r
- }\r
-\r
- /**\r
- * <p>\r
- * Decodes the supplied string.\r
- * </p>\r
- * <p>\r
- * The supplied string is decoded into a binary sequence, and then the\r
- * sequence is encoded with the supplied charset and returned.\r
- * </p>\r
- * \r
- * @param str\r
- * The encoded string.\r
- * @param charset\r
- * The charset name.\r
- * @return The decoded string.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- * @since 1.2\r
- */\r
- public static String decode(String str, String charset)\r
- throws RuntimeException {\r
- byte[] bytes;\r
- try {\r
- bytes = str.getBytes("ASCII");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("ASCII is not supported!", e);\r
- }\r
- byte[] decoded = decode(bytes);\r
- try {\r
- return new String(decoded, charset);\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("Unsupported charset: " + charset, e);\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes a binary sequence.\r
- * </p>\r
- * <p>\r
- * If data are large, i.e. if you are working with large binary files,\r
- * consider to use a {@link Base64OutputStream} instead of loading too much\r
- * data in memory.\r
- * </p>\r
- * \r
- * @param bytes\r
- * The source sequence.\r
- * @return The encoded sequence.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- * @since 1.2\r
- */\r
- public static byte[] encode(byte[] bytes) throws RuntimeException {\r
- return encode(bytes, 0);\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes a binary sequence, wrapping every encoded line every\r
- * <em>wrapAt</em> characters. A <em>wrapAt</em> value less than 1 disables\r
- * wrapping.\r
- * </p>\r
- * <p>\r
- * If data are large, i.e. if you are working with large binary files,\r
- * consider to use a {@link Base64OutputStream} instead of loading too much\r
- * data in memory.\r
- * </p>\r
- * \r
- * @param bytes\r
- * The source sequence.\r
- * @param wrapAt\r
- * The max line length for encoded data. If less than 1 no wrap\r
- * is applied.\r
- * @return The encoded sequence.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- * @since 1.2\r
- */\r
- public static byte[] encode(byte[] bytes, int wrapAt)\r
- throws RuntimeException {\r
- ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);\r
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\r
- try {\r
- encode(inputStream, outputStream, wrapAt);\r
- } catch (IOException e) {\r
- throw new RuntimeException("Unexpected I/O error", e);\r
- } finally {\r
- try {\r
- inputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- try {\r
- outputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- return outputStream.toByteArray();\r
- }\r
-\r
- /**\r
- * <p>\r
- * Decodes a binary sequence.\r
- * </p>\r
- * <p>\r
- * If data are large, i.e. if you are working with large binary files,\r
- * consider to use a {@link Base64InputStream} instead of loading too much\r
- * data in memory.\r
- * </p>\r
- * \r
- * @param bytes\r
- * The encoded sequence.\r
- * @return The decoded sequence.\r
- * @throws RuntimeException\r
- * If an unexpected error occurs.\r
- * @since 1.2\r
- */\r
- public static byte[] decode(byte[] bytes) throws RuntimeException {\r
- ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);\r
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\r
- try {\r
- decode(inputStream, outputStream);\r
- } catch (IOException e) {\r
- throw new RuntimeException("Unexpected I/O error", e);\r
- } finally {\r
- try {\r
- inputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- try {\r
- outputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- return outputStream.toByteArray();\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes data from the given input stream and writes them in the given\r
- * output stream.\r
- * </p>\r
- * <p>\r
- * The supplied input stream is read until its end is reached, but it's not\r
- * closed by this method.\r
- * </p>\r
- * <p>\r
- * The supplied output stream is nor flushed neither closed by this method.\r
- * </p>\r
- * \r
- * @param inputStream\r
- * The input stream.\r
- * @param outputStream\r
- * The output stream.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- */\r
- public static void encode(InputStream inputStream, OutputStream outputStream)\r
- throws IOException {\r
- encode(inputStream, outputStream, 0);\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes data from the given input stream and writes them in the given\r
- * output stream, wrapping every encoded line every <em>wrapAt</em>\r
- * characters. A <em>wrapAt</em> value less than 1 disables wrapping.\r
- * </p>\r
- * <p>\r
- * The supplied input stream is read until its end is reached, but it's not\r
- * closed by this method.\r
- * </p>\r
- * <p>\r
- * The supplied output stream is nor flushed neither closed by this method.\r
- * </p>\r
- * \r
- * @param inputStream\r
- * The input stream from which clear data are read.\r
- * @param outputStream\r
- * The output stream in which encoded data are written.\r
- * @param wrapAt\r
- * The max line length for encoded data. If less than 1 no wrap\r
- * is applied.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- */\r
- public static void encode(InputStream inputStream,\r
- OutputStream outputStream, int wrapAt) throws IOException {\r
- Base64OutputStream aux = new Base64OutputStream(outputStream, wrapAt);\r
- copy(inputStream, aux);\r
- aux.commit();\r
- }\r
-\r
- /**\r
- * <p>\r
- * Decodes data from the given input stream and writes them in the given\r
- * output stream.\r
- * </p>\r
- * <p>\r
- * The supplied input stream is read until its end is reached, but it's not\r
- * closed by this method.\r
- * </p>\r
- * <p>\r
- * The supplied output stream is nor flushed neither closed by this method.\r
- * </p>\r
- * \r
- * @param inputStream\r
- * The input stream from which encoded data are read.\r
- * @param outputStream\r
- * The output stream in which decoded data are written.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- */\r
- public static void decode(InputStream inputStream, OutputStream outputStream)\r
- throws IOException {\r
- copy(new Base64InputStream(inputStream), outputStream);\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes data from the given source file contents and writes them in the\r
- * given target file, wrapping every encoded line every <em>wrapAt</em>\r
- * characters. A <em>wrapAt</em> value less than 1 disables wrapping.\r
- * </p>\r
- * \r
- * @param source\r
- * The source file, from which decoded data are read.\r
- * @param target\r
- * The target file, in which encoded data are written.\r
- * @param wrapAt\r
- * The max line length for encoded data. If less than 1 no wrap\r
- * is applied.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- * @since 1.3\r
- */\r
- public static void encode(File source, File target, int wrapAt)\r
- throws IOException {\r
- InputStream inputStream = null;\r
- OutputStream outputStream = null;\r
- try {\r
- inputStream = new FileInputStream(source);\r
- outputStream = new FileOutputStream(target);\r
- Base64.encode(inputStream, outputStream, wrapAt);\r
- } finally {\r
- if (outputStream != null) {\r
- try {\r
- outputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- if (inputStream != null) {\r
- try {\r
- inputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Encodes data from the given source file contents and writes them in the\r
- * given target file.\r
- * </p>\r
- * \r
- * @param source\r
- * The source file, from which decoded data are read.\r
- * @param target\r
- * The target file, in which encoded data are written.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- * @since 1.3\r
- */\r
- public static void encode(File source, File target) throws IOException {\r
- InputStream inputStream = null;\r
- OutputStream outputStream = null;\r
- try {\r
- inputStream = new FileInputStream(source);\r
- outputStream = new FileOutputStream(target);\r
- Base64.encode(inputStream, outputStream);\r
- } finally {\r
- if (outputStream != null) {\r
- try {\r
- outputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- if (inputStream != null) {\r
- try {\r
- inputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * <p>\r
- * Decodes data from the given source file contents and writes them in the\r
- * given target file.\r
- * </p>\r
- * \r
- * @param source\r
- * The source file, from which encoded data are read.\r
- * @param target\r
- * The target file, in which decoded data are written.\r
- * @throws IOException\r
- * If an I/O error occurs.\r
- * @since 1.3\r
- */\r
- public static void decode(File source, File target) throws IOException {\r
- InputStream inputStream = null;\r
- OutputStream outputStream = null;\r
- try {\r
- inputStream = new FileInputStream(source);\r
- outputStream = new FileOutputStream(target);\r
- decode(inputStream, outputStream);\r
- } finally {\r
- if (outputStream != null) {\r
- try {\r
- outputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- if (inputStream != null) {\r
- try {\r
- inputStream.close();\r
- } catch (Throwable t) {\r
- ;\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Copies data from a stream to another.\r
- * \r
- * @param inputStream\r
- * The input stream.\r
- * @param outputStream\r
- * The output stream.\r
- * @throws IOException\r
- * If a unexpected I/O error occurs.\r
- */\r
- private static void copy(InputStream inputStream, OutputStream outputStream)\r
- throws IOException {\r
- // 1KB buffer\r
- byte[] b = new byte[1024];\r
- int len;\r
- while ((len = inputStream.read(b)) != -1) {\r
- outputStream.write(b, 0, len);\r
- }\r
- }\r
-\r
-}\r
+/*
+ * Java Base64 - A pure Java library for reading and writing Base64
+ * encoded streams.
+ *
+ * Copyright (C) 2007-2009 Carlo Pelliccia (www.sauronsoftware.it)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version
+ * 2.1, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License version 2.1 along with this program.
+ * If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.yuji.em.base64;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * <p>
+ * Base64 encoding and decoding utility methods, both for binary and textual
+ * informations.
+ * </p>
+ *
+ * @author Carlo Pelliccia
+ * @since 1.1
+ * @version 1.3
+ */
+public class Base64 {
+
+ /**
+ * <p>
+ * Encodes a string.
+ * </p>
+ * <p>
+ * Before the string is encoded in Base64, it is converted in a binary
+ * sequence using the system default charset.
+ * </p>
+ *
+ * @param str
+ * The source string.
+ * @return The encoded string.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ */
+ public static String encode(String str) throws RuntimeException {
+ byte[] bytes = str.getBytes();
+ byte[] encoded = encode(bytes);
+ try {
+ return new String(encoded, "ASCII");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("ASCII is not supported!", e);
+ }
+ }
+
+ /**
+ * <p>
+ * Encodes a string.
+ * </p>
+ * <p>
+ * Before the string is encoded in Base64, it is converted in a binary
+ * sequence using the supplied charset.
+ * </p>
+ *
+ * @param str
+ * The source string
+ * @param charset
+ * The charset name.
+ * @return The encoded string.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ * @since 1.2
+ */
+ public static String encode(String str, String charset)
+ throws RuntimeException {
+ byte[] bytes;
+ try {
+ bytes = str.getBytes(charset);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("Unsupported charset: " + charset, e);
+ }
+ byte[] encoded = encode(bytes);
+ try {
+ return new String(encoded, "ASCII");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("ASCII is not supported!", e);
+ }
+ }
+
+ /**
+ * <p>
+ * Decodes the supplied string.
+ * </p>
+ * <p>
+ * The supplied string is decoded into a binary sequence, and then the
+ * sequence is encoded with the system default charset and returned.
+ * </p>
+ *
+ * @param str
+ * The encoded string.
+ * @return The decoded string.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ */
+ public static String decode(String str) throws RuntimeException {
+ byte[] bytes;
+ try {
+ bytes = str.getBytes("ASCII");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("ASCII is not supported!", e);
+ }
+ byte[] decoded = decode(bytes);
+ return new String(decoded);
+ }
+
+ /**
+ * <p>
+ * Decodes the supplied string.
+ * </p>
+ * <p>
+ * The supplied string is decoded into a binary sequence, and then the
+ * sequence is encoded with the supplied charset and returned.
+ * </p>
+ *
+ * @param str
+ * The encoded string.
+ * @param charset
+ * The charset name.
+ * @return The decoded string.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ * @since 1.2
+ */
+ public static String decode(String str, String charset)
+ throws RuntimeException {
+ byte[] bytes;
+ try {
+ bytes = str.getBytes("ASCII");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("ASCII is not supported!", e);
+ }
+ byte[] decoded = decode(bytes);
+ try {
+ return new String(decoded, charset);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("Unsupported charset: " + charset, e);
+ }
+ }
+
+ /**
+ * <p>
+ * Encodes a binary sequence.
+ * </p>
+ * <p>
+ * If data are large, i.e. if you are working with large binary files,
+ * consider to use a {@link Base64OutputStream} instead of loading too much
+ * data in memory.
+ * </p>
+ *
+ * @param bytes
+ * The source sequence.
+ * @return The encoded sequence.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ * @since 1.2
+ */
+ public static byte[] encode(byte[] bytes) throws RuntimeException {
+ return encode(bytes, 0);
+ }
+
+ /**
+ * <p>
+ * Encodes a binary sequence, wrapping every encoded line every
+ * <em>wrapAt</em> characters. A <em>wrapAt</em> value less than 1 disables
+ * wrapping.
+ * </p>
+ * <p>
+ * If data are large, i.e. if you are working with large binary files,
+ * consider to use a {@link Base64OutputStream} instead of loading too much
+ * data in memory.
+ * </p>
+ *
+ * @param bytes
+ * The source sequence.
+ * @param wrapAt
+ * The max line length for encoded data. If less than 1 no wrap
+ * is applied.
+ * @return The encoded sequence.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ * @since 1.2
+ */
+ public static byte[] encode(byte[] bytes, int wrapAt)
+ throws RuntimeException {
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
+ encode(inputStream, outputStream, wrapAt);
+ } catch (IOException e) {
+ throw new RuntimeException("Unexpected I/O error", e);
+ } finally {
+ try {
+ inputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ try {
+ outputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ return outputStream.toByteArray();
+ }
+
+ /**
+ * <p>
+ * Decodes a binary sequence.
+ * </p>
+ * <p>
+ * If data are large, i.e. if you are working with large binary files,
+ * consider to use a {@link Base64InputStream} instead of loading too much
+ * data in memory.
+ * </p>
+ *
+ * @param bytes
+ * The encoded sequence.
+ * @return The decoded sequence.
+ * @throws RuntimeException
+ * If an unexpected error occurs.
+ * @since 1.2
+ */
+ public static byte[] decode(byte[] bytes) throws RuntimeException {
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ try {
+ decode(inputStream, outputStream);
+ } catch (IOException e) {
+ throw new RuntimeException("Unexpected I/O error", e);
+ } finally {
+ try {
+ inputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ try {
+ outputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ return outputStream.toByteArray();
+ }
+
+ /**
+ * <p>
+ * Encodes data from the given input stream and writes them in the given
+ * output stream.
+ * </p>
+ * <p>
+ * The supplied input stream is read until its end is reached, but it's not
+ * closed by this method.
+ * </p>
+ * <p>
+ * The supplied output stream is nor flushed neither closed by this method.
+ * </p>
+ *
+ * @param inputStream
+ * The input stream.
+ * @param outputStream
+ * The output stream.
+ * @throws IOException
+ * If an I/O error occurs.
+ */
+ public static void encode(InputStream inputStream, OutputStream outputStream)
+ throws IOException {
+ encode(inputStream, outputStream, 0);
+ }
+
+ /**
+ * <p>
+ * Encodes data from the given input stream and writes them in the given
+ * output stream, wrapping every encoded line every <em>wrapAt</em>
+ * characters. A <em>wrapAt</em> value less than 1 disables wrapping.
+ * </p>
+ * <p>
+ * The supplied input stream is read until its end is reached, but it's not
+ * closed by this method.
+ * </p>
+ * <p>
+ * The supplied output stream is nor flushed neither closed by this method.
+ * </p>
+ *
+ * @param inputStream
+ * The input stream from which clear data are read.
+ * @param outputStream
+ * The output stream in which encoded data are written.
+ * @param wrapAt
+ * The max line length for encoded data. If less than 1 no wrap
+ * is applied.
+ * @throws IOException
+ * If an I/O error occurs.
+ */
+ public static void encode(InputStream inputStream,
+ OutputStream outputStream, int wrapAt) throws IOException {
+ Base64OutputStream aux = new Base64OutputStream(outputStream, wrapAt);
+ copy(inputStream, aux);
+ aux.commit();
+ }
+
+ /**
+ * <p>
+ * Decodes data from the given input stream and writes them in the given
+ * output stream.
+ * </p>
+ * <p>
+ * The supplied input stream is read until its end is reached, but it's not
+ * closed by this method.
+ * </p>
+ * <p>
+ * The supplied output stream is nor flushed neither closed by this method.
+ * </p>
+ *
+ * @param inputStream
+ * The input stream from which encoded data are read.
+ * @param outputStream
+ * The output stream in which decoded data are written.
+ * @throws IOException
+ * If an I/O error occurs.
+ */
+ public static void decode(InputStream inputStream, OutputStream outputStream)
+ throws IOException {
+ copy(new Base64InputStream(inputStream), outputStream);
+ }
+
+ /**
+ * <p>
+ * Encodes data from the given source file contents and writes them in the
+ * given target file, wrapping every encoded line every <em>wrapAt</em>
+ * characters. A <em>wrapAt</em> value less than 1 disables wrapping.
+ * </p>
+ *
+ * @param source
+ * The source file, from which decoded data are read.
+ * @param target
+ * The target file, in which encoded data are written.
+ * @param wrapAt
+ * The max line length for encoded data. If less than 1 no wrap
+ * is applied.
+ * @throws IOException
+ * If an I/O error occurs.
+ * @since 1.3
+ */
+ public static void encode(File source, File target, int wrapAt)
+ throws IOException {
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ try {
+ inputStream = new FileInputStream(source);
+ outputStream = new FileOutputStream(target);
+ Base64.encode(inputStream, outputStream, wrapAt);
+ } finally {
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Encodes data from the given source file contents and writes them in the
+ * given target file.
+ * </p>
+ *
+ * @param source
+ * The source file, from which decoded data are read.
+ * @param target
+ * The target file, in which encoded data are written.
+ * @throws IOException
+ * If an I/O error occurs.
+ * @since 1.3
+ */
+ public static void encode(File source, File target) throws IOException {
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ try {
+ inputStream = new FileInputStream(source);
+ outputStream = new FileOutputStream(target);
+ Base64.encode(inputStream, outputStream);
+ } finally {
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ }
+ }
+
+ /**
+ * <p>
+ * Decodes data from the given source file contents and writes them in the
+ * given target file.
+ * </p>
+ *
+ * @param source
+ * The source file, from which encoded data are read.
+ * @param target
+ * The target file, in which decoded data are written.
+ * @throws IOException
+ * If an I/O error occurs.
+ * @since 1.3
+ */
+ public static void decode(File source, File target) throws IOException {
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ try {
+ inputStream = new FileInputStream(source);
+ outputStream = new FileOutputStream(target);
+ decode(inputStream, outputStream);
+ } finally {
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (Throwable t) {
+ ;
+ }
+ }
+ }
+ }
+
+ /**
+ * Copies data from a stream to another.
+ *
+ * @param inputStream
+ * The input stream.
+ * @param outputStream
+ * The output stream.
+ * @throws IOException
+ * If a unexpected I/O error occurs.
+ */
+ private static void copy(InputStream inputStream, OutputStream outputStream)
+ throws IOException {
+ // 1KB buffer
+ byte[] b = new byte[1024];
+ int len;
+ while ((len = inputStream.read(b)) != -1) {
+ outputStream.write(b, 0, len);
+ }
+ }
+
+}