Logo
Distributed Genetic Programming Framework
print print

File org.sfc.io.StreamLoader.java

Here you can find all the information about the file org.sfc.io.StreamLoader.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-05-23 13:39:04
 * Original Filename: org.sfc.io.StreamLoader.java
 * Version          : 1.0.2
 * Last modification: 2006-02-12
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.sfc.io;

import java.io.IOException;
import java.io.InputStream;

import org.sfc.collections.Arrays;


/**
 * This one is allows you to load streams completely into a byte array.
 * You can also concat multiple streams that way.
 * 
 * @author  Thomas Weise
 */

public final  class StreamLoader
  {
/**
 * The internal data array.
 */

  private byte[]  m_data    ;
/**
 * The internal read buffer.
 */

  private byte[]  m_buffer  ;
/** 
 * The current length of the internal data.
 */

  private int     m_length  ;
  
/**
 * Create a new StreamLoader.
 * @param p_buffer_size The initial read buffer size. You can leave this
 *                      this parameter 0 and a reasonable default will
 *                      be used.
 */

  public  StreamLoader  (int  p_buffer_size)
    {
    super();
    if(p_buffer_size <= 0) p_buffer_size = 4096;
    this.m_data    = new byte[p_buffer_size];
    this.m_buffer  = new byte[p_buffer_size];
    }
  
/**
 * Reset the stream loader.
 * Invalidate the internal data and set the current data length back
 * to 0.
 * You can reuse the loader after a call to reset.
 * The content returned by get_data() is unspecified afterwards.
 * get_length() will return 0.
 */

  public  final void  reset ()
    {
    this.m_length = 0;
    }
  
/**
 * Load an input stream completely into the buffer.
 * @param p_is  The input stream to load.
 * @return  true if everything went ok, false if an exception got
 *          caught.
 */

  public  final boolean  load_stream  (InputStream  p_is)
    {
    int     l_i, l_j;
    byte[]  l_b;
    
    try
      {
      while((l_i = p_is.read(this.m_buffer)) > 0)
        {
        l_j           = this.m_length;
        l_b           = Arrays.insert_space(this.m_data, l_j, l_j, l_i);
        System.arraycopy(this.m_buffer, 0, l_b, l_j, l_i);
        this.m_data   = l_b;
        this.m_length = (l_j + l_i);
        }
      
      return true;
      }    
    catch(IOException l_ioe)
      {      
      return false;
      }
    finally
      {
      try
        {
        p_is.close();
        }
      catch(IOException l_ioe2)
        {        
        return false;
        }
      }
    }
  
  
/**
 * Returns the current length of the loaded data.
 * @return The current length of the loaded data.
 */

  public  final int get_length  ()
    {
    return this.m_length;
    }
  
/**
 * Returns the currently loaded data.
 * @return  A byte array containing get_length() bytes of valid data.
 */

  public  final byte[]  get_data  ()
    {
    return this.m_data;
    }
  
/**
 * A convenient method to load streams identified by an object.
 * @param p_source The source object to be loaded.
 * @return  true if everything went ok, false otherwise.
 */

  public  final boolean load  (Object p_source)
    {
    return load_stream(IO.get_input_stream(p_source));
    }
  }

File Information:

file name:StreamLoader.java
package:org.sfc.io
qualified name:org.sfc.io.StreamLoader.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/io/StreamLoader.java
size:4.005 KB (4102 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-03-07 04:20:28 GMT+0000
last access: 2018-04-26 13:40:53 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2018-01-07 12:03:34 GMT+0000 served at 2018-04-26 13:40:53 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo