You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2009/07/21 10:00:52 UTC
svn commit: r796172 [1/3] - in /commons/proper/codec/trunk: ./
src/java/org/apache/commons/codec/ src/java/org/apache/commons/codec/binary/
src/java/org/apache/commons/codec/net/ src/test/org/apache/commons/codec/
src/test/org/apache/commons/codec/bina...
Author: ggregory
Date: Tue Jul 21 08:00:52 2009
New Revision: 796172
URL: http://svn.apache.org/viewvc?rev=796172&view=rev
Log:
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.
Added:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/RequiredCharsetNames.java
- copied, changed from r795566, commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/CharacterEncodingNames.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/StringBytesUtilsTest.java
Removed:
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/CharacterEncodingNames.java
Modified:
commons/proper/codec/trunk/ (props changed)
commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/BCodec.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QCodec.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/RFC1522Codec.java
commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64Test.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/BCodecTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QCodecTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/QuotedPrintableCodecTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/RFC1522CodecTest.java
commons/proper/codec/trunk/src/test/org/apache/commons/codec/net/URLCodecTest.java
Propchange: commons/proper/codec/trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Tue Jul 21 08:00:52 2009
@@ -12,3 +12,4 @@
cobertura.ser
.settings
bin
+build.properties
Copied: commons/proper/codec/trunk/src/java/org/apache/commons/codec/RequiredCharsetNames.java (from r795566, commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/CharacterEncodingNames.java)
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/RequiredCharsetNames.java?p2=commons/proper/codec/trunk/src/java/org/apache/commons/codec/RequiredCharsetNames.java&p1=commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/CharacterEncodingNames.java&r1=795566&r2=796172&rev=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/CharacterEncodingNames.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/RequiredCharsetNames.java Tue Jul 21 08:00:52 2009
@@ -15,30 +15,59 @@
* limitations under the License.
*/
-package org.apache.commons.codec.net;
+package org.apache.commons.codec;
/**
* Character encoding names required of every implementation of the Java platform.
*
- * According to the Java documentation <a
+ * From the Java documentation <a
* href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding names
* </a>:
* <p>
* <cite>Every implementation of the Java platform is required to support the following character encodings. Consult the
- * release documentation for your implementation to see if any other encodings are supported. </cite>
+ * release documentation for your implementation to see if any other encodings are supported. Consult the release
+ * documentation for your implementation to see if any other encodings are supported. </cite>
* </p>
*
- * This interface is private to the package since it perhaps would best belong in the [lang] project with other required
- * encoding names. As is, this interface only defines the names used in this package. Even if a similar interface is
- * defined in [lang], it is not forseen that [codec] would be made to depend on [lang].
+ * <ul>
+ * <li><code>US-ASCII</code><br/>
+ * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set.</li>
+ * <li><code>ISO-8859-1</code><br/>
+ * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.</li>
+ * <li><code>UTF-8</code><br/>
+ * Eight-bit Unicode Transformation Format.</li>
+ * <li><code>UTF-16BE</code><br/>
+ * Sixteen-bit Unicode Transformation Format, big-endian byte order.</li>
+ * <li><code>UTF-16LE</code><br/>
+ * Sixteen-bit Unicode Transformation Format, little-endian byte order.</li>
+ * <li><code>UTF-16</code><br/>
+ * Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order
+ * accepted on input, big-endian used on output.)</li>
+ * </ul>
+ *
+ * This perhaps would best belong in the [lang] project. Even if a similar interface is defined in [lang], it is not
+ * forseen that [codec] would be made to depend on [lang].
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character encoding
- * names </a>
+ * names </a>
* @author Apache Software Foundation
* @since 1.4
* @version $Id$
*/
-class CharacterEncodingNames {
+public class RequiredCharsetNames {
+ /**
+ * <p>
+ * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1.
+ * </p>
+ * <p>
+ * Every implementation of the Java platform is required to support this character encoding.
+ * </p>
+ *
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ */
+ public static final String ISO_8859_1 = "ISO-8859-1";
+
/**
* <p>
* Seven-bit ASCII, also known as ISO646-US, also known as the Basic Latin block of the Unicode character set.
@@ -48,9 +77,49 @@
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
+ * encoding names </a>
+ */
+ public static final String US_ASCII = "US-ASCII";
+
+ /**
+ * <p>
+ * Sixteen-bit Unicode Transformation Format, The byte order specified by a mandatory initial byte-order mark
+ * (either order accepted on input, big-endian used on output)
+ * </p>
+ * <p>
+ * Every implementation of the Java platform is required to support this character encoding.
+ * </p>
+ *
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ */
+ public static final String UTF_16 = "UTF-16";
+
+ /**
+ * <p>
+ * Sixteen-bit Unicode Transformation Format, big-endian byte order.
+ * </p>
+ * <p>
+ * Every implementation of the Java platform is required to support this character encoding.
+ * </p>
+ *
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ */
+ public static final String UTF_16BE = "UTF-16BE";
+
+ /**
+ * <p>
+ * Sixteen-bit Unicode Transformation Format, little-endian byte order.
+ * </p>
+ * <p>
+ * Every implementation of the Java platform is required to support this character encoding.
+ * </p>
+ *
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
*/
- static final String US_ASCII = "US-ASCII";
+ public static final String UTF_16LE = "UTF-16LE";
/**
* <p>
@@ -61,7 +130,7 @@
* </p>
*
* @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
- * encoding names </a>
+ * encoding names </a>
*/
- static final String UTF8 = "UTF-8";
+ public static final String UTF_8 = "UTF-8";
}
\ No newline at end of file
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Base64.java Tue Jul 21 08:00:52 2009
@@ -17,7 +17,6 @@
package org.apache.commons.codec.binary;
-import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import org.apache.commons.codec.BinaryDecoder;
@@ -328,12 +327,7 @@
}
this.decodeSize = this.encodeSize - 1;
if (containsBase64Byte(lineSeparator)) {
- String sep;
- try {
- sep = new String(lineSeparator, "UTF-8");
- } catch (UnsupportedEncodingException uee) {
- sep = new String(lineSeparator);
- }
+ String sep = StringBytesUtils.newStringUtf8(lineSeparator);
throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]");
}
this.encodeTable = urlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
@@ -738,8 +732,8 @@
len += (1 + (len / CHUNK_SIZE)) * CHUNK_SEPARATOR.length;
}
if (len > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("Input array too big, output array would be bigger than Integer.MAX_VALUE="
- + Integer.MAX_VALUE);
+ throw new IllegalArgumentException("Input array too big, output array would be bigger than Integer.MAX_VALUE=" +
+ Integer.MAX_VALUE);
}
byte[] buf = new byte[(int) len];
b64.setInitialBuffer(buf, 0, buf.length);
Added: commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java?rev=796172&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java (added)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/StringBytesUtils.java Tue Jul 21 08:00:52 2009
@@ -0,0 +1,272 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.codec.binary;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.commons.codec.RequiredCharsetNames;
+
+/**
+ * Converts String to bytes using the encodings required by the Java specification.
+ *
+ * @see RequiredCharsetNames
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @version $Id: $
+ */
+public class StringBytesUtils {
+
+ /**
+ * Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
+ * byte array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesIso8859_1(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.ISO_8859_1);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the US-ASCII charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUsAscii(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.US_ASCII);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16 charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16Be(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16BE);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-16LE charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf16Le(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_16LE);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the UTF-8 charset, storing the result into a new byte
+ * array.
+ *
+ * @param string
+ * the String to encode
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when the charset is missing, which should be never according the the Java specification.
+ * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/package-summary.html#charenc">JRE character
+ * encoding names </a>
+ * @see #getSupportedBytes(String, String)
+ */
+ public static byte[] getBytesUtf8(String string) {
+ return StringBytesUtils.getSupportedBytes(string, RequiredCharsetNames.UTF_8);
+ }
+
+ /**
+ * Encodes the given string into a sequence of bytes using the named charset, storing the result into a new byte
+ * array.
+ * <p>
+ * This method catches {@link UnsupportedEncodingException} and rethrows it as {@link IllegalStateException}, which
+ * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
+ * </p>
+ *
+ * @param string
+ * the String to encode
+ * @param charsetName
+ * The name of a required {@link java.nio.charset.Charset}
+ * @return encoded bytes
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
+ * required charset name.
+ * @see RequiredCharsetNames
+ * @see String#getBytes(String)
+ */
+ public static byte[] getSupportedBytes(String string, String charsetName) {
+ try {
+ return string.getBytes(charsetName);
+ } catch (UnsupportedEncodingException e) {
+ throw StringBytesUtils.newIllegalStateException(charsetName, e);
+ }
+ }
+
+ private static IllegalStateException newIllegalStateException(String charsetName, UnsupportedEncodingException e) {
+ return new IllegalStateException(charsetName + ": " + e);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the given charset.
+ * <p>
+ * This method catches {@link UnsupportedEncodingException} and re-throws it as {@link IllegalStateException}, which
+ * should never happen for a required charset name. Use this method when the encoding is required to be in the JRE.
+ * </p>
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @param charsetName
+ * The name of a required {@link java.nio.charset.Charset}
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen for a
+ * required charset name.
+ * @see RequiredCharsetNames
+ * @see String#String(byte[], String)
+ */
+ public static String newString(byte[] bytes, String charsetName) {
+ try {
+ return new String(bytes, charsetName);
+ } catch (UnsupportedEncodingException e) {
+ throw StringBytesUtils.newIllegalStateException(charsetName, e);
+ }
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the ISO-8859-1 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringIso8859_1(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.ISO_8859_1);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the US-ASCII charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUsAscii(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.US_ASCII);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16BE charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16Be(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16BE);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-16LE charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf16Le(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_16LE);
+ }
+
+ /**
+ * Constructs a new <code>String</code> by decoding the specified array of bytes using the UTF-8 charset.
+ *
+ * @param bytes
+ * The bytes to be decoded into characters
+ * @throws IllegalStateException
+ * Thrown when a {@link UnsupportedEncodingException} is caught, which should never happen since the
+ * charset is required.
+ */
+ public static String newStringUtf8(byte[] bytes) {
+ return StringBytesUtils.newString(bytes, RequiredCharsetNames.UTF_8);
+ }
+
+ private StringBytesUtils() {
+ // noop, cannot instantiate.
+ }
+}
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/BCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/BCodec.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/BCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/BCodec.java Tue Jul 21 08:00:52 2009
@@ -21,6 +21,7 @@
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.RequiredCharsetNames;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
import org.apache.commons.codec.binary.Base64;
@@ -54,7 +55,7 @@
* Default constructor.
*/
public BCodec() {
- this(CharacterEncodingNames.UTF8);
+ this(RequiredCharsetNames.UTF_8);
}
/**
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QCodec.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QCodec.java Tue Jul 21 08:00:52 2009
@@ -22,6 +22,7 @@
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.RequiredCharsetNames;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
@@ -109,7 +110,7 @@
* Default constructor.
*/
public QCodec() {
- this(CharacterEncodingNames.UTF8);
+ this(RequiredCharsetNames.UTF_8);
}
/**
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java Tue Jul 21 08:00:52 2009
@@ -25,8 +25,10 @@
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.RequiredCharsetNames;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
+import org.apache.commons.codec.binary.StringBytesUtils;
/**
* <p>
@@ -91,7 +93,7 @@
* Default constructor.
*/
public QuotedPrintableCodec() {
- this(CharacterEncodingNames.UTF8);
+ this(RequiredCharsetNames.UTF_8);
}
/**
@@ -278,7 +280,7 @@
if (pString == null) {
return null;
}
- return new String(decode(pString.getBytes(CharacterEncodingNames.US_ASCII)), charset);
+ return new String(decode(StringBytesUtils.getBytesUsAscii(pString)), charset);
}
/**
@@ -383,6 +385,6 @@
if (pString == null) {
return null;
}
- return new String(encode(pString.getBytes(charset)), CharacterEncodingNames.US_ASCII);
+ return StringBytesUtils.newStringUsAscii(encode(pString.getBytes(charset)));
}
}
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/RFC1522Codec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/RFC1522Codec.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/RFC1522Codec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/RFC1522Codec.java Tue Jul 21 08:00:52 2009
@@ -21,6 +21,7 @@
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.binary.StringBytesUtils;
/**
* <p>
@@ -76,7 +77,7 @@
buffer.append(getEncoding());
buffer.append('?');
byte [] rawdata = doEncoding(text.getBytes(charset));
- buffer.append(new String(rawdata, CharacterEncodingNames.US_ASCII));
+ buffer.append(StringBytesUtils.newStringUsAscii(rawdata));
buffer.append("?=");
return buffer.toString();
}
@@ -125,7 +126,7 @@
}
from = to + 1;
to = text.indexOf("?", from);
- byte[] data = text.substring(from, to).getBytes(CharacterEncodingNames.US_ASCII);
+ byte[] data = StringBytesUtils.getBytesUsAscii(text.substring(from, to));
data = doDecoding(data);
return new String(data, charset);
}
Modified: commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java (original)
+++ commons/proper/codec/trunk/src/java/org/apache/commons/codec/net/URLCodec.java Tue Jul 21 08:00:52 2009
@@ -25,8 +25,10 @@
import org.apache.commons.codec.BinaryEncoder;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.EncoderException;
+import org.apache.commons.codec.RequiredCharsetNames;
import org.apache.commons.codec.StringDecoder;
import org.apache.commons.codec.StringEncoder;
+import org.apache.commons.codec.binary.StringBytesUtils;
/**
* <p>Implements the 'www-form-urlencoded' encoding scheme,
@@ -92,7 +94,7 @@
* Default constructor.
*/
public URLCodec() {
- this(CharacterEncodingNames.UTF8);
+ this(RequiredCharsetNames.UTF_8);
}
/**
@@ -211,27 +213,24 @@
return decodeUrl(bytes);
}
-
/**
- * Encodes a string into its URL safe form using the specified
- * string charset. Unsafe characters are escaped.
- *
- * @param pString string to convert to a URL safe form
- * @param charset the charset for pString
+ * Encodes a string into its URL safe form using the specified string charset. Unsafe characters are escaped.
+ *
+ * @param pString
+ * string to convert to a URL safe form
+ * @param charset
+ * the charset for pString
* @return URL safe string
- * @throws UnsupportedEncodingException Thrown if charset is not
- * supported
+ * @throws UnsupportedEncodingException
+ * Thrown if charset is not supported
*/
- public String encode(String pString, String charset)
- throws UnsupportedEncodingException
- {
+ public String encode(String pString, String charset) throws UnsupportedEncodingException {
if (pString == null) {
return null;
}
- return new String(encode(pString.getBytes(charset)), CharacterEncodingNames.US_ASCII);
+ return StringBytesUtils.newStringUsAscii(encode(pString.getBytes(charset)));
}
-
/**
* Encodes a string into its URL safe form using the default string
* charset. Unsafe characters are escaped.
@@ -266,16 +265,13 @@
* @throws UnsupportedEncodingException Thrown if charset is not
* supported
*/
- public String decode(String pString, String charset)
- throws DecoderException, UnsupportedEncodingException
- {
+ public String decode(String pString, String charset) throws DecoderException, UnsupportedEncodingException {
if (pString == null) {
return null;
}
- return new String(decode(pString.getBytes(CharacterEncodingNames.US_ASCII)), charset);
+ return new String(decode(StringBytesUtils.getBytesUsAscii(pString)), charset);
}
-
/**
* Decodes a URL safe string into its original form using the default
* string charset. Escaped characters are converted back to their
Added: commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java?rev=796172&view=auto
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java (added)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/RequiredCharsetNamesTest.java Tue Jul 21 08:00:52 2009
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 1993-2003 SEAGULL
+ *
+ * RequiredCharsetNamesTest.java
+ * Created on Jul 20, 2009, 6:08:58 PM
+ *
+ */
+
+package org.apache.commons.codec;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+/**
+ * Sanity checks.
+ *
+ * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
+ * @version $Id: $
+ */
+public class RequiredCharsetNamesTest extends TestCase {
+
+ public void testIso8859_1() {
+ Assert.assertEquals("ISO-8859-1", RequiredCharsetNames.ISO_8859_1);
+ }
+
+ public void testUsAscii() {
+ Assert.assertEquals("US-ASCII", RequiredCharsetNames.US_ASCII);
+ }
+
+ public void testUtf16() {
+ Assert.assertEquals("UTF-16", RequiredCharsetNames.UTF_16);
+ }
+
+ public void testUtf16Be() {
+ Assert.assertEquals("UTF-16BE", RequiredCharsetNames.UTF_16BE);
+ }
+
+ public void testUtf16Le() {
+ Assert.assertEquals("UTF-16LE", RequiredCharsetNames.UTF_16LE);
+ }
+
+ public void testUtf8() {
+ Assert.assertEquals("UTF-8", RequiredCharsetNames.UTF_8);
+ }
+
+}
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64InputStreamTest.java Tue Jul 21 08:00:52 2009
@@ -36,8 +36,6 @@
private static final String STRING_FIXTURE = "Hello World";
- private static final String UTF_8_NAME = "UTF-8";
-
/**
* Construct a new instance of this test case.
*
@@ -69,23 +67,23 @@
*/
public void testBase64InputStreamByChunk() throws Exception {
// Hello World test.
- byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
testByChunk(encoded, decoded, 76, CRLF);
// Single Byte test.
- encoded = "AA==\r\n".getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByChunk(encoded, decoded, 76, CRLF);
// OpenSSL interop test.
- encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
decoded = Base64TestData.DECODED;
testByChunk(encoded, decoded, 64, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = singleLine.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
decoded = Base64TestData.DECODED;
testByChunk(encoded, decoded, 0, LF);
@@ -106,23 +104,23 @@
*/
public void testBase64InputStreamByteByByte() throws Exception {
// Hello World test.
- byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
testByteByByte(encoded, decoded, 76, CRLF);
// Single Byte test.
- encoded = "AA==\r\n".getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByteByByte(encoded, decoded, 76, CRLF);
// OpenSSL interop test.
- encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
decoded = Base64TestData.DECODED;
testByteByByte(encoded, decoded, 64, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = singleLine.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
decoded = Base64TestData.DECODED;
testByteByByte(encoded, decoded, 0, LF);
@@ -252,7 +250,7 @@
* @throws Exception
*/
public void testMarkSupported() throws Exception {
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
// Always returns false for now.
@@ -265,7 +263,7 @@
* @throws Exception
*/
public void testRead0() throws Exception {
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
byte[] buf = new byte[1024];
int bytesRead = 0;
ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
@@ -281,7 +279,7 @@
* for some failure scenarios.
*/
public void testReadNull() throws Exception {
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
try {
@@ -298,7 +296,7 @@
* @throws Exception
*/
public void testReadOutOfBounds() throws Exception {
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
byte[] buf = new byte[1024];
ByteArrayInputStream bin = new ByteArrayInputStream(decoded);
Base64InputStream in = new Base64InputStream(bin, true, 4, new byte[]{0, 0, 0});
@@ -329,6 +327,6 @@
fail("Base64InputStream.read(buf, buf.length - 1, 2) throws IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException e) {
// Expected
- }
+ }
}
}
Modified: commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java?rev=796172&r1=796171&r2=796172&view=diff
==============================================================================
--- commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java (original)
+++ commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base64OutputStreamTest.java Tue Jul 21 08:00:52 2009
@@ -36,8 +36,6 @@
private static final String STRING_FIXTURE = "Hello World";
- private static final String UTF_8_NAME = "UTF-8";
-
/**
* Construct a new instance of this test case.
*
@@ -69,23 +67,23 @@
*/
public void testBase64OutputStreamByChunk() throws Exception {
// Hello World test.
- byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
testByChunk(encoded, decoded, 76, CRLF);
// Single Byte test.
- encoded = "AA==\r\n".getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByChunk(encoded, decoded, 76, CRLF);
// OpenSSL interop test.
- encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
decoded = Base64TestData.DECODED;
testByChunk(encoded, decoded, 64, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = singleLine.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
decoded = Base64TestData.DECODED;
testByChunk(encoded, decoded, 0, LF);
@@ -106,23 +104,23 @@
*/
public void testBase64OutputStreamByteByByte() throws Exception {
// Hello World test.
- byte[] encoded = "SGVsbG8gV29ybGQ=\r\n".getBytes(UTF_8_NAME);
- byte[] decoded = STRING_FIXTURE.getBytes(UTF_8_NAME);
+ byte[] encoded = StringBytesUtils.getBytesUtf8("SGVsbG8gV29ybGQ=\r\n");
+ byte[] decoded = StringBytesUtils.getBytesUtf8(STRING_FIXTURE);
testByteByByte(encoded, decoded, 76, CRLF);
// Single Byte test.
- encoded = "AA==\r\n".getBytes("UTF-8");
+ encoded = StringBytesUtils.getBytesUtf8("AA==\r\n");
decoded = new byte[]{(byte) 0};
testByteByByte(encoded, decoded, 76, CRLF);
// OpenSSL interop test.
- encoded = Base64TestData.ENCODED.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(Base64TestData.ENCODED);
decoded = Base64TestData.DECODED;
testByteByByte(encoded, decoded, 64, LF);
// Single Line test.
String singleLine = Base64TestData.ENCODED.replaceAll("\n", "");
- encoded = singleLine.getBytes(UTF_8_NAME);
+ encoded = StringBytesUtils.getBytesUtf8(singleLine);
decoded = Base64TestData.DECODED;
testByteByByte(encoded, decoded, 0, LF);
@@ -231,10 +229,10 @@
for (int i = 0; i < encoded.length; i++) {
out.write(encoded[i]);
out.flush();
- }
+ }
out.close();
output = byteOut.toByteArray();
- assertTrue("Streaming byte-by-byte flush() base64 decode", Arrays.equals(output, decoded));
+ assertTrue("Streaming byte-by-byte flush() base64 decode", Arrays.equals(output, decoded));
// I always wanted to do this! (wrap encoder with decoder etc etc).
byteOut = new ByteArrayOutputStream();
@@ -289,7 +287,7 @@
fail("Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ioobe) {
// Expected
- }
+ }
}
/**