head	1.4;
access;
symbols;
locks; strict;
comment	@# @;


1.4
date	2001.08.15.22.08.50;	author leonardr;	state dead;
branches;
next	1.3;

1.3
date	2001.08.15.20.41.27;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.08.15.18.46.17;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.08.15.17.34.20;	author leonardr;	state Exp;
branches;
next	;


desc
@@


1.4
log
@Cleanup and rename of StreamUtils, and rewrite of streamToString to handle multi-byte characters properly.
@
text
@/* ================================================================
 * Copyright (c) 2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.StringBuffer;

/**
 * Methods for manipulating streams.
 *
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@version $Id: StreamTools.java,v 1.3 2001/08/15 20:41:27 dlr Exp $
 */
public class StreamTools
{
    /**
     * Buffer size to use if one is not specified.
     */
    private static final int DEFAULT_BUFFER_SIZE = 1024;

    /**
     * Reads from a stream until EOF and returns everything read as a
     * string.  
     */
    public static String streamAsString(InputStream toRead)
        throws IOException
    {    
        return streamAsString(toRead, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Reads from a stream until EOF and returns everything read as a
     * string.  
     */
    public static String streamAsString(InputStream toRead, int bufferSize)
        throws IOException
    {    
        StringBuffer contents = new StringBuffer();
        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        
        while ( (bytesRead = toRead.read(buffer)) != -1 )
        {            
            //FIXME: Assumes default encoding.
            contents.append(new String(trimBuffer(buffer, bytesRead)));
        }
        return contents.toString();
    }

    /**
     * Pipes the contents of an InputStream into an OutputStream.
     * Note that you still need to close the OutputStream.
     */
    public static void pipe(InputStream toRead, OutputStream toWrite)
        throws IOException
    {    
        pipe(toRead, toWrite, DEFAULT_BUFFER_SIZE);
    }

    /**
     * Pipes the contents of an InputStream into an OutputStream.
     * Note that you still need to close the OutputStream.
     */    
    public static void pipe(InputStream toRead, OutputStream toWrite,
                            int bufferSize)
        throws IOException
    {    
        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        
        while ( (bytesRead = toRead.read(buffer)) != -1 )
        {
            toWrite.write(trimBuffer(buffer, bytesRead));
        }
    }

    /**
     * If the buffer was not filled up by a read, this method will
     * swap it for a new buffer of the appropriate size.
     *
     * @@param buffer The buffer to trim.
     * @@param size   The size to trim the buffer to.
     * @@return       A trimmed version of the buffer.
     */
    private static byte[] trimBuffer(byte[] buffer, int size)
    {
        byte[] newBuffer = buffer;
        if (size < buffer.length)
        {
            newBuffer = new byte[size];
            System.arraycopy(buffer, 0, newBuffer, 0, size);
        }
        return newBuffer;
    }
}
@


1.3
log
@Removed * import in favor of being more explicit, and added whitespace
to constant definition.
@
text
@d59 1
a59 1
 * @@version $Id: StreamTools.java,v 1.2 2001/08/15 18:46:17 dlr Exp $
@


1.2
log
@Renamed param and added JavaDoc to trimBuffer() class method.
@
text
@d49 3
a51 1
import java.io.*;
d59 1
a59 1
 * @@version $Id: StreamTools.java,v 1.1 2001/08/15 17:34:20 leonardr Exp $
d66 1
a66 1
    private static int DEFAULT_BUFFER_SIZE=1024;
@


1.1
log
@Factored out stream manipulation methods into a utility class.
@
text
@d57 1
a57 1
 * @@version $Id: URL.java,v 1.3 2001/08/11 09:08:10 dlr Exp $
a60 1

d125 4
d130 1
a130 1
    private static byte[] trimBuffer(byte[] buffer, int bytesRead)
d133 1
a133 1
        if (bytesRead < buffer.length)
d135 2
a136 2
            newBuffer = new byte[bytesRead];
            System.arraycopy(buffer, 0, newBuffer, 0, bytesRead);
@

