From cf109532127547c7f60bc9bb630f6c97d3b8a8f6 Mon Sep 17 00:00:00 2001 From: mguessan Date: Mon, 10 Mar 2014 20:46:29 +0000 Subject: [PATCH] Refactor base64 encode/decode methods git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2244 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- src/java/davmail/util/IOUtil.java | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/java/davmail/util/IOUtil.java b/src/java/davmail/util/IOUtil.java index 5a9f0602..5a764d3e 100644 --- a/src/java/davmail/util/IOUtil.java +++ b/src/java/davmail/util/IOUtil.java @@ -18,6 +18,8 @@ */ package davmail.util; +import org.apache.commons.codec.binary.Base64; + import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; @@ -46,6 +48,40 @@ public final class IOUtil { } + /** + * Decode base64 input string, return byte array. + * + * @param encoded Base64 encoded string + * @return decoded content as byte arrey + * @throws IOException on error + */ + public static byte[] decodeBase64(String encoded) throws IOException { + return Base64.decodeBase64(encoded.getBytes("ASCII")); + } + + /** + * Decode base64 input string, return content as UTF-8 String. + * + * @param encoded Base64 encoded string + * @return decoded content as byte arrey + * @throws IOException on error + */ + public static String decodeBase64AsString(String encoded) throws IOException { + return new String(decodeBase64(encoded), "UTF-8"); + } + + /** + * Base64 encode value. + * + * @param value input value + * @return base64 value + * @throws IOException on error + */ + public static String encodeBase64(String value) throws IOException { + return new String(Base64.encodeBase64(value.getBytes("UTF-8")), "ASCII"); + } + + /** * Resize image bytes to a max width or height image size. * @@ -89,6 +125,13 @@ public final class IOUtil { return targetImage; } + /** + * Read all inputStream content to a byte array. + * + * @param inputStream input stream + * @return content as byte array + * @throws IOException on error + */ public static byte[] readFully(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bytes = new byte[8192]; @@ -98,4 +141,5 @@ public final class IOUtil { } return baos.toByteArray(); } + }