Debian Codegraph @

src/com/google/debugging/sourcemap/Base64.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.debugging.sourcemap;
18
19 import java.util.Arrays;
20
21 /**
22 * A utility class for working with Base64 values.
23 * @author [email protected] (John Lenz)
24 */
25 public final class Base64 {
26
27 // This is a utility class
28 private Base64() {}
29
30 /**
31 * A map used to convert integer values in the range 0-63 to their base64
32 * values.
33 */
34 private static final String BASE64_MAP =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
36 "abcdefghijklmnopqrstuvwxyz" +
37 "0123456789+/";
38
39 /**
40 * A map used to convert base64 character into integer values.
41 */
42 private static final int[] BASE64_DECODE_MAP = new int[256];
43 static {
44 Arrays.fill(BASE64_DECODE_MAP, -1);
45 for (int i = 0; i < BASE64_MAP.length(); i++) {
46 BASE64_DECODE_MAP[BASE64_MAP.charAt(i)] = i;
47 }
48 }
49
50 /**
51 * @param value A value in the range of 0-63.
52 * @return a base64 digit.
53 */
54 public static char toBase64(int value) {
55 assert (value <= 63 && value >= 0) : "value out of range:" + value;
56 return BASE64_MAP.charAt(value);
57 }
58
59 /**
60 * @param c A base64 digit.
61 * @return A value in the range of 0-63.
62 */
63 public static int fromBase64(char c) {
64 int result = BASE64_DECODE_MAP[c];
65 assert (result != -1) : "invalid char";
66 return BASE64_DECODE_MAP[c];
67 }
68
69 /**
70 * @param value an integer to base64 encode.
71 * @return the six digit long base64 encoded value of the integer.
72 */
73 public static String base64EncodeInt(int value) {
74 char[] c = new char[6];
75 for (int i = 0; i < 5; i++) {
76 c[i] = Base64.toBase64((value >> (26 - i * 6)) & 0x3f);
77 }
78 c[5] = Base64.toBase64((value << 4) & 0x3f);
79 return new String(c);
80 }
81 }