001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.io.base64;
020    
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.io.UnsupportedEncodingException;
024    
025    /**
026     * <code>Base64Encoder</code> provides methods to ease the encoding of strings and byte arrays in base64.
027     * The {@link Base64OutputStream} class is used under the hood to perform the actual base64 encoding.
028     *
029     * @see Base64OutputStream
030     * @author Maxence Bernard
031     */
032    public abstract class Base64Encoder {
033    
034        /**
035         * Base64-encodes the given byte array and returns the result. The specified encoding is used for tranforming
036         * the string into bytes.
037         *
038         * @param b the String to base64-encode
039         * @return the base64-encoded String
040         */
041        public static String encode(byte[] b) {
042            return encode(b, 0, b.length);
043        }
044    
045        /**
046         * Base64-encodes the given byte array, from off to len, and returns the result. The specified encoding is used
047         * for tranforming the string into bytes.
048         *
049         * @param b the String to base64-encode
050         * @param off position to the first byte in the array to be encoded
051         * @param len number of bytes in the array to encode
052         * @return the base64-encoded String
053         */
054        public static String encode(byte[] b, int off, int len) {
055            ByteArrayOutputStream bout = new ByteArrayOutputStream();
056            Base64OutputStream out64 = new Base64OutputStream(bout, false);
057    
058            try {
059                out64.write(b, off, len);
060                out64.writePadding();
061    
062                return new String(bout.toByteArray());
063            }
064            catch(IOException e) {
065                // Should never happen
066                return null;
067            }
068            finally {
069                try { out64.close(); }
070                catch(IOException e) {
071                    // Should never happen
072                }
073            }
074        }
075    
076        /**
077         * Shorthand for {@link #encode(String, String)} invoked with UTF-8 encoding.
078         *
079         * @param s the String to base64-encode
080         * @return the base64-encoded String
081         */
082        public static String encode(String s) {
083            try {
084                return encode(s, "UTF-8");
085            }
086            catch(UnsupportedEncodingException e) {
087                // Should never happen, UTF-8 is necessarily supported by the Java runtime
088                return null;
089            }
090        }
091    
092        /**
093         * Base64-encodes the given String and returns result. The specified encoding is used for tranforming
094         * the string into bytes.
095         *
096         * @param s the String to base64-encode
097         * @param encoding the character encoding to use for transforming the string into bytes
098         * @return the base64-encoded String
099         * @throws UnsupportedEncodingException if the specified encoding is not supported by the Java runtime
100         */
101        public static String encode(String s, String encoding) throws UnsupportedEncodingException {
102            return encode(s.getBytes(encoding));
103        }
104    }