查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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
14   * under the License.
15   */
16  /*
17   * Written by Robert Harder and released to the public domain, as explained at
18   * https://creativecommons.org/licenses/publicdomain
19   */
20  package io.netty.handler.codec.base64;
21  
22  /**
23   * Enumeration of supported Base64 dialects.
24   * <p>
25   * The internal lookup tables in this class has been derived from
26   * <a href="http://iharder.sourceforge.net/current/java/base64/">Robert Harder's Public Domain
27   * Base64 Encoder/Decoder</a>.
28   */
29  public enum Base64Dialect {
30      /**
31       * Standard Base64 encoding as described in the Section 3 of
32       * <a href="http://www.faqs.org/rfcs/rfc3548.html">RFC3548</a>.
33       */
34      STANDARD(new byte[] {
35              (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
36              (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
37              (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
38              (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
39              (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
40              (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
41              (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
42              (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
43              (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
44              (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
45              (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
46              (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
47              (byte) '8', (byte) '9', (byte) '+', (byte) '/' },
48              new byte[] {
49              -9, -9, -9, -9, -9, -9,
50              -9, -9, -9, // Decimal  0 -  8
51              -5, -5, // Whitespace: Tab and Linefeed
52              -9, -9, // Decimal 11 - 12
53              -5, // Whitespace: Carriage Return
54              -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
55              -9, -9, -9, -9, -9, // Decimal 27 - 31
56              -5, // Whitespace: Space
57              -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
58              62, // Plus sign at decimal 43
59              -9, -9, -9, // Decimal 44 - 46
60              63, // Slash at decimal 47
61              52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
62              -9, -9, -9, // Decimal 58 - 60
63              -1, // Equals sign at decimal 61
64              -9, -9, -9, // Decimal 62 - 64
65               0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
66              14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
67              -9, -9, -9, -9, -9, -9, // Decimal 91 - 96
68              26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
69              39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
70              -9, -9, -9, -9, -9 // Decimal 123 - 127
71           /* -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 128 - 140
72              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 141 - 153
73              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 154 - 166
74              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 167 - 179
75              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 180 - 192
76              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 193 - 205
77              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 206 - 218
78              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 219 - 231
79              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 232 - 244
80              -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9            // Decimal 245 - 255 */
81              }, true),
82      /**
83       * Base64-like encoding that is URL-safe as described in the Section 4 of
84       * <a href="http://www.faqs.org/rfcs/rfc3548.html">RFC3548</a>.  It is
85       * important to note that data encoded this way is <em>not</em> officially
86       * valid Base64, or at the very least should not be called Base64 without
87       * also specifying that is was encoded using the URL-safe dialect.
88       */
89      URL_SAFE(new byte[] {
90              (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
91              (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J',
92              (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O',
93              (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T',
94              (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y',
95              (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd',
96              (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i',
97              (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
98              (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's',
99              (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
100             (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2',
101             (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
102             (byte) '8', (byte) '9', (byte) '-', (byte) '_' },
103             new byte[] {
104             -9, -9, -9, -9, -9, -9,
105             -9, -9, -9, // Decimal  0 -  8
106             -5, -5, // Whitespace: Tab and Linefeed
107             -9, -9, // Decimal 11 - 12
108             -5, // Whitespace: Carriage Return
109             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
110             -9, -9, -9, -9, -9, // Decimal 27 - 31
111             -5, // Whitespace: Space
112             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
113             -9, // Plus sign at decimal 43
114             -9, // Decimal 44
115             62, // Minus sign at decimal 45
116             -9, // Decimal 46
117             -9, // Slash at decimal 47
118             52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
119             -9, -9, -9, // Decimal 58 - 60
120             -1, // Equals sign at decimal 61
121             -9, -9, -9, // Decimal 62 - 64
122              0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
123             14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
124             -9, -9, -9, -9, // Decimal 91 - 94
125             63, // Underscore at decimal 95
126             -9, // Decimal 96
127             26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
128             39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
129             -9, -9, -9, -9, -9, // Decimal 123 - 127
130          /* -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 128 - 140
131             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 141 - 153
132             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 154 - 166
133             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 167 - 179
134             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 180 - 192
135             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 193 - 205
136             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 206 - 218
137             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 219 - 231
138             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 232 - 244
139             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9            // Decimal 245 - 255 */
140             }, false),
141     /**
142      * Special "ordered" dialect of Base64 described in
143      * <a href="http://www.faqs.org/qa/rfcc-1940.html">RFC1940</a>.
144      */
145     ORDERED(new byte[] {
146             (byte) '-', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
147             (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',
148             (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D',
149             (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I',
150             (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
151             (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S',
152             (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
153             (byte) 'Y', (byte) 'Z', (byte) '_', (byte) 'a', (byte) 'b',
154             (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g',
155             (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l',
156             (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q',
157             (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
158             (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z' },
159             new byte[] {
160             -9, -9, -9, -9, -9, -9,
161             -9, -9, -9, // Decimal  0 -  8
162             -5, -5, // Whitespace: Tab and Linefeed
163             -9, -9, // Decimal 11 - 12
164             -5, // Whitespace: Carriage Return
165             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
166             -9, -9, -9, -9, -9, // Decimal 27 - 31
167             -5, // Whitespace: Space
168             -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
169             -9, // Plus sign at decimal 43
170             -9, // Decimal 44
171              0, // Minus sign at decimal 45
172             -9, // Decimal 46
173             -9, // Slash at decimal 47
174              1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // Numbers zero through nine
175             -9, -9, -9, // Decimal 58 - 60
176             -1, // Equals sign at decimal 61
177             -9, -9, -9, // Decimal 62 - 64
178             11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, // Letters 'A' through 'M'
179             24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, // Letters 'N' through 'Z'
180             -9, -9, -9, -9, // Decimal 91 - 94
181             37, // Underscore at decimal 95
182             -9, // Decimal 96
183             38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, // Letters 'a' through 'm'
184             51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // Letters 'n' through 'z'
185             -9, -9, -9, -9, -9 // Decimal 123 - 127
186          /* -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 128 - 140
187             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 141 - 153
188             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 154 - 166
189             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 167 - 179
190             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 180 - 192
191             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 193 - 205
192             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 206 - 218
193             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 219 - 231
194             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,     // Decimal 232 - 244
195             -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9            // Decimal 245 - 255 */
196             }, true);
197 
198     final byte[] alphabet;
199     final byte[] decodabet;
200     final boolean breakLinesByDefault;
201 
202     Base64Dialect(byte[] alphabet, byte[] decodabet, boolean breakLinesByDefault) {
203         this.alphabet = alphabet;
204         this.decodabet = decodabet;
205         this.breakLinesByDefault = breakLinesByDefault;
206     }
207 }