[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 | }
|
---|
| 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);
|
---|
[626] | 47 | out.append(enc.charAt(buf.charAt(0)>>2));
|
---|
[662] | 48 | out.append(enc.charAt(
|
---|
| 49 | (buf.charAt(0) & 0x03) << 4 |
|
---|
| 50 | (l==1?
|
---|
| 51 | 0:
|
---|
| 52 | (buf.charAt(1) & 0xf0) >> 4)));
|
---|
[626] | 53 | out.append(l>1?enc.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2?0:(buf.charAt(2) & 0xc0) >> 6)):'=');
|
---|
| 54 | out.append(l>2?enc.charAt(buf.charAt(2) & 0x3f):'=');
|
---|
[662] | 55 | }
|
---|
| 56 | return out.toString();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[7060] | 59 | /**
|
---|
| 60 | * Encodes all remaining bytes from the specified byte buffer into a new string using the Base64 default encoding scheme.
|
---|
| 61 | * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
|
---|
| 62 | * @param s the source ByteBuffer to encode
|
---|
| 63 | * @return A new string containing the resulting encoded characters.
|
---|
| 64 | */
|
---|
[662] | 65 | public static String encode(ByteBuffer s) {
|
---|
[3840] | 66 | return encode(s, false);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[7060] | 69 | /**
|
---|
| 70 | * Encodes all remaining bytes from the specified byte buffer into a new string using a supported Base64 encoding scheme.
|
---|
| 71 | * Upon return, the source buffer's position will be updated to its limit; its limit will not have been changed.
|
---|
| 72 | * @param s the source ByteBuffer to encode
|
---|
| 73 | * @param urlsafe if {@code true}, uses "base64url" encoding, otherwise use the "base64" default encoding
|
---|
| 74 | * @return A new string containing the resulting encoded characters.
|
---|
| 75 | * @since 3840
|
---|
| 76 | */
|
---|
[3840] | 77 | public static String encode(ByteBuffer s, boolean urlsafe) {
|
---|
[662] | 78 | StringBuilder out = new StringBuilder();
|
---|
[3840] | 79 | String enc = urlsafe ? encUrlSafe : encDefault;
|
---|
[662] | 80 | // Read 3 bytes at a time.
|
---|
| 81 | for (int i = 0; i < (s.limit()+2)/3; ++i) {
|
---|
| 82 | int l = Math.min(3, s.limit()-i*3);
|
---|
| 83 | int byte0 = s.get() & 0xff;
|
---|
| 84 | int byte1 = l>1? s.get() & 0xff : 0;
|
---|
| 85 | int byte2 = l>2? s.get() & 0xff : 0;
|
---|
| 86 |
|
---|
| 87 | out.append(enc.charAt(byte0>>2));
|
---|
| 88 | out.append(enc.charAt(
|
---|
| 89 | (byte0 & 0x03) << 4 |
|
---|
| 90 | (l==1?
|
---|
| 91 | 0:
|
---|
| 92 | (byte1 & 0xf0) >> 4)));
|
---|
| 93 | out.append(l>1?enc.charAt((byte1 & 0x0f) << 2 | (l==2?0:(byte2 & 0xc0) >> 6)):'=');
|
---|
| 94 | out.append(l>2?enc.charAt(byte2 & 0x3f):'=');
|
---|
| 95 | }
|
---|
| 96 | return out.toString();
|
---|
| 97 | }
|
---|
[626] | 98 | }
|
---|