1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.nio.ByteBuffer;
|
---|
5 |
|
---|
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 | */
|
---|
13 | public final class Base64 {
|
---|
14 | // TODO: Remove this class when switching to Java 8 (finally integrated in Java SE as java.util.Base64.Encoder)
|
---|
15 |
|
---|
16 | private Base64() {
|
---|
17 | // Hide default constructor for utils classes
|
---|
18 | }
|
---|
19 |
|
---|
20 | /** "base64": RFC 2045 default encoding */
|
---|
21 | private static String encDefault = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
---|
22 | /** "base64url": RFC 4648 url-safe encoding */
|
---|
23 | private static String encUrlSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
---|
24 |
|
---|
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 | */
|
---|
30 | public static String encode(String s) {
|
---|
31 | return encode(s, false);
|
---|
32 | }
|
---|
33 |
|
---|
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 | */
|
---|
41 | public static String encode(String s, boolean urlsafe) {
|
---|
42 | StringBuilder out = new StringBuilder();
|
---|
43 | String enc = urlsafe ? encUrlSafe : encDefault;
|
---|
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);
|
---|
47 | out.append(enc.charAt(buf.charAt(0) >> 2));
|
---|
48 | out.append(enc.charAt(
|
---|
49 | (buf.charAt(0) & 0x03) << 4 |
|
---|
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) : '=');
|
---|
53 | }
|
---|
54 | return out.toString();
|
---|
55 | }
|
---|
56 |
|
---|
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 | */
|
---|
63 | public static String encode(ByteBuffer s) {
|
---|
64 | return encode(s, false);
|
---|
65 | }
|
---|
66 |
|
---|
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 | */
|
---|
75 | public static String encode(ByteBuffer s, boolean urlsafe) {
|
---|
76 | StringBuilder out = new StringBuilder();
|
---|
77 | String enc = urlsafe ? encUrlSafe : encDefault;
|
---|
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;
|
---|
82 | int byte1 = l > 1 ? s.get() & 0xff : 0;
|
---|
83 | int byte2 = l > 2 ? s.get() & 0xff : 0;
|
---|
84 |
|
---|
85 | out.append(enc.charAt(byte0 >> 2));
|
---|
86 | out.append(enc.charAt(
|
---|
87 | (byte0 & 0x03) << 4 |
|
---|
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) : '=');
|
---|
91 | }
|
---|
92 | return out.toString();
|
---|
93 | }
|
---|
94 | }
|
---|