Hi,

I developed a pretty comprehensive class for this sort of IO stream
manipulation:

http://jakarta.apache.org/avalon/excalibur/api/org/apache/avalon/excalibur/io/IOUtil.html

Offered to Commons as a StreamUtils replacement once, but got no reply.
So I'll offer again.


--Jeff

On Thu, Dec 06, 2001 at 04:08:36PM -0800, Michael Bayne wrote:
> I've been using commons/util for some projects and inevitably end up
> extending the useful utility classes provided therein. I've been putting
> these in my own utility library, but I'd like to contribute back to
> commons/util where appropriate.
> 
> What would be the procedure for doing this?
> 
> Here's a patch for starters:
> 
> Index: src/java/org/apache/commons/util/StreamUtils.java
> ===================================================================
> RCS file: 
>/home/cvspublic/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StreamUtils.java,v
> retrieving revision 1.2
> diff -u -p -r1.2 StreamUtils.java
> --- src/java/org/apache/commons/util/StreamUtils.java 2001/08/15 22:57:03     1.2
> +++ src/java/org/apache/commons/util/StreamUtils.java 2001/12/06 23:48:58
> @@ -104,6 +104,40 @@ public class StreamUtils
>                                          String encoding)
>          throws IOException
>      {
> +        ByteArrayOutputStream contents = readStream(toRead, bufferSize);
> +        return (encoding == null ? contents.toString() :
> +                contents.toString(encoding));
> +    }
> +
> +    /**
> +     * Reads from a stream until EOF, and returns the bytes read.
> +     *
> +     * @param toRead     Stream to use as source.
> +     * @param bufferSize Size of buffer to use when reading from source.
> +     * @return The contents of <code>toRead</code>.
> +     */
> +    public static byte[] streamAsBytes(InputStream toRead, int bufferSize,
> +                                       String encoding)
> +        throws IOException
> +    {
> +        ByteArrayOutputStream contents = readStream(toRead, bufferSize);
> +        return contents.toByteArray();
> +    }
> +
> +    /**
> +     * Reads from a stream util EOF, placing the resulting data into a
> +     * <code>ByteArrayOutputStream</code> which is subsequently returned.
> +     *
> +     * @param toRead     Stream to use as source.
> +     * @param bufferSize Size of buffer to use when reading from source.
> +     *
> +     * @return a <code>ByteArrayOutputStream</code> containing the
> +     * contents of <code>toRead</code>.
> +     */
> +    protected static ByteArrayOutputStream readStream(InputStream toRead,
> +                                                      int bufferSize)
> +        throws IOException
> +    {
>          ByteArrayOutputStream contents = new ByteArrayOutputStream();
>          byte[] buffer = new byte[bufferSize];
>          int bytesRead;
> @@ -113,8 +147,7 @@ public class StreamUtils
>              contents.write(buffer, 0, bytesRead);
>          }
> 
> -        return (encoding == null ? contents.toString() :
> -                contents.toString(encoding));
> +        return contents;
>      }
> 
>      /**
> 
> Thanks,
> 
> -- mdb /o)\ Well, I'll be a greased Jesus!
>        \(o/
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to