source: other-projects/FileTransfer-WebSocketPair/GXTWebsocketClient/src/com/sksamuel/gwt/websockets/Base64Utils.java@ 31449

Last change on this file since 31449 was 31449, checked in by davidb, 7 years ago

Adding three project folders for Nathan Kelly's 2016/2017 summer project, experimenting with Websockets for File Transfer with GWT and Sencha GXT

File size: 6.2 KB
Line 
1/*
2 * Copyright 2009 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package com.sksamuel.gwt.websockets;
17
18/**
19 * A utility to decode and encode byte arrays as Strings, using only "safe"
20 * characters.
21 */
22public class Base64Utils {
23
24 /**
25 * An array mapping size but values to the characters that will be used to
26 * represent them. Note that this is not identical to the set of characters
27 * used by MIME-Base64.
28 */
29 private static final char[] base64Chars = new char[] {
30 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
31 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
32 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
33 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
34 '4', '5', '6', '7', '8', '9', '$', '_'};
35
36 /**
37 * An array mapping legal base 64 characters [a-zA-Z0-9$_] to their associated
38 * 6-bit values. The source indices will be given by 7-bit ASCII characters,
39 * thus the array size needs to be 128 (actually 123 would suffice for the
40 * given set of characters in use).
41 */
42 private static final byte[] base64Values = new byte[128];
43
44 /**
45 * Initialize the base 64 encoder values.
46 */
47 static {
48 // Invert the mapping (i -> base64Chars[i])
49 for (int i = 0; i < base64Chars.length; i++) {
50 base64Values[base64Chars[i]] = (byte) i;
51 }
52 }
53
54 /**
55 * Decode a base64 string into a byte array.
56 *
57 * @param data the encoded data.
58 * @return a byte array.
59 * @see #fromBase64(String)
60 */
61 public static byte[] fromBase64(String data) {
62 if (data == null) {
63 return null;
64 }
65
66 int len = data.length();
67 assert (len % 4) == 0;
68
69 if (len == 0) {
70 return new byte[0];
71 }
72
73 char[] chars = new char[len];
74 data.getChars(0, len, chars, 0);
75
76 int olen = 3 * (len / 4);
77 if (chars[len - 2] == '=') {
78 --olen;
79 }
80 if (chars[len - 1] == '=') {
81 --olen;
82 }
83
84 byte[] bytes = new byte[olen];
85
86 int iidx = 0;
87 int oidx = 0;
88 while (iidx < len) {
89 int c0 = base64Values[chars[iidx++] & 0xff];
90 int c1 = base64Values[chars[iidx++] & 0xff];
91 int c2 = base64Values[chars[iidx++] & 0xff];
92 int c3 = base64Values[chars[iidx++] & 0xff];
93 int c24 = (c0 << 18) | (c1 << 12) | (c2 << 6) | c3;
94
95 bytes[oidx++] = (byte) (c24 >> 16);
96 if (oidx == olen) {
97 break;
98 }
99 bytes[oidx++] = (byte) (c24 >> 8);
100 if (oidx == olen) {
101 break;
102 }
103 bytes[oidx++] = (byte) c24;
104 }
105
106 return bytes;
107 }
108
109 /**
110 * Decode a base64 string into a long value.
111 */
112 public static long longFromBase64(String value) {
113 int pos = 0;
114 long longVal = base64Values[value.charAt(pos++)];
115 int len = value.length();
116 while (pos < len) {
117 longVal <<= 6;
118 longVal |= base64Values[value.charAt(pos++)];
119 }
120 return longVal;
121 }
122
123 /**
124 * Converts a byte array into a base 64 encoded string. Null is encoded as
125 * null, and an empty array is encoded as an empty string. Otherwise, the byte
126 * data is read 3 bytes at a time, with bytes off the end of the array padded
127 * with zeros. Each 24-bit chunk is encoded as 4 characters from the sequence
128 * [A-Za-z0-9$_]. If one of the source positions consists entirely of padding
129 * zeros, an '=' character is used instead.
130 *
131 * @param data a byte array, which may be null or empty
132 * @return a String
133 */
134 public static String toBase64(byte[] data) {
135 if (data == null) {
136 return null;
137 }
138
139 int len = data.length;
140 if (len == 0) {
141 return "";
142 }
143
144 int olen = 4 * ((len + 2) / 3);
145 char[] chars = new char[olen];
146
147 int iidx = 0;
148 int oidx = 0;
149 int charsLeft = len;
150 while (charsLeft > 0) {
151 int b0 = data[iidx++] & 0xff;
152 int b1 = (charsLeft > 1) ? data[iidx++] & 0xff : 0;
153 int b2 = (charsLeft > 2) ? data[iidx++] & 0xff : 0;
154 int b24 = (b0 << 16) | (b1 << 8) | b2;
155
156 int c0 = (b24 >> 18) & 0x3f;
157 int c1 = (b24 >> 12) & 0x3f;
158 int c2 = (b24 >> 6) & 0x3f;
159 int c3 = b24 & 0x3f;
160
161 chars[oidx++] = base64Chars[c0];
162 chars[oidx++] = base64Chars[c1];
163 chars[oidx++] = (charsLeft > 1) ? base64Chars[c2] : '=';
164 chars[oidx++] = (charsLeft > 2) ? base64Chars[c3] : '=';
165
166 charsLeft -= 3;
167 }
168
169 return new String(chars);
170 }
171
172 /**
173 * Return a string containing a base-64 encoded version of the given long
174 * value. Leading groups of all zero bits are omitted.
175 */
176 public static String toBase64(long value) {
177 // Convert to ints early to avoid need for long ops
178 int low = (int) (value & 0xffffffff);
179 int high = (int) (value >> 32);
180
181 StringBuilder sb = new StringBuilder();
182 boolean haveNonZero = base64Append(sb, (high >> 28) & 0xf, false);
183 haveNonZero = base64Append(sb, (high >> 22) & 0x3f, haveNonZero);
184 haveNonZero = base64Append(sb, (high >> 16) & 0x3f, haveNonZero);
185 haveNonZero = base64Append(sb, (high >> 10) & 0x3f, haveNonZero);
186 haveNonZero = base64Append(sb, (high >> 4) & 0x3f, haveNonZero);
187 int v = ((high & 0xf) << 2) | ((low >> 30) & 0x3);
188 haveNonZero = base64Append(sb, v, haveNonZero);
189 haveNonZero = base64Append(sb, (low >> 24) & 0x3f, haveNonZero);
190 haveNonZero = base64Append(sb, (low >> 18) & 0x3f, haveNonZero);
191 haveNonZero = base64Append(sb, (low >> 12) & 0x3f, haveNonZero);
192 base64Append(sb, (low >> 6) & 0x3f, haveNonZero);
193 base64Append(sb, low & 0x3f, true);
194
195 return sb.toString();
196 }
197
198 private static boolean base64Append(StringBuilder sb, int digit,
199 boolean haveNonZero) {
200 if (digit > 0) {
201 haveNonZero = true;
202 }
203 if (haveNonZero) {
204 sb.append(base64Chars[digit]);
205 }
206 return haveNonZero;
207 }
208}
Note: See TracBrowser for help on using the repository browser.