Return-Path: Delivered-To: apmail-commons-dev-archive@www.apache.org Received: (qmail 26183 invoked from network); 6 Aug 2009 01:30:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 6 Aug 2009 01:30:57 -0000 Received: (qmail 65542 invoked by uid 500); 6 Aug 2009 01:31:03 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 65415 invoked by uid 500); 6 Aug 2009 01:31:03 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 65405 invoked by uid 99); 6 Aug 2009 01:31:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Aug 2009 01:31:03 +0000 X-ASF-Spam-Status: No, hits=-4.0 required=10.0 tests=RCVD_IN_DNSWL_MED,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of GGregory@seagullsoftware.com designates 216.82.253.3 as permitted sender) Received: from [216.82.253.3] (HELO mail151.messagelabs.com) (216.82.253.3) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Aug 2009 01:30:50 +0000 X-VirusChecked: Checked X-Env-Sender: GGregory@seagullsoftware.com X-Msg-Ref: server-6.tower-151.messagelabs.com!1249522226!13987306!1 X-StarScan-Version: 6.0.0; banners=-,-,- X-Originating-IP: [137.134.240.188] Received: (qmail 14675 invoked from network); 6 Aug 2009 01:30:27 -0000 Received: from unknown (HELO postal.rocketsoftware.com) (137.134.240.188) by server-6.tower-151.messagelabs.com with RC4-SHA encrypted SMTP; 6 Aug 2009 01:30:27 -0000 Received: from HQMAIL.rocketsoftware.com ([172.16.37.60]) by RS1062.rocketsoftware.com ([172.16.37.62]) with mapi; Wed, 5 Aug 2009 21:30:14 -0400 From: Gary Gregory To: Commons Developers List Date: Wed, 5 Aug 2009 21:30:13 -0400 Subject: RE: [codec] API Change [was Re: svn commit: r801391 - in /commons/proper/codec/trunk/src...] Thread-Topic: [codec] API Change [was Re: svn commit: r801391 - in /commons/proper/codec/trunk/src...] Thread-Index: AcoWIYVS5cETL72YRYuKQjf/YVdk2QACxrTw Message-ID: <94C476C03BFF5E42AC3518FDAC9643C4E2BE115BFE@HQMAIL.rocketsoftware.com> References: <55afdc850908051607v1ac7effve354d1fdb4160dbe@mail.gmail.com> In-Reply-To: <55afdc850908051607v1ac7effve354d1fdb4160dbe@mail.gmail.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Virus-Checked: Checked by ClamAV on apache.org > -----Original Message----- > From: Niall Pemberton [mailto:niall.pemberton@gmail.com] > Sent: Wednesday, August 05, 2009 4:07 PM > To: dev@commons.apache.org > Subject: [codec] API Change [was Re: svn commit: r801391 - in > /commons/proper/codec/trunk/src...] > > OK so I guess renaming getSupportedBytes() to getBytesUnchecked() > means we need another Codec RC. Its really painful to follow along > with commits like this when theres reformatting changes mixed in with > real code changes. It would make life easier if you could resist and > do the formatting changes separately. Roger that. I will be careful to split commits in the future. Thank you for= following the bouncing ball :) > > Also adding a new instance variable to Hex adds another thread safety > issue to http://issues.apache.org/jira/browse/CODEC-55 > > How about specifying charsetName in the constructor, making it final > and removing the setter? I have made the mutator method private instead. Gary > > Niall > > On Wed, Aug 5, 2009 at 8:55 PM, wrote: > > Author: ggregory > > Date: Wed Aug 5 19:55:54 2009 > > New Revision: 801391 > > > > URL: http://svn.apache.org/viewvc?rev=3D801391&view=3Drev > > Log: > > [CODEC-73] Hex: Make string2byte conversions indepedent of platform > default encoding. > > Also refactor DigestUtils to call a new method on Hex. > > > > Modified: > > > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.= j > ava > > > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Stri= n > gUtils.java > > > commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/Dige= s > tUtils.java > > > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base= 6 > 4Test.java > > > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexT= e > st.java > > > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Stri= n > gUtilsTest.java > > > > Modified: > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.j= a > va > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apac= h > e/commons/codec/binary/Hex.java?rev=3D801391&r1=3D801390&r2=3D801391&view= =3Ddiff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.j= a > va (original) > > +++ > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Hex.j= a > va Wed Aug 5 19:55:54 2009 > > @@ -17,13 +17,17 @@ > > > > package org.apache.commons.codec.binary; > > > > +import java.io.UnsupportedEncodingException; > > + > > import org.apache.commons.codec.BinaryDecoder; > > import org.apache.commons.codec.BinaryEncoder; > > +import org.apache.commons.codec.CharEncoding; > > import org.apache.commons.codec.DecoderException; > > import org.apache.commons.codec.EncoderException; > > > > /** > > - * Hex encoder and decoder. > > + * Hex encoder and decoder. The charset used for certain operation can > be set, the default is set in > > + * {@link #DEFAULT_CHARSET_NAME} > > * > > * @since 1.1 > > * @author Apache Software Foundation > > @@ -31,34 +35,31 @@ > > */ > > public class Hex implements BinaryEncoder, BinaryDecoder { > > > > - /** > > - * Used to build output as Hex > > + /** > > + * Default charset name is {@link CharEncoding#UTF_8} > > + */ > > + public static final String DEFAULT_CHARSET_NAME =3D > CharEncoding.UTF_8; > > + > > + /** > > + * Used to build output as Hex > > + */ > > + private static final char[] DIGITS_LOWER =3D {'0', '1', '2', '3', > '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; > > + > > + /** > > + * Used to build output as Hex > > */ > > - private static final char[] DIGITS_LOWER =3D { > > - '0', '1', '2', '3', '4', '5', '6', '7', > > - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' > > - }; > > - > > - /** > > - * Used to build output as Hex > > - */ > > - private static final char[] DIGITS_UPPER =3D { > > - '0', '1', '2', '3', '4', '5', '6', '7', > > - '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' > > - }; > > - > > - /** > > - * Converts an array of characters representing hexadecimal values > into an > > - * array of bytes of those same values. The returned array will be > half the > > - * length of the passed array, as it takes two characters to > represent any > > - * given byte. An exception is thrown if the passed char array has > an odd > > - * number of elements. > > - * > > - * @param data An array of characters containing hexadecimal digit= s > > - * @return A byte array containing binary data decoded from > > - * the supplied char array. > > - * @throws DecoderException Thrown if an odd number or illegal of > characters > > - * is supplied > > + private static final char[] DIGITS_UPPER =3D {'0', '1', '2', '3', > '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; > > + > > + /** > > + * Converts an array of characters representing hexadecimal values > into an array of bytes of those same values. The > > + * returned array will be half the length of the passed array, as > it takes two characters to represent any given > > + * byte. An exception is thrown if the passed char array has an od= d > number of elements. > > + * > > + * @param data > > + * An array of characters containing hexadecimal digits > > + * @return A byte array containing binary data decoded from the > supplied char array. > > + * @throws DecoderException > > + * Thrown if an odd number or illegal of characters is > supplied > > */ > > public static byte[] decodeHex(char[] data) throws DecoderException > { > > > > @@ -83,22 +84,6 @@ > > } > > > > /** > > - * Converts a hexadecimal character to an integer. > > - * > > - * @param ch A character to convert to an integer digit > > - * @param index The index of the character in the source > > - * @return An integer > > - * @throws DecoderException Thrown if ch is an illegal hex > character > > - */ > > - protected static int toDigit(char ch, int index) throws > DecoderException { > > - int digit =3D Character.digit(ch, 16); > > - if (digit =3D=3D -1) { > > - throw new DecoderException("Illegal hexadecimal charcter " > + ch + " at index " + index); > > - } > > - return digit; > > - } > > - > > - /** > > * Converts an array of bytes into an array of characters > representing the hexadecimal values of each byte in order. > > * The returned array will be double the length of the passed array= , > as it takes two characters to represent any > > * given byte. > > @@ -151,35 +136,87 @@ > > } > > > > /** > > - * Converts an array of character bytes representing hexadecimal > values into an > > - * array of bytes of those same values. The returned array will be > half the > > - * length of the passed array, as it takes two characters to > represent any > > - * given byte. An exception is thrown if the passed char array has > an odd > > - * number of elements. > > - * > > - * @param array An array of character bytes containing hexadecimal > digits > > - * @return A byte array containing binary data decoded from > > - * the supplied byte array (representing characters). > > - * @throws DecoderException Thrown if an odd number of characters > is supplied > > - * to this function > > + * Converts an array of bytes into a String representing the > hexadecimal values of each byte in order. The returned > > + * String will be double the length of the passed array, as it > takes two characters to represent any given byte. > > + * > > + * @param data > > + * a byte[] to convert to Hex characters > > + * @return A String containing hexadecimal characters > > + * @since 1.4 > > + */ > > + public static String encodeHexString(byte[] data) { > > + return new String(encodeHex(data)); > > + } > > + > > + /** > > + * Converts a hexadecimal character to an integer. > > + * > > + * @param ch > > + * A character to convert to an integer digit > > + * @param index > > + * The index of the character in the source > > + * @return An integer > > + * @throws DecoderException > > + * Thrown if ch is an illegal hex character > > + */ > > + protected static int toDigit(char ch, int index) throws > DecoderException { > > + int digit =3D Character.digit(ch, 16); > > + if (digit =3D=3D -1) { > > + throw new DecoderException("Illegal hexadecimal charcter " > + ch + " at index " + index); > > + } > > + return digit; > > + } > > + > > + private String charsetName =3D DEFAULT_CHARSET_NAME; > > + > > + /** > > + * Creates a new codec with the default charset name {@link > #DEFAULT_CHARSET_NAME} > > + */ > > + public Hex() { > > + // use default encoding > > + } > > + > > + /** > > + * Creates a new codec with the given charset name. > > + * > > + * @param csName > > + * the charset name. > > + */ > > + public Hex(String csName) { > > + setCharsetName(csName); > > + } > > + > > + /** > > + * Converts an array of character bytes representing hexadecimal > values into an array of bytes of those same values. > > + * The returned array will be half the length of the passed array, > as it takes two characters to represent any given > > + * byte. An exception is thrown if the passed char array has an od= d > number of elements. > > + * > > + * @param array > > + * An array of character bytes containing hexadecimal > digits > > + * @return A byte array containing binary data decoded from the > supplied byte array (representing characters). > > + * @throws DecoderException > > + * Thrown if an odd number of characters is supplied t= o > this function > > * @see #decodeHex(char[]) > > */ > > public byte[] decode(byte[] array) throws DecoderException { > > - return decodeHex(new String(array).toCharArray()); > > + try { > > + return decodeHex(new String(array, > getCharsetName()).toCharArray()); > > + } catch (UnsupportedEncodingException e) { > > + throw new DecoderException(e.getMessage(), e); > > + } > > } > > - > > + > > /** > > - * Converts a String or an array of character bytes representing > hexadecimal values into an > > - * array of bytes of those same values. The returned array will be > half the > > - * length of the passed String or array, as it takes two character= s > to represent any > > - * given byte. An exception is thrown if the passed char array has > an odd > > - * number of elements. > > - * > > - * @param object A String or, an array of character bytes > containing hexadecimal digits > > - * @return A byte array containing binary data decoded from > > - * the supplied byte array (representing characters). > > - * @throws DecoderException Thrown if an odd number of characters > is supplied > > - * to this function or the object is not a Strin= g > or char[] > > + * Converts a String or an array of character bytes representing > hexadecimal values into an array of bytes of those > > + * same values. The returned array will be half the length of the > passed String or array, as it takes two characters > > + * to represent any given byte. An exception is thrown if the > passed char array has an odd number of elements. > > + * > > + * @param object > > + * A String or, an array of character bytes containing > hexadecimal digits > > + * @return A byte array containing binary data decoded from the > supplied byte array (representing characters). > > + * @throws DecoderException > > + * Thrown if an odd number of characters is supplied t= o > this function or the object is not a String or > > + * char[] > > * @see #decodeHex(char[]) > > */ > > public Object decode(Object object) throws DecoderException { > > @@ -192,38 +229,73 @@ > > } > > > > /** > > - * Converts an array of bytes into an array of bytes for the > characters representing the > > - * hexadecimal values of each byte in order. The returned array > will be > > - * double the length of the passed array, as it takes two > characters to > > + * Converts an array of bytes into an array of bytes for the > characters representing the hexadecimal values of each > > + * byte in order. The returned array will be double the length of > the passed array, as it takes two characters to > > * represent any given byte. > > - * > > - * @param array a byte[] to convert to Hex characters > > + *

> > + * The conversion from hexadecimal characters to the returned byte= s > is performed with the charset named by > > + * {@link #getCharsetName()}. > > + *

> > + * > > + * @param array > > + * a byte[] to convert to Hex characters > > * @return A byte[] containing the bytes of the hexadecimal > characters > > + * @throws IllegalStateException > > + * if the charsetName is invalid. This API throws > {@link IllegalStateException} instead of > > + * {@link UnsupportedEncodingException} for backward > compatibility. > > * @see #encodeHex(byte[]) > > */ > > public byte[] encode(byte[] array) { > > - return new String(encodeHex(array)).getBytes(); > > + return StringUtils.getBytesUnchecked(encodeHexString(array), > getCharsetName()); > > } > > > > /** > > - * Converts a String or an array of bytes into an array of > characters representing the > > - * hexadecimal values of each byte in order. The returned array > will be > > - * double the length of the passed String or array, as it takes tw= o > characters to > > - * represent any given byte. > > - * > > - * @param object a String, or byte[] to convert to Hex characters > > + * Converts a String or an array of bytes into an array of > characters representing the hexadecimal values of each > > + * byte in order. The returned array will be double the length of > the passed String or array, as it takes two > > + * characters to represent any given byte. > > + *

> > + * The conversion from hexadecimal characters to bytes to be > encoded to performed with the charset named by > > + * {@link #getCharsetName()}. > > + *

> > + * > > + * @param object > > + * a String, or byte[] to convert to Hex characters > > * @return A char[] containing hexadecimal characters > > - * @throws EncoderException Thrown if the given object is not a > String or byte[] > > + * @throws EncoderException > > + * Thrown if the given object is not a String or byte[= ] > > * @see #encodeHex(byte[]) > > */ > > - public Object encode(Object object) throws EncoderException { > > + public Object encode(Object object) throws EncoderException { > > try { > > - byte[] byteArray =3D object instanceof String ? ((String) > object).getBytes() : (byte[]) object; > > + byte[] byteArray =3D object instanceof String ? ((String) > object).getBytes(getCharsetName()) : (byte[]) object; > > return encodeHex(byteArray); > > } catch (ClassCastException e) { > > throw new EncoderException(e.getMessage(), e); > > + } catch (UnsupportedEncodingException e) { > > + throw new EncoderException(e.getMessage(), e); > > } > > } > > > > -} > > + /** > > + * Gets the charset name. > > + * > > + * @return the charset name. > > + */ > > + public String getCharsetName() { > > + return this.charsetName; > > + } > > > > + /** > > + * Sets the charset name. > > + * > > + * @param charsetName > > + * the charset name. > > + */ > > + public void setCharsetName(String charsetName) { > > + this.charsetName =3D charsetName; > > + } > > + > > + public String toString() { > > + return super.toString() + "[charsetName=3D" + this.charsetName= + > "]"; > > + } > > +} > > > > Modified: > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Strin= g > Utils.java > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apac= h > e/commons/codec/binary/StringUtils.java?rev=3D801391&r1=3D801390&r2=3D801= 391&vie > w=3Ddiff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Strin= g > Utils.java (original) > > +++ > commons/proper/codec/trunk/src/java/org/apache/commons/codec/binary/Strin= g > Utils.java Wed Aug 5 19:55:54 2009 > > @@ -43,10 +43,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesIso8859_1(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.ISO_8859_1); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.ISO_8859_1); > > } > > > > /** > > @@ -59,10 +59,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesUsAscii(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.US_ASCII); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.US_ASCII); > > } > > > > /** > > @@ -75,10 +75,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesUtf16(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.UTF_16); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.UTF_16); > > } > > > > /** > > @@ -91,10 +91,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesUtf16Be(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.UTF_16BE); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.UTF_16BE); > > } > > > > /** > > @@ -107,10 +107,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesUtf16Le(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.UTF_16LE); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.UTF_16LE); > > } > > > > /** > > @@ -123,10 +123,10 @@ > > * @throws IllegalStateException > > * Thrown when the charset is missing, which should be > never according the the Java specification. > > * @see href=3D"http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/Charset.= htm > l">Standard charsets > > - * @see #getSupportedBytes(String, String) > > + * @see #getBytesUnchecked(String, String) > > */ > > public static byte[] getBytesUtf8(String string) { > > - return StringUtils.getSupportedBytes(string, > CharEncoding.UTF_8); > > + return StringUtils.getBytesUnchecked(string, > CharEncoding.UTF_8); > > } > > > > /** > > @@ -148,7 +148,7 @@ > > * @see CharEncoding > > * @see String#getBytes(String) > > */ > > - public static byte[] getSupportedBytes(String string, String > charsetName) { > > + public static byte[] getBytesUnchecked(String string, String > charsetName) { > > if (string =3D=3D null) { > > return null; > > } > > > > Modified: > commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/Diges= t > Utils.java > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/java/org/apac= h > e/commons/codec/digest/DigestUtils.java?rev=3D801391&r1=3D801390&r2=3D801= 391&vie > w=3Ddiff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/Diges= t > Utils.java (original) > > +++ > commons/proper/codec/trunk/src/java/org/apache/commons/codec/digest/Diges= t > Utils.java Wed Aug 5 19:55:54 2009 > > @@ -198,7 +198,7 @@ > > * @return MD5 digest as a hex string > > */ > > public static String md5Hex(byte[] data) { > > - return new String(Hex.encodeHex(md5(data))); > > + return Hex.encodeHexString(md5(data)); > > } > > > > /** > > @@ -212,7 +212,7 @@ > > * @since 1.4 > > */ > > public static String md5Hex(InputStream data) throws IOException { > > - return new String(Hex.encodeHex(md5(data))); > > + return Hex.encodeHexString(md5(data)); > > } > > > > /** > > @@ -223,7 +223,7 @@ > > * @return MD5 digest as a hex string > > */ > > public static String md5Hex(String data) { > > - return new String(Hex.encodeHex(md5(data))); > > + return Hex.encodeHexString(md5(data)); > > } > > > > /** > > @@ -321,7 +321,7 @@ > > * @since 1.4 > > */ > > public static String sha256Hex(byte[] data) { > > - return new String(Hex.encodeHex(sha256(data))); > > + return Hex.encodeHexString(sha256(data)); > > } > > > > /** > > @@ -338,7 +338,7 @@ > > * @since 1.4 > > */ > > public static String sha256Hex(InputStream data) throws IOException > { > > - return new String(Hex.encodeHex(sha256(data))); > > + return Hex.encodeHexString(sha256(data)); > > } > > > > /** > > @@ -353,7 +353,7 @@ > > * @since 1.4 > > */ > > public static String sha256Hex(String data) { > > - return new String(Hex.encodeHex(sha256(data))); > > + return Hex.encodeHexString(sha256(data)); > > } > > > > /** > > @@ -415,7 +415,7 @@ > > * @since 1.4 > > */ > > public static String sha384Hex(byte[] data) { > > - return new String(Hex.encodeHex(sha384(data))); > > + return Hex.encodeHexString(sha384(data)); > > } > > > > /** > > @@ -432,7 +432,7 @@ > > * @since 1.4 > > */ > > public static String sha384Hex(InputStream data) throws IOException > { > > - return new String(Hex.encodeHex(sha384(data))); > > + return Hex.encodeHexString(sha384(data)); > > } > > > > /** > > @@ -447,7 +447,7 @@ > > * @since 1.4 > > */ > > public static String sha384Hex(String data) { > > - return new String(Hex.encodeHex(sha384(data))); > > + return Hex.encodeHexString(sha384(data)); > > } > > > > /** > > @@ -509,7 +509,7 @@ > > * @since 1.4 > > */ > > public static String sha512Hex(byte[] data) { > > - return new String(Hex.encodeHex(sha512(data))); > > + return Hex.encodeHexString(sha512(data)); > > } > > > > /** > > @@ -526,7 +526,7 @@ > > * @since 1.4 > > */ > > public static String sha512Hex(InputStream data) throws IOException > { > > - return new String(Hex.encodeHex(sha512(data))); > > + return Hex.encodeHexString(sha512(data)); > > } > > > > /** > > @@ -541,7 +541,7 @@ > > * @since 1.4 > > */ > > public static String sha512Hex(String data) { > > - return new String(Hex.encodeHex(sha512(data))); > > + return Hex.encodeHexString(sha512(data)); > > } > > > > /** > > @@ -552,7 +552,7 @@ > > * @return SHA-1 digest as a hex string > > */ > > public static String shaHex(byte[] data) { > > - return new String(Hex.encodeHex(sha(data))); > > + return Hex.encodeHexString(sha(data)); > > } > > > > /** > > @@ -566,7 +566,7 @@ > > * @since 1.4 > > */ > > public static String shaHex(InputStream data) throws IOException { > > - return new String(Hex.encodeHex(sha(data))); > > + return Hex.encodeHexString(sha(data)); > > } > > > > /** > > @@ -577,6 +577,6 @@ > > * @return SHA-1 digest as a hex string > > */ > > public static String shaHex(String data) { > > - return new String(Hex.encodeHex(sha(data))); > > + return Hex.encodeHexString(sha(data)); > > } > > } > > > > Modified: > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base6= 4 > Test.java > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apac= h > e/commons/codec/binary/Base64Test.java?rev=3D801391&r1=3D801390&r2=3D8013= 91&view > =3Ddiff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base6= 4 > Test.java (original) > > +++ > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Base6= 4 > Test.java Wed Aug 5 19:55:54 2009 > > @@ -940,24 +940,24 @@ > > // Very important debugging output should anyone > > // ever need to delve closely into this stuff. > > if (false) { > > - System.out.println("reference: [" + new > String(Hex.encodeHex(ids[i])) + "]"); > > + System.out.println("reference: [" + > Hex.encodeHexString(ids[i]) + "]"); > > System.out.println("standard: [" + > > - new String(Hex.encodeHex(decodedStandard)) + > > + Hex.encodeHexString(decodedStandard) + > > "] From: [" + > > StringUtils.newStringUtf8(standard[i]) + > > "]"); > > System.out.println("safe1: [" + > > - new String(Hex.encodeHex(decodedUrlSafe1)) + > > + Hex.encodeHexString(decodedUrlSafe1) + > > "] From: [" + > > StringUtils.newStringUtf8(urlSafe1[i]) + > > "]"); > > System.out.println("safe2: [" + > > - new String(Hex.encodeHex(decodedUrlSafe2)) + > > + Hex.encodeHexString(decodedUrlSafe2) + > > "] From: [" + > > StringUtils.newStringUtf8(urlSafe2[i]) + > > "]"); > > System.out.println("safe3: [" + > > - new String(Hex.encodeHex(decodedUrlSafe3)) + > > + Hex.encodeHexString(decodedUrlSafe3) + > > "] From: [" + > > StringUtils.newStringUtf8(urlSafe3[i]) + > > "]"); > > > > Modified: > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTe= s > t.java > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apac= h > e/commons/codec/binary/HexTest.java?rev=3D801391&r1=3D801390&r2=3D801391&= view=3Ddi > ff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTe= s > t.java (original) > > +++ > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/HexTe= s > t.java Wed Aug 5 19:55:54 2009 > > @@ -17,9 +17,15 @@ > > > > package org.apache.commons.codec.binary; > > > > +import java.io.UnsupportedEncodingException; > > +import java.nio.charset.Charset; > > import java.util.Arrays; > > +import java.util.Iterator; > > import java.util.Random; > > +import java.util.Set; > > +import java.util.SortedMap; > > > > +import junit.framework.Assert; > > import junit.framework.TestCase; > > > > import org.apache.commons.codec.DecoderException; > > @@ -31,19 +37,112 @@ > > * @author Apache Software Foundation > > * @version $Id$ > > */ > > - > > public class HexTest extends TestCase { > > > > + private final static boolean LOG =3D false; > > + > > public HexTest(String name) { > > super(name); > > } > > - > > - public void testDecodeArrayOddCharacters() { > > + > > + /** > > + * @param data > > + */ > > + private void checkDecodeHexOddCharacters(char[] data) { > > try { > > - new Hex().decode(new byte[] { 65 }); > > + Hex.decodeHex(data); > > fail("An exception wasn't thrown when trying to decode an > odd number of characters"); > > + } catch (DecoderException e) { > > + // Expected exception > > + } > > + } > > + > > + private void log(String s) { > > + if (LOG) { > > + System.out.println(s); > > + } > > + } > > + > > + public void testCustomCharset() throws > UnsupportedEncodingException, DecoderException { > > + SortedMap map =3D Charset.availableCharsets(); > > + Set keys =3D map.keySet(); > > + Iterator iterator =3D keys.iterator(); > > + log("testCustomCharset: Checking " + keys.size() + " > charsets..."); > > + while (iterator.hasNext()) { > > + String name =3D (String) iterator.next(); > > + testCustomCharset(name, "testCustomCharset"); > > + } > > + } > > + > > + /** > > + * @param name > > + * @param parent > > + * TODO > > + * @throws UnsupportedEncodingException > > + * @throws DecoderException > > + */ > > + private void testCustomCharset(String name, String parent) throws > UnsupportedEncodingException, DecoderException { > > + if (charsetSanityCheck(name) =3D=3D false) { > > + log("Interesting Java charset oddity: Charset sanity check > failed for " + name); > > + return; > > + } > > + log(parent + "=3D" + name); > > + Hex customCodec =3D new Hex(name); > > + // source data > > + String sourceString =3D "Hello World"; > > + byte[] sourceBytes =3D sourceString.getBytes(name); > > + // test 1 > > + // encode source to hex string to bytes with charset > > + byte[] actualEncodedBytes =3D customCodec.encode(sourceBytes); > > + // encode source to hex string... > > + String expectedHexString =3D Hex.encodeHexString(sourceBytes); > > + // ... and get the bytes in the expected charset > > + byte[] expectedHexStringBytes =3D > expectedHexString.getBytes(name); > > + Assert.assertTrue(Arrays.equals(expectedHexStringBytes, > actualEncodedBytes)); > > + // test 2 > > + String actualStringFromBytes =3D new String(actualEncodedBytes= , > name); > > + assertEquals(name + ", expectedHexString=3D" + expectedHexStri= ng > + ", actualStringFromBytes=3D" + actualStringFromBytes, > > + expectedHexString, actualStringFromBytes); > > + // second test: > > + Hex utf8Codec =3D new Hex(); > > + expectedHexString =3D "48656c6c6f20576f726c64"; > > + byte[] decodedUtf8Bytes =3D (byte[]) > utf8Codec.decode(expectedHexString); > > + actualStringFromBytes =3D new String(decodedUtf8Bytes, > utf8Codec.getCharsetName()); > > + // sanity check: > > + assertEquals(name, sourceString, actualStringFromBytes); > > + // actual check: > > + byte[] decodedCustomBytes =3D > customCodec.decode(actualEncodedBytes); > > + actualStringFromBytes =3D new String(decodedCustomBytes, name)= ; > > + assertEquals(name, sourceString, actualStringFromBytes); > > + } > > + > > + private boolean charsetSanityCheck(String name) { > > + final String source =3D "the quick brown dog jumped over the l= azy > fox"; > > + try { > > + byte[] bytes =3D source.getBytes(name); > > + String str =3D new String(bytes, name); > > + return source.equals(str); > > + } catch (UnsupportedEncodingException e) { > > + // Should NEVER happen since we are getting the name from > the Charset class. > > + if (LOG) { > > + e.printStackTrace(); > > + } > > + return false; > > + } catch (UnsupportedOperationException e) { > > + // Happens with: > > + // x-JIS0208 on Windows XP and Java Sun 1.6.0_14 > > + if (LOG) { > > + e.printStackTrace(); > > + } > > + return false; > > } > > - catch (DecoderException e) { > > + } > > + > > + public void testDecodeArrayOddCharacters() { > > + try { > > + new Hex().decode(new byte[]{65}); > > + fail("An exception wasn't thrown when trying to decode an > odd number of characters"); > > + } catch (DecoderException e) { > > // Expected exception > > } > > } > > @@ -52,8 +151,7 @@ > > try { > > new Hex().decode("q0"); > > fail("An exception wasn't thrown when trying to decode an > illegal character"); > > - } > > - catch (DecoderException e) { > > + } catch (DecoderException e) { > > // Expected exception > > } > > } > > @@ -62,38 +160,37 @@ > > try { > > new Hex().decode("0q"); > > fail("An exception wasn't thrown when trying to decode an > illegal character"); > > - } > > - catch (DecoderException e) { > > + } catch (DecoderException e) { > > // Expected exception > > } > > } > > > > public void testDecodeClassCastException() { > > try { > > - new Hex().decode(new int[] { 65 }); > > + new Hex().decode(new int[]{65}); > > fail("An exception wasn't thrown when trying to decode."); > > - } > > - catch (DecoderException e) { > > + } catch (DecoderException e) { > > // Expected exception > > } > > } > > > > - public void testDecodeHexOddCharacters() { > > - try { > > - Hex.decodeHex(new char[] { 'A' }); > > - fail("An exception wasn't thrown when trying to decode an > odd number of characters"); > > - } > > - catch (DecoderException e) { > > - // Expected exception > > - } > > + public void testDecodeHexOddCharacters1() { > > + checkDecodeHexOddCharacters(new char[]{'A'}); > > + } > > + > > + public void testDecodeHexOddCharacters3() { > > + checkDecodeHexOddCharacters(new char[]{'A', 'B', 'C'}); > > + } > > + > > + public void testDecodeHexOddCharacters5() { > > + checkDecodeHexOddCharacters(new char[]{'A', 'B', 'C', 'D', > 'E'}); > > } > > > > public void testDecodeStringOddCharacters() { > > try { > > new Hex().decode("6"); > > fail("An exception wasn't thrown when trying to decode an > odd number of characters"); > > - } > > - catch (DecoderException e) { > > + } catch (DecoderException e) { > > // Expected exception > > } > > } > > @@ -101,15 +198,14 @@ > > public void testDencodeEmpty() throws DecoderException { > > assertTrue(Arrays.equals(new byte[0], Hex.decodeHex(new > char[0]))); > > assertTrue(Arrays.equals(new byte[0], new Hex().decode(new > byte[0]))); > > - assertTrue(Arrays.equals(new byte[0], (byte[])new > Hex().decode(""))); > > + assertTrue(Arrays.equals(new byte[0], (byte[]) new > Hex().decode(""))); > > } > > - > > + > > public void testEncodeClassCastException() { > > try { > > - new Hex().encode(new int[] { 65 }); > > + new Hex().encode(new int[]{65}); > > fail("An exception wasn't thrown when trying to encode."); > > - } > > - catch (EncoderException e) { > > + } catch (EncoderException e) { > > // Expected exception > > } > > } > > @@ -126,7 +222,7 @@ > > char[] encodedChars =3D Hex.encodeHex(data); > > byte[] decodedBytes =3D Hex.decodeHex(encodedChars); > > assertTrue(Arrays.equals(data, decodedBytes)); > > - > > + > > // instance API with array parameter > > byte[] encodedStringBytes =3D hex.encode(data); > > decodedBytes =3D hex.decode(encodedStringBytes); > > @@ -134,34 +230,31 @@ > > > > // instance API with char[] (Object) parameter > > String dataString =3D new String(encodedChars); > > - char[] encodedStringChars =3D (char[])hex.encode(dataStrin= g); > > - decodedBytes =3D (byte[])hex.decode(encodedStringChars); > > - assertTrue(Arrays.equals(dataString.getBytes(), > decodedBytes)); > > + char[] encodedStringChars =3D (char[]) > hex.encode(dataString); > > + decodedBytes =3D (byte[]) hex.decode(encodedStringChars); > > + > assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), > decodedBytes)); > > > > // instance API with String (Object) parameter > > dataString =3D new String(encodedChars); > > - encodedStringChars =3D (char[])hex.encode(dataString); > > - decodedBytes =3D (byte[])hex.decode(new > String(encodedStringChars)); > > - assertTrue(Arrays.equals(dataString.getBytes(), > decodedBytes)); > > + encodedStringChars =3D (char[]) hex.encode(dataString); > > + decodedBytes =3D (byte[]) hex.decode(new > String(encodedStringChars)); > > + > assertTrue(Arrays.equals(StringUtils.getBytesUtf8(dataString), > decodedBytes)); > > } > > } > > > > public void testEncodeEmpty() throws EncoderException { > > assertTrue(Arrays.equals(new char[0], Hex.encodeHex(new > byte[0]))); > > assertTrue(Arrays.equals(new byte[0], new Hex().encode(new > byte[0]))); > > - assertTrue(Arrays.equals(new char[0], (char[])new > Hex().encode(""))); > > + assertTrue(Arrays.equals(new char[0], (char[]) new > Hex().encode(""))); > > } > > > > public void testEncodeZeroes() { > > char[] c =3D Hex.encodeHex(new byte[36]); > > - assertEquals( > > - "000000000000000000000000000000000000" > > - + "000000000000000000000000000000000000", > > - new String(c)); > > + > assertEquals("0000000000000000000000000000000000000000000000000000000000= 0 > 0000000000000", new String(c)); > > } > > > > public void testHelloWorldLowerCaseHex() { > > - byte[] b =3D "Hello World".getBytes(); > > + byte[] b =3D StringUtils.getBytesUtf8("Hello World"); > > final String expected =3D "48656c6c6f20576f726c64"; > > char[] actual; > > actual =3D Hex.encodeHex(b); > > @@ -173,7 +266,7 @@ > > } > > > > public void testHelloWorldUpperCaseHex() { > > - byte[] b =3D "Hello World".getBytes(); > > + byte[] b =3D StringUtils.getBytesUtf8("Hello World"); > > final String expected =3D "48656C6C6F20576F726C64"; > > char[] actual; > > actual =3D Hex.encodeHex(b); > > @@ -183,4 +276,13 @@ > > actual =3D Hex.encodeHex(b, false); > > assertTrue(expected.equals(new String(actual))); > > } > > + > > + public void testRequiredCharset() throws > UnsupportedEncodingException, DecoderException { > > + testCustomCharset("UTF-8", "testRequiredCharset"); > > + testCustomCharset("UTF-16", "testRequiredCharset"); > > + testCustomCharset("UTF-16BE", "testRequiredCharset"); > > + testCustomCharset("UTF-16LE", "testRequiredCharset"); > > + testCustomCharset("US-ASCII", "testRequiredCharset"); > > + testCustomCharset("ISO8859_1", "testRequiredCharset"); > > + } > > } > > > > Modified: > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Strin= g > UtilsTest.java > > URL: > http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/test/org/apac= h > e/commons/codec/binary/StringUtilsTest.java?rev=3D801391&r1=3D801390&r2= =3D801391 > &view=3Ddiff > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =3D=3D=3D=3D > > --- > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Strin= g > UtilsTest.java (original) > > +++ > commons/proper/codec/trunk/src/test/org/apache/commons/codec/binary/Strin= g > UtilsTest.java Wed Aug 5 19:55:54 2009 > > @@ -53,15 +53,21 @@ > > > > public void testGetBytesIso8859_1() throws > UnsupportedEncodingException { > > String charsetName =3D "ISO-8859-1"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesIso8859_1(STRING_FIXTURE)= ; > > Assert.assertTrue(Arrays.equals(expected, actual)); > > } > > > > + private void testGetBytesUnchecked(String charsetName) throws > UnsupportedEncodingException { > > + byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > + byte[] actual =3D StringUtils.getBytesUnchecked(STRING_FIXTURE= , > charsetName); > > + Assert.assertTrue(Arrays.equals(expected, actual)); > > + } > > + > > public void testGetBytesUsAscii() throws > UnsupportedEncodingException { > > String charsetName =3D "US-ASCII"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesUsAscii(STRING_FIXTURE); > > Assert.assertTrue(Arrays.equals(expected, actual)); > > @@ -69,7 +75,7 @@ > > > > public void testGetBytesUtf16() throws UnsupportedEncodingException > { > > String charsetName =3D "UTF-16"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesUtf16(STRING_FIXTURE); > > Assert.assertTrue(Arrays.equals(expected, actual)); > > @@ -77,7 +83,7 @@ > > > > public void testGetBytesUtf16Be() throws > UnsupportedEncodingException { > > String charsetName =3D "UTF-16BE"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesUtf16Be(STRING_FIXTURE); > > Assert.assertTrue(Arrays.equals(expected, actual)); > > @@ -85,7 +91,7 @@ > > > > public void testGetBytesUtf16Le() throws > UnsupportedEncodingException { > > String charsetName =3D "UTF-16LE"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesUtf16Le(STRING_FIXTURE); > > Assert.assertTrue(Arrays.equals(expected, actual)); > > @@ -93,21 +99,15 @@ > > > > public void testGetBytesUtf8() throws UnsupportedEncodingException = { > > String charsetName =3D "UTF-8"; > > - testGetSupportedBytes(charsetName); > > + testGetBytesUnchecked(charsetName); > > byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > byte[] actual =3D StringUtils.getBytesUtf8(STRING_FIXTURE); > > Assert.assertTrue(Arrays.equals(expected, actual)); > > } > > > > - private void testGetSupportedBytes(String charsetName) throws > UnsupportedEncodingException { > > - byte[] expected =3D STRING_FIXTURE.getBytes(charsetName); > > - byte[] actual =3D StringUtils.getSupportedBytes(STRING_FIXTURE= , > charsetName); > > - Assert.assertTrue(Arrays.equals(expected, actual)); > > - } > > - > > - public void testGetSupportedBytesBadEnc() { > > + public void testGetBytesUncheckedBadName() { > > try { > > - StringUtils.getSupportedBytes(STRING_FIXTURE, "UNKNOWN"); > > + StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN"); > > Assert.fail("Expected " + > IllegalStateException.class.getName()); > > } catch (IllegalStateException e) { > > // Expected > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org > For additional commands, e-mail: dev-help@commons.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org