/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.util;

import java.io.*;

/**
 * A collection of IO-related static methods which are useful in various contexts.<P>
 * Some of the concepts in this class have been borrowed from Apache Jakarta Avalon Excalibur 4.0.
 *
 * @author Eric J. Bowersox &lt;erbo@silcom.com&gt;
 * @version X
 */
public class IOUtil
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  /**
   * The default buffer size to use for copying, if none is specified.
   */
  public static int DEFAULT_BUFSIZE = 4096;

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Closes an input stream cleanly, without throwing an exception.
   *
   * @param stm The stream to be closed.
   * @see java.io.InputStream#close()
   */
  public static void shutdown(InputStream stm)
  {
    try
    { // close the stream
      stm.close();

    } // end try
    catch (IOException e)
    { // throw away the exception
    } // end catch

  } // end shutdown

  /**
   * Closes an output stream cleanly, without throwing an exception.
   *
   * @param stm The stream to be closed.
   * @see java.io.OutputStream#close()
   */
  public static void shutdown(OutputStream stm)
  {
    try
    { // close the stream
      stm.close();

    } // end try
    catch (IOException e)
    { // throw away the exception
    } // end catch

  } // end shutdown

  /**
   * Closes an input reader cleanly, without throwing an exception.
   *
   * @param stm The stream to be closed.
   * @see java.io.Reader#close()
   */
  public static void shutdown(Reader rdr)
  {
    try
    { // close the stream
      rdr.close();

    } // end try
    catch (IOException e)
    { // throw away the exception
    } // end catch

  } // end shutdown

  /**
   * Closes an output reader cleanly, without throwing an exception.
   *
   * @param stm The stream to be closed.
   * @see java.io.Writer#close()
   */
  public static void shutdown(Writer wr)
  {
    try
    { // close the stream
      wr.close();

    } // end try
    catch (IOException e)
    { // throw away the exception
    } // end catch

  } // end shutdown

  /**
   * Copies the contents of the given input stream to the given output stream.
   *
   * @param input The stream to copy binary data from.
   * @param output The stream to copy binary data to.
   * @param bufsize The size of the buffer to allocate for copying.
   * @exception java.io.IOException If an exception occurred while reading or writing data.
   */
  public static void copy(InputStream input, OutputStream output, int bufsize) throws IOException
  {
    byte[] buffer = new byte[bufsize];
    int rd = input.read(buffer);
    while (rd>=0)
    { // simple read-write loop to shove data out the door
      if (rd>0)
	output.write(buffer,0,rd);
      rd = input.read(buffer);

    } // end while

  } // end copy

  /**
   * Copies the contents of the given input stream to the given output stream.  Uses a default
   * buffer size.
   *
   * @param input The stream to copy binary data from.
   * @param output The stream to copy binary data to.
   * @exception java.io.IOException If an exception occurred while reading or writing data.
   * @see #DEFAULT_BUFSIZE
   * @see #copy(java.io.InputStream,java.io.OutputStream,int)
   */
  public static void copy(InputStream input, OutputStream output) throws IOException
  {
    copy(input,output,DEFAULT_BUFSIZE);

  } // end copy

  /**
   * Takes the contents of an input stream and returns it as an array of bytes.
   *
   * @param input The stream to load binary data from.
   * @param bufsize The size of the buffer to allocate for copying.
   * @return A new byte array containing the contents of the specified input stream.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #copy(java.io.InputStream,java.io.OutputStream,int)
   */
  public static byte[] load(InputStream input, int bufsize) throws IOException
  {
    ByteArrayOutputStream stm = new ByteArrayOutputStream();
    try
    { // copy the data to the input stream
      copy(input,stm,bufsize);
      return stm.toByteArray();

    } // end try
    finally
    { // close our byte array stream before we go
      shutdown(stm);

    } // end finally

  } // end load

  /**
   * Takes the contents of an input stream and returns it as an array of bytes.  Uses a default
   * buffer size.
   *
   * @param input The stream to load binary data from.
   * @return A new byte array containing the contents of the specified input stream.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #DEFAULT_BUFSIZE
   * @see #load(java.io.InputStream,int)
   */
  public static byte[] load(InputStream input) throws IOException
  {
    return load(input,DEFAULT_BUFSIZE);

  } // end load

  /**
   * Takes the contents of a binary file and returns it as an array of bytes.
   *
   * @param file The file to load binary data from.
   * @param bufsize The size of the buffer to allocate for copying.
   * @return A new byte array containing the contents of the specified file.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #load(java.io.InputStream,int)
   */
  public static byte[] loadBinary(File file, int bufsize) throws IOException
  {
    FileInputStream stm = new FileInputStream(file);
    try
    { // now just load from the stream
      return load(stm,bufsize);

    } // end try
    finally
    { // close the file before we leave
      shutdown(stm);

    } // end finally

  } // end loadBinary

  /**
   * Takes the contents of a binary file and returns it as an array of bytes.  Uses a default
   * buffer size.
   *
   * @param file The file to load binary data from.
   * @return A new byte array containing the contents of the specified file.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #DEFAULT_BUFSIZE
   * @see #loadBinary(java.io.File,int)
   */
  public static byte[] loadBinary(File file) throws IOException
  {
    return loadBinary(file,DEFAULT_BUFSIZE);

  } // end loadBinary

  /**
   * Copies the contents of the given input reader to the given output writer.
   *
   * @param input The reader to copy character data from.
   * @param output The writer to copy character data to.
   * @param bufsize The size of the buffer to allocate for copying.
   * @exception java.io.IOException If an exception occurred while reading or writing data.
   */
  public static void copy(Reader input, Writer output, int bufsize) throws IOException
  {
    char[] buffer = new char[bufsize];
    int rd = input.read(buffer);
    while (rd>=0)
    { // simple read-write loop to shove data out the door
      if (rd>0)
	output.write(buffer,0,rd);
      rd = input.read(buffer);

    } // end while

  } // end copy

  /**
   * Copies the contents of the given input reader to the given output writer.  Uses a default
   * buffer size.
   *
   * @param input The reader to copy character data from.
   * @param output The writer to copy character data to.
   * @exception java.io.IOException If an exception occurred while reading or writing data.
   * @see #DEFAULT_BUFSIZE
   * @see #copy(java.io.Reader,java.io.Writer,int)
   */
  public static void copy(Reader input, Writer output) throws IOException
  {
    copy(input,output,DEFAULT_BUFSIZE);

  } // end copy

  /**
   * Takes the contents of an input reader and returns it as a <CODE>StringBuffer</CODE>.
   *
   * @param input The reader to copy character data from.
   * @param bufsize The size of the buffer to allocate for copying.
   * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified reader.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #copy(java.io.Reader,java.io.Writer,int)
   */
  public static StringBuffer load(Reader input, int bufsize) throws IOException
  {
    StringWriter wr = new StringWriter();
    try
    { // copy from reader to StringWriter
      copy(input,wr,bufsize);
      return wr.getBuffer();

    } // end try
    finally
    { // make sure and close the StringWriter before we go
      shutdown(wr);

    } // end finally

  } // end load

  /**
   * Takes the contents of an input reader and returns it as a <CODE>StringBuffer</CODE>.  Uses a
   * default buffer size.
   *
   * @param input The reader to copy character data from.
   * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified reader.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #DEFAULT_BUFSIZE
   * @see #load(java.io.Reader,int)
   */
  public static StringBuffer load(Reader input) throws IOException
  {
    return load(input,DEFAULT_BUFSIZE);

  } // end load

  /**
   * Takes the contents of a text file and returns it as a <CODE>StringBuffer</CODE>.
   *
   * @param file The file to copy text from.
   * @param bufsize The size of the buffer to allocate for copying.
   * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified text file.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #load(java.io.Reader,int)
   */
  public static StringBuffer loadText(File file, int bufsize) throws IOException
  {
    FileReader rdr = new FileReader(file);
    try
    { // load from the string reader
      return load(rdr,bufsize);

    } // end try
    finally
    { // make sure and close the reader before we go
      shutdown(rdr);

    } // end finally

  } // end load

  /**
   * Takes the contents of a text file and returns it as a <CODE>StringBuffer</CODE>.
   *
   * @param file The file to copy text from.
   * @return A new <CODE>StringBuffer</CODE> containing the contents of the specified text file.
   * @exception java.io.IOException If an exception occurred while reading data.
   * @see #DEFAULT_BUFSIZE
   * @see #loadText(java.io.File,int)
   */
  public static StringBuffer loadText(File file) throws IOException
  {
    return loadText(file,DEFAULT_BUFSIZE);

  } // end load

} // end class IOUtil
