Skip to content

Commit 4d67ff2

Browse files
committed
Moved package private class CharsetEncodingNames to the main package under the name RequiredCharsetNames. "Charset" reflects the name used in the JRE instead of "encoding". Updated string literals for required charset names ("UTF-8", "US-ASCII", etc) with references to new class static constants. Also created the new class StringBytesUtils (need a better name?) to wrap calls to String#getBytes(String) and String#String(byte[],String) for required charset names, such that it is not required for call sites to catch or re-throw UnsupportedEncodingException since Java requires 6 charsets to be present.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@796172 13f79535-47bb-0310-9956-ffa450edef68
1 parent a870413 commit 4d67ff2

19 files changed

Lines changed: 1241 additions & 673 deletions
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
18+
package org.apache.commons.codec;
19+
20+
/**
21+
* Character encoding names required of every implementation of the Java platform.
22+
*
23+
* From the Java documentation <a
24+
* href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding names
25+
* </a>:
26+
* <p>
27+
* <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
28+
* release documentation for your implementation to see if any other encodings are supported. Consult the release
29+
* documentation for your implementation to see if any other encodings are supported. </cite>
30+
* </p>
31+
*
32+
* <ul>
33+
* <li><code>US-ASCII</code><br/>
34+
* Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
35+
* <li><code>ISO-8859-1</code><br/>
36+
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
37+
* <li><code>UTF-8</code><br/>
38+
* Eight-bit Unicode Transformation Format.</li>
39+
* <li><code>UTF-16BE</code><br/>
40+
* Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
41+
* <li><code>UTF-16LE</code><br/>
42+
* Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
43+
* <li><code>UTF-16</code><br/>
44+
* Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
45+
* accepted on input, big-endian used on output.)</li>
46+
* </ul>
47+
*
48+
* This perhaps would best belong in the [lang] project. Even if a similar interface is defined in [lang], it is not
49+
* forseen that [codec] would be made to depend on [lang].
50+
*
51+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding
52+
* names </a>
53+
* @author Apache Software Foundation
54+
* @since 1.4
55+
* @version $Id$
56+
*/
57+
public class RequiredCharsetNames {
58+
/**
59+
* <p>
60+
* ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
61+
* </p>
62+
* <p>
63+
* Every implementation of the Java platform is required to support this character encoding.
64+
* </p>
65+
*
66+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
67+
* encoding names </a>
68+
*/
69+
public static final String ISO_8859_1 = "ISO-8859-1";
70+
71+
/**
72+
* <p>
73+
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
74+
* </p>
75+
* <p>
76+
* Every implementation of the Java platform is required to support this character encoding.
77+
* </p>
78+
*
79+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
80+
* encoding names </a>
81+
*/
82+
public static final String US_ASCII = "US-ASCII";
83+
84+
/**
85+
* <p>
86+
* Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
87+
* (either order accepted on input, big-endian used on output)
88+
* </p>
89+
* <p>
90+
* Every implementation of the Java platform is required to support this character encoding.
91+
* </p>
92+
*
93+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
94+
* encoding names </a>
95+
*/
96+
public static final String UTF_16 = "UTF-16";
97+
98+
/**
99+
* <p>
100+
* Sixteen-bit Unicode Transformation Format, big-endian byte order.
101+
* </p>
102+
* <p>
103+
* Every implementation of the Java platform is required to support this character encoding.
104+
* </p>
105+
*
106+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
107+
* encoding names </a>
108+
*/
109+
public static final String UTF_16BE = "UTF-16BE";
110+
111+
/**
112+
* <p>
113+
* Sixteen-bit Unicode Transformation Format, little-endian byte order.
114+
* </p>
115+
* <p>
116+
* Every implementation of the Java platform is required to support this character encoding.
117+
* </p>
118+
*
119+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
120+
* encoding names </a>
121+
*/
122+
public static final String UTF_16LE = "UTF-16LE";
123+
124+
/**
125+
* <p>
126+
* Eight-bit Unicode Transformation Format.
127+
* </p>
128+
* <p>
129+
* Every implementation of the Java platform is required to support this character encoding.
130+
* </p>
131+
*
132+
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
133+
* encoding names </a>
134+
*/
135+
public static final String UTF_8 = "UTF-8";
136+
}

src/java/org/apache/commons/codec/binary/Base64.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.commons.codec.binary;
1919

20-
import java.io.UnsupportedEncodingException;
2120
import java.math.BigInteger;
2221

2322
import org.apache.commons.codec.BinaryDecoder;
@@ -328,12 +327,7 @@ public Base64(int lineLength, byte[] lineSeparator, boolean urlSafe) {
328327
}
329328
this.decodeSize = this.encodeSize - 1;
330329
if (containsBase64Byte(lineSeparator)) {
331-
String sep;
332-
try {
333-
sep = new String(lineSeparator, "UTF-8");
334-
} catch (UnsupportedEncodingException uee) {
335-
sep = new String(lineSeparator);
336-
}
330+
String sep = StringBytesUtils.newStringUtf8(lineSeparator);
337331
throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]");
338332
}
339333
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
@@ -738,8 +732,8 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean
738732
len += (1 + (len / CHUNK_SIZE)) * CHUNK_SEPARATOR.length;
739733
}
740734
if (len > Integer.MAX_VALUE) {
741-
throw new IllegalArgumentException("Input array too big, output array would be bigger than Integer.MAX_VALUE="
742-
+ Integer.MAX_VALUE);
735+
throw new IllegalArgumentException("Input array too big, output array would be bigger than Integer.MAX_VALUE=" +
736+
Integer.MAX_VALUE);
743737
}
744738
byte[] buf = new byte[(int) len];
745739
b64.setInitialBuffer(buf, 0, buf.length);

0 commit comments

Comments
 (0)