Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 62284 invoked from network); 27 Nov 2005 09:50:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 27 Nov 2005 09:50:24 -0000 Received: (qmail 59737 invoked by uid 500); 27 Nov 2005 09:50:21 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 59673 invoked by uid 500); 27 Nov 2005 09:50:20 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 59662 invoked by uid 500); 27 Nov 2005 09:50:20 -0000 Received: (qmail 59659 invoked by uid 99); 27 Nov 2005 09:50:20 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 27 Nov 2005 01:50:20 -0800 Received: (qmail 62130 invoked by uid 65534); 27 Nov 2005 09:49:59 -0000 Message-ID: <20051127094959.62125.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r349195 - in /jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl: ResourceBundleResources.java ResourcesBase.java Date: Sun, 27 Nov 2005 09:49:59 -0000 To: commons-cvs@jakarta.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: niallp Date: Sun Nov 27 01:49:47 2005 New Revision: 349195 URL: http://svn.apache.org/viewcvs?rev=349195&view=rev Log: Fix for Bug 37642 - Improve ResourcesBase method inter-operability Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java?rev=349195&r1=349194&r2=349195&view=diff ============================================================================== --- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java (original) +++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java Sun Nov 27 01:49:47 2005 @@ -194,58 +194,6 @@ /** - *

Return the content for the specified key as a - * byte array, localized based on the specified locale - * and/or timeZone.

- * - * @param key Identifier for the requested content - * @param locale Locale with which to localize retrieval, - * or null for the default Locale - * @param timeZone TimeZone with which to localize retrieval, - * or null for the default TimeZone - * @return content for a specified key. - * - * @exception ResourcesException if an error occurs retrieving or - * returning the requested content - * @exception ResourcesKeyException if the no value for the specified - * key was found, and isReturnNull() returns - * false - */ - public byte[] getBytes(String key, Locale locale, TimeZone timeZone) - throws ResourcesException { - - throw new UnsupportedOperationException(); // FIXME - - } - - - /** - *

Return the content for the specified key as an - * InputStream, localized based on the specified locale - * and/or timeZone.

- * - * @param key Identifier for the requested content - * @param locale Locale with which to localize retrieval, - * or null for the default Locale - * @param timeZone TimeZone with which to localize retrieval, - * or null for the default TimeZone - * @return content for a specified key. - * - * @exception ResourcesException if an error occurs retrieving or - * returning the requested content - * @exception ResourcesKeyException if the no value for the specified - * key was found, and isReturnNull() returns - * false - */ - public InputStream getInputStream(String key, Locale locale, - TimeZone timeZone) { - - throw new UnsupportedOperationException(); // FIXME - - } - - - /** *

Return the content for the specified key as an * Object, localized based on the specified locale * and/or timeZone.

@@ -265,85 +213,25 @@ */ public Object getObject(String key, Locale locale, TimeZone timeZone) { - try { - ResourceBundle bundle = getBundle(locale, timeZone); - return (bundle.getObject(key)); - - } catch (MissingResourceException e) { - if (isReturnNull()) { - return (null); - } else { - throw new ResourcesKeyException(key); - } - } - - } - - - /** - *

Return the content for the specified key as a - * Reader, localized based on the specified locale - * and/or timeZone.

- * - * @param key Identifier for the requested content - * @param locale Locale with which to localize retrieval, - * or null for the default Locale - * @param timeZone TimeZone with which to localize retrieval, - * or null for the default TimeZone - * @return content for a specified key. - * - * @exception ResourcesException if an error occurs retrieving or - * returning the requested content - * @exception ResourcesKeyException if the no value for the specified - * key was found, and isReturnNull() returns - * false - */ - public Reader getReader(String key, Locale locale, TimeZone timeZone) { - - throw new UnsupportedOperationException(); // FIXME - - } - - - /** - *

Return the content for the specified key as a - * String, localized based on the specified locale - * and/or timeZone.

- * - * @param key Identifier for the requested content - * @param locale Locale with which to localize retrieval, - * or null for the default Locale - * @param timeZone TimeZone with which to localize retrieval, - * or null for the default TimeZone - * @return content for a specified key. - * - * @exception ResourcesException if an error occurs retrieving or - * returning the requested content - * @exception ResourcesKeyException if the no value for the specified - * key was found, and isReturnNull() returns - * false - */ - public String getString(String key, Locale locale, TimeZone timeZone) { - if (getLog().isTraceEnabled()) { - getLog().trace("Retrieving message for key '" + key + "' for locale '" + getLog().trace("Retrieving message for key '" + key + "' and locale '" + locale + "'"); } - + try { ResourceBundle bundle = getBundle(locale, timeZone); - String message = bundle.getString(key); + Object object = bundle.getObject(key); if (getLog().isTraceEnabled()) { - getLog().trace("Retrieved message is '" + message + "'"); + getLog().trace("Retrieved object for key '" + key + + "' and locale '" + locale + + "' is '" + object + "'"); } - return (message); - - } catch (ClassCastException e) { - throw new ResourcesException(e); + return object; } catch (MissingResourceException e) { if (getLog().isTraceEnabled()) { - getLog().trace("No message found"); + getLog().trace("No message found for key '" + key + + "' and locale '" + locale + "'"); } if (isReturnNull()) { return (null); @@ -353,7 +241,6 @@ } } - // ------------------------------------------------------ Protected Methods Modified: jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java?rev=349195&r1=349194&r2=349195&view=diff ============================================================================== --- jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java (original) +++ jakarta/commons/proper/resources/trunk/src/java/org/apache/commons/resources/impl/ResourcesBase.java Sun Nov 27 01:49:47 2005 @@ -25,8 +25,10 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.io.IOException; import java.util.Iterator; import java.util.Locale; import java.util.TimeZone; @@ -101,6 +103,10 @@ */ private boolean returnNull = true; + /** + * Buffer size for creating char and byte arrays. + */ + private int bufferSize = 500; // ------------------------------------------------------ Lifecycle Methods @@ -190,6 +196,31 @@ } + /** + *

Return the size of the buffer to use when converting + * InputStream or Reader objects.

+ * + * @return The buffer size. + */ + public int getBufferSize() { + + return bufferSize; + + } + + /** + *

Set the size of the buffer to use when converting + * InputStream or Reader objects.

+ * + * @param bufferSize The buffer size. + */ + public void setBufferSize(int bufferSize) { + + this.bufferSize = bufferSize; + + } + + // ---------------------------------------------- Content Retrieval Methods @@ -216,11 +247,27 @@ */ public byte[] getBytes(String key, Locale locale, TimeZone timeZone) { - String value = getString(key, locale, timeZone); + Object value = getObject(key, locale, timeZone); if (value == null) { return (null); + } else if (value instanceof String) { + return ((String)value).getBytes(); + } else if (value instanceof Reader) { + char[] chars = getChars((Reader)value); + if (chars == null) { + return (byte[])checkThrow(key); + } + return new String(chars).getBytes(); + } else if (value instanceof InputStream) { + byte[] bytes = getBytes((InputStream)value); + if (bytes == null) { + return (byte[])checkThrow(key); + } + return bytes; + } else if (value instanceof byte[]) { + return (byte[])value; } else { - return (value.getBytes()); + return value.toString().getBytes(); } } @@ -249,12 +296,27 @@ */ public InputStream getInputStream(String key, Locale locale, TimeZone timeZone) { - - byte bytes[] = getBytes(key, locale, timeZone); - if (bytes == null) { - bytes = new byte[0]; + Object value = getObject(key, locale, timeZone); + if (value == null) { + return (null); + } else if (value instanceof String) { + byte[] bytes = ((String)value).getBytes(); + return new ByteArrayInputStream(bytes); + } else if (value instanceof Reader) { + char[] chars = getChars((Reader)value); + if (chars == null) { + return (InputStream)checkThrow(key); + } + byte[] bytes = (new String(chars)).getBytes(); + return new ByteArrayInputStream(bytes); + } else if (value instanceof InputStream) { + return (InputStream)value; + } else if (value instanceof byte[]) { + return new ByteArrayInputStream((byte[])value); + } else { + byte[] bytes = value.toString().getBytes(); + return new ByteArrayInputStream(bytes); } - return (new ByteArrayInputStream(bytes)); } @@ -306,11 +368,20 @@ */ public Reader getReader(String key, Locale locale, TimeZone timeZone) { - String value = getString(key, locale, timeZone); + Object value = getObject(key, locale, timeZone); if (value == null) { return (null); + } else if (value instanceof String) { + return new StringReader((String)value); + } else if (value instanceof Reader) { + return (Reader)value; + } else if (value instanceof InputStream) { + return new InputStreamReader((InputStream)value); + } else if (value instanceof byte[]) { + InputStream bais = new ByteArrayInputStream((byte[])value); + return new InputStreamReader(bais); } else { - return (new StringReader(value)); + return new StringReader(value.toString()); } } @@ -344,10 +415,107 @@ return (null); } else if (value instanceof String) { return ((String) value); + } else if (value instanceof Reader) { + char[] chars = getChars((Reader)value); + if (chars == null) { + return (String)checkThrow(key); + } + return new String(chars); + } else if (value instanceof InputStream) { + byte[] bytes = getBytes((InputStream)value); + if (bytes == null) { + return (String)checkThrow(key); + } + return new String(bytes); + } else if (value instanceof byte[]) { + return new String((byte[])value); } else { return (value.toString()); } + } + + + /** + * Convert a Reader to a char array. + */ + private char[] getChars(Reader reader) { + + char[] array = null; + int arrayLth = 0; + try { + while (true) { + char[] buffer = new char[bufferSize]; + int bufferLth = reader.read(buffer); + if (bufferLth < 0) { + break; + } + if (array == null && bufferLth == buffer.length) { + array = buffer; + } else { + char[] newArray = new char[arrayLth + bufferLth]; + if (array != null) { + System.arraycopy(array, 0, newArray, 0, arrayLth); + } + System.arraycopy(buffer, 0, newArray, arrayLth, bufferLth); + array = newArray; + } + arrayLth = array.length; + } + } catch(IOException e) { + throw new ResourcesException("Error reading Reader", e); + } + + return array; + + } + + /** + * Convert an InputStream to a byte array. + */ + private byte[] getBytes(InputStream inputStream) { + + byte[] array = null; + int arrayLth = 0; + try { + while (true) { + byte[] buffer = new byte[bufferSize]; + int bufferLth = inputStream.read(buffer); + if (bufferLth < 0) { + break; + } + if (array == null && bufferLth == buffer.length) { + array = buffer; + } else { + byte[] newArray = new byte[arrayLth + bufferLth]; + if (array != null) { + System.arraycopy(array, 0, newArray, 0, arrayLth); + } + System.arraycopy(buffer, 0, newArray, arrayLth, bufferLth); + array = newArray; + } + arrayLth = array.length; + } + } catch(IOException e) { + throw new ResourcesException("Error reading InputStream", e); + } + return array; + + } + + /** + * Check whether this Resources instance is + * configured to return null or throw a ResourcesKeyException. + * @param key Identifier for the requested content + * @return 'null' if returnNull is 'true' otherwise throws + * a ResourcesKeyException. + */ + private Object checkThrow(String key) { + if (isReturnNull()) { + return (null); + } else { + throw new ResourcesKeyException(key); + } } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org