~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/solrj/src/java/org/apache/solr/common/util/Base64.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
package org.apache.solr.common.util;
18
 
 
19
 
/**
20
 
 * Static methods for translating Base64 encoded strings to byte arrays
21
 
 * and vice-versa. 
22
 
 */
23
 
 
24
 
public class Base64 {
25
 
  /**
26
 
   * This array is a lookup table that translates 6-bit positive integer
27
 
   * index values into their "Base64 Alphabet" equivalents as specified
28
 
   * in Table 1 of RFC 2045.
29
 
   */
30
 
  private static final char intToBase64[] = {
31
 
          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
32
 
          'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
33
 
          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
34
 
          'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
35
 
          '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
36
 
  };
37
 
 
38
 
  /**
39
 
   * This array is a lookup table that translates unicode characters
40
 
   * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045)
41
 
   * into their 6-bit positive integer equivalents.  Characters that
42
 
   * are not in the Base64 alphabet but fall within the bounds of the
43
 
   * array are translated to -1.
44
 
   */
45
 
  private static final byte base64ToInt[] = {
46
 
          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47
 
          -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48
 
          -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
49
 
          55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4,
50
 
          5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
51
 
          24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34,
52
 
          35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
53
 
  };
54
 
 
55
 
  public static String byteArrayToBase64(byte[] a, int offset, int len) {
56
 
    int aLen = len;
57
 
    int numFullGroups = aLen / 3;
58
 
    int numBytesInPartialGroup = aLen - 3 * numFullGroups;
59
 
    int resultLen = 4 * ((aLen + 2) / 3);
60
 
    StringBuffer result = new StringBuffer(resultLen);
61
 
    char[] intToAlpha = intToBase64;
62
 
 
63
 
    // Translate all full groups from byte array elements to Base64
64
 
    int inCursor = offset;
65
 
    for (int i = 0; i < numFullGroups; i++) {
66
 
      int byte0 = a[inCursor++] & 0xff;
67
 
      int byte1 = a[inCursor++] & 0xff;
68
 
      int byte2 = a[inCursor++] & 0xff;
69
 
      result.append(intToAlpha[byte0 >> 2]);
70
 
      result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
71
 
      result.append(intToAlpha[(byte1 << 2) & 0x3f | (byte2 >> 6)]);
72
 
      result.append(intToAlpha[byte2 & 0x3f]);
73
 
    }
74
 
 
75
 
    // Translate partial group if present
76
 
    if (numBytesInPartialGroup != 0) {
77
 
      int byte0 = a[inCursor++] & 0xff;
78
 
      result.append(intToAlpha[byte0 >> 2]);
79
 
      if (numBytesInPartialGroup == 1) {
80
 
        result.append(intToAlpha[(byte0 << 4) & 0x3f]);
81
 
        result.append("==");
82
 
      } else {
83
 
        // assert numBytesInPartialGroup == 2;
84
 
        int byte1 = a[inCursor++] & 0xff;
85
 
        result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]);
86
 
        result.append(intToAlpha[(byte1 << 2) & 0x3f]);
87
 
        result.append('=');
88
 
      }
89
 
    }
90
 
    return result.toString();
91
 
  }
92
 
 
93
 
  public static byte[] base64ToByteArray(String s) {
94
 
    byte[] alphaToInt = base64ToInt;
95
 
    int sLen = s.length();
96
 
    int numGroups = sLen / 4;
97
 
    if (4 * numGroups != sLen)
98
 
      throw new IllegalArgumentException(
99
 
              "String length must be a multiple of four.");
100
 
    int missingBytesInLastGroup = 0;
101
 
    int numFullGroups = numGroups;
102
 
    if (sLen != 0) {
103
 
      if (s.charAt(sLen - 1) == '=') {
104
 
        missingBytesInLastGroup++;
105
 
        numFullGroups--;
106
 
      }
107
 
      if (s.charAt(sLen - 2) == '=')
108
 
        missingBytesInLastGroup++;
109
 
    }
110
 
    byte[] result = new byte[3 * numGroups - missingBytesInLastGroup];
111
 
 
112
 
    // Translate all full groups from base64 to byte array elements
113
 
    int inCursor = 0, outCursor = 0;
114
 
    for (int i = 0; i < numFullGroups; i++) {
115
 
      int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
116
 
      int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
117
 
      int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
118
 
      int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt);
119
 
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
120
 
      result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
121
 
      result[outCursor++] = (byte) ((ch2 << 6) | ch3);
122
 
    }
123
 
 
124
 
    // Translate partial group, if present
125
 
    if (missingBytesInLastGroup != 0) {
126
 
      int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt);
127
 
      int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt);
128
 
      result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4));
129
 
 
130
 
      if (missingBytesInLastGroup == 1) {
131
 
        int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt);
132
 
        result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
133
 
      }
134
 
    }
135
 
    // assert inCursor == s.length()-missingBytesInLastGroup;
136
 
    // assert outCursor == result.length;
137
 
    return result;
138
 
  }
139
 
 
140
 
  /**
141
 
   * Translates the specified character, which is assumed to be in the
142
 
   * "Base 64 Alphabet" into its equivalent 6-bit positive integer.
143
 
   *
144
 
   * @throw IllegalArgumentException or ArrayOutOfBoundsException if
145
 
   * c is not in the Base64 Alphabet.
146
 
   */
147
 
  private static int base64toInt(char c, byte[] alphaToInt) {
148
 
    int result = alphaToInt[c];
149
 
    if (result < 0)
150
 
      throw new IllegalArgumentException("Illegal character " + c);
151
 
    return result;
152
 
  }
153
 
}