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 ? 0 : (buf.charAt(1) & 0xf0) >> 4)));
051            out.append(l > 1 ? enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l == 2 ? 0 : (buf.charAt(2) & 0xc0) >> 6)) : '=');
052            out.append(l > 2 ? enc.charAt(buf.charAt(2) & 0x3f) : '=');
053        }
054        return out.toString();
055    }
056
057    /**
058     * Encodes all remaining bytes from the specified byte buffer into a new string using the Base64 default encoding scheme.
059     * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
060     * @param s the source ByteBuffer to encode
061     * @return A new string containing the resulting encoded characters.
062     */
063    public static String encode(ByteBuffer s) {
064        return encode(s, false);
065    }
066
067    /**
068     * Encodes all remaining bytes from the specified byte buffer into a new string using a supported Base64 encoding scheme.
069     * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
070     * @param s the source ByteBuffer to encode
071     * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
072     * @return A new string containing the resulting encoded characters.
073     * @since 3840
074     */
075    public static String encode(ByteBuffer s, boolean urlsafe) {
076        StringBuilder out = new StringBuilder();
077        String enc = urlsafe ? encUrlSafe : encDefault;
078        // Read 3 bytes at a time.
079        for (int i = 0; i < (s.limit()+2)/3; ++i) {
080            int l = Math.min(3, s.limit()-i*3);
081            int byte0 = s.get() & 0xff;
082            int byte1 = l > 1 ? s.get() & 0xff : 0;
083            int byte2 = l > 2 ? s.get() & 0xff : 0;
084
085            out.append(enc.charAt(byte0 >> 2));
086            out.append(enc.charAt(
087                                  (byte0 & 0x03) << 4 |
088                                  (l == 1 ? 0 : (byte1 & 0xf0) >> 4)));
089            out.append(l > 1 ? enc.charAt((byte1 & 0x0f) << 2 | (l == 2 ? 0 : (byte2 & 0xc0) >> 6)) : '=');
090            out.append(l > 2 ? enc.charAt(byte2 & 0x3f) : '=');
091        }
092        return out.toString();
093    }
094}