001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.nio.ByteBuffer;
005
006/**
007 * This class implements an encoder for encoding byte and character data using the
008 * <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding scheme as specified in
009 * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> ("base64", default) and
010 * <a href="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648</a> ("base64url").
011 * @since 195
012 */
013public final class Base64 {
014    // TODO: Remove this class when switching to Java 8 (finally integrated in Java SE as java.util.Base64.Encoder)
015
016    private Base64() {
017        // Hide default constructor for utils classes
018    }
019
020    /** "base64": RFC 2045 default encoding */
021    private static String encDefault = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
022    /** "base64url": RFC 4648 url-safe encoding */
023    private static String encUrlSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
024
025    /**
026     * Encodes all characters from the specified string into a new String using the Base64 default encoding scheme.
027     * @param s the string to encode
028     * @return A new string containing the resulting encoded characters.
029     */
030    public static String encode(String s) {
031        return encode(s, false);
032    }
033
034    /**
035     * Encodes all characters from the specified string into a new String using a supported Base64 encoding scheme.
036     * @param s the string to encode
037     * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
038     * @return A new string containing the resulting encoded characters.
039     * @since 3840
040     */
041    public static String encode(String s, boolean urlsafe) {
042        StringBuilder out = new StringBuilder();
043        String enc = urlsafe ? encUrlSafe : encDefault;
044        for (int i = 0; i < (s.length()+2)/3; ++i) {
045            int l = Math.min(3, s.length()-i*3);
046            String buf = s.substring(i*3, i*3+l);
047            out.append(enc.charAt(buf.charAt(0)>>2));
048            out.append(enc.charAt(
049                                  (buf.charAt(0) & 0x03) << 4 |
050                                  (l==1?
051                                   0:
052                                   (buf.charAt(1) & 0xf0) >> 4)));
053            out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
054            out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
055        }
056        return out.toString();
057    }
058
059    /**
060     * Encodes all remaining bytes from the specified byte buffer into a new string using the Base64 default encoding scheme.
061     * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
062     * @param s the source ByteBuffer to encode
063     * @return A new string containing the resulting encoded characters.
064     */
065    public static String encode(ByteBuffer s) {
066        return encode(s, false);
067    }
068
069    /**
070     * Encodes all remaining bytes from the specified byte buffer into a new string using a supported Base64 encoding scheme.
071     * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
072     * @param s the source ByteBuffer to encode
073     * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
074     * @return A new string containing the resulting encoded characters.
075     * @since 3840
076     */
077    public static String encode(ByteBuffer s, boolean urlsafe) {
078        StringBuilder out = new StringBuilder();
079        String enc = urlsafe ? encUrlSafe : encDefault;
080        // Read 3 bytes at a time.
081        for (int i = 0; i < (s.limit()+2)/3; ++i) {
082            int l = Math.min(3, s.limit()-i*3);
083            int byte0 = s.get() & 0xff;
084            int byte1 = l>1? s.get() & 0xff : 0;
085            int byte2 = l>2? s.get() & 0xff : 0;
086
087            out.append(enc.charAt(byte0>>2));
088            out.append(enc.charAt(
089                                  (byte0 & 0x03) << 4 |
090                                  (l==1?
091                                   0:
092                                   (byte1 & 0xf0) >> 4)));
093            out.append(l>1?enc.charAt((byte1 & 0x0f) << 2 | (l==2?0:(byte2 & 0xc0) >> 6)):'=');
094            out.append(l>2?enc.charAt(byte2 & 0x3f):'=');
095        }
096        return out.toString();
097    }
098}