Refactor base64 encode/decode methods

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@2244 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2014-03-10 20:46:29 +00:00
parent bc9ae770e7
commit cf10953212
1 changed files with 44 additions and 0 deletions

View File

@ -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();
}
}