[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[626] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[662] | 4 | import java.nio.ByteBuffer;
|
---|
| 5 |
|
---|
[7060] | 6 | /**
|
---|
| 7 | * This class implements an encoder for encoding byte and character data using the
|
---|
| 8 | * <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding scheme as specified in
|
---|
| 9 | * <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> ("base64", default) and
|
---|
| 10 | * <a href="http://tools.ietf.org/html/rfc4648#section-4">RFC 4648</a> ("base64url").
|
---|
| 11 | * @since 195
|
---|
| 12 | */
|
---|
[6362] | 13 | public final class Base64 {
|
---|
[7060] | 14 | // TODO: Remove this class when switching to Java 8 (finally integrated in Java SE as java.util.Base64.Encoder)
|
---|
[626] | 15 |
|
---|
[6362] | 16 | private Base64() {
|
---|
| 17 | // Hide default constructor for utils classes
|
---|
| 18 | }
|
---|
[7509] | 19 |
|
---|
[7060] | 20 | /** "base64": RFC 2045 default encoding */
|
---|
[3840] | 21 | private static String encDefault = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
---|
[7060] | 22 | /** "base64url": RFC 4648 url-safe encoding */
|
---|
[3840] | 23 | private static String encUrlSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
---|
[626] | 24 |
|
---|
[7060] | 25 | /**
|
---|
| 26 | * Encodes all characters from the specified string into a new String using the Base64 default encoding scheme.
|
---|
| 27 | * @param s the string to encode
|
---|
| 28 | * @return A new string containing the resulting encoded characters.
|
---|
| 29 | */
|
---|
[662] | 30 | public static String encode(String s) {
|
---|
[3840] | 31 | return encode(s, false);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[7060] | 34 | /**
|
---|
| 35 | * Encodes all characters from the specified string into a new String using a supported Base64 encoding scheme.
|
---|
| 36 | * @param s the string to encode
|
---|
| 37 | * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
|
---|
| 38 | * @return A new string containing the resulting encoded characters.
|
---|
| 39 | * @since 3840
|
---|
| 40 | */
|
---|
[3840] | 41 | public static String encode(String s, boolean urlsafe) {
|
---|
[662] | 42 | StringBuilder out = new StringBuilder();
|
---|
[3840] | 43 | String enc = urlsafe ? encUrlSafe : encDefault;
|
---|
[662] | 44 | for (int i = 0; i < (s.length()+2)/3; ++i) {
|
---|
| 45 | int l = Math.min(3, s.length()-i*3);
|
---|
| 46 | String buf = s.substring(i*3, i*3+l);
|
---|
[8510] | 47 | out.append(enc.charAt(buf.charAt(0) >> 2));
|
---|
[662] | 48 | out.append(enc.charAt(
|
---|
| 49 | (buf.charAt(0) & 0x03) << 4 |
|
---|
[8510] | 50 | (l == 1 ? 0 : (buf.charAt(1) & 0xf0) >> 4)));
|
---|
| 51 | out.append(l > 1 ? enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l == 2 ? 0 : (buf.charAt(2) & 0xc0) >> 6)) : '=');
|
---|
| 52 | out.append(l > 2 ? enc.charAt(buf.charAt(2) & 0x3f) : '=');
|
---|
[662] | 53 | }
|
---|
| 54 | return out.toString();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[7060] | 57 | /**
|
---|
| 58 | * Encodes all remaining bytes from the specified byte buffer into a new string using the Base64 default encoding scheme.
|
---|
| 59 | * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
|
---|
| 60 | * @param s the source ByteBuffer to encode
|
---|
| 61 | * @return A new string containing the resulting encoded characters.
|
---|
| 62 | */
|
---|
[662] | 63 | public static String encode(ByteBuffer s) {
|
---|
[3840] | 64 | return encode(s, false);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[7060] | 67 | /**
|
---|
| 68 | * Encodes all remaining bytes from the specified byte buffer into a new string using a supported Base64 encoding scheme.
|
---|
| 69 | * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
|
---|
| 70 | * @param s the source ByteBuffer to encode
|
---|
| 71 | * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
|
---|
| 72 | * @return A new string containing the resulting encoded characters.
|
---|
| 73 | * @since 3840
|
---|
| 74 | */
|
---|
[3840] | 75 | public static String encode(ByteBuffer s, boolean urlsafe) {
|
---|
[662] | 76 | StringBuilder out = new StringBuilder();
|
---|
[3840] | 77 | String enc = urlsafe ? encUrlSafe : encDefault;
|
---|
[662] | 78 | // Read 3 bytes at a time.
|
---|
| 79 | for (int i = 0; i < (s.limit()+2)/3; ++i) {
|
---|
| 80 | int l = Math.min(3, s.limit()-i*3);
|
---|
| 81 | int byte0 = s.get() & 0xff;
|
---|
[8510] | 82 | int byte1 = l > 1 ? s.get() & 0xff : 0;
|
---|
| 83 | int byte2 = l > 2 ? s.get() & 0xff : 0;
|
---|
[662] | 84 |
|
---|
[8510] | 85 | out.append(enc.charAt(byte0 >> 2));
|
---|
[662] | 86 | out.append(enc.charAt(
|
---|
| 87 | (byte0 & 0x03) << 4 |
|
---|
[8510] | 88 | (l == 1 ? 0 : (byte1 & 0xf0) >> 4)));
|
---|
| 89 | out.append(l > 1 ? enc.charAt((byte1 & 0x0f) << 2 | (l == 2 ? 0 : (byte2 & 0xc0) >> 6)) : '=');
|
---|
| 90 | out.append(l > 2 ? enc.charAt(byte2 & 0x3f) : '=');
|
---|
[662] | 91 | }
|
---|
| 92 | return out.toString();
|
---|
| 93 | }
|
---|
[626] | 94 | }
|
---|