Logo
Distributed Genetic Programming Framework
print print

File org.sfc.xml.ReferenceCountedStreamSource.java

Here you can find all the information about the file org.sfc.xml.ReferenceCountedStreamSource.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-07-25 16:27:33
 * Original Filename: org.sfc.xml.ReferenceCountedStreamSource.java
 * Version          : 1.0.2
 * Last modification: 2006-03-07
 *                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.xml;

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

import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.InputSource;

import org.sfc.io.ReferenceCountedInputStream;
import org.sfc.io.ReferenceCountedReader;
import org.sfc.scoped.AlreadyDisposedException;
import org.sfc.scoped.IReferenceCounted;
import org.sfc.utils.ICloneable;

/**
 * A stream source implementing the <code>IReferenceCounted</code>-
 * interface.
 *
 * @author Thomas Weise
 */

public class ReferenceCountedStreamSource extends     StreamSource
                                          implements  Result,
                                                      IReferenceCounted,
                                                      ICloneable
  {
/**
 * The internal reference counter.
 */

  private   int       m_ref_count ;
/**
 * <code>true</code> as long as this object is alive, <code>false</code> if
 * it died. 
 */

  private   boolean   m_living    ;
  
/**
 * Construct a <code>StreamSource</code>.
 */

  public  ReferenceCountedStreamSource()
    {
    super();
    this.m_living     = true;
    this.m_ref_count  = 1;
    }

/**
 * Construct a StreamSource from another <code>StreamSource</code>.
 *
 * @param p_source The <code>StreamSource</code> to copy.
 */

  public  final void  set_stream_source(final  StreamSource p_source)
    {
    InputStream l_x;
    Reader      l_y;
    
    l_x = p_source.getInputStream();
    l_y = p_source.getReader();
    
    if(p_source instanceof ReferenceCountedStreamSource)
      {
      if(l_x != null)
        {
        ((ReferenceCountedInputStream)l_x).add_ref();
        }
      if(l_y != null)
        {
        ((ReferenceCountedReader)l_y).add_ref();
        }
      }
        
    this.setInputStream(l_x);
    this.setPublicId(p_source.getPublicId());
    this.setReader(l_y);
    this.setSystemId(p_source.getSystemId());
    }
    
/**
 * Construct a StreamSource from an <code>InputSource</code>.
 *
 * @param p_source The <code>InputSource</code> to copy.
 */

  public  final void  set_input_source(final  InputSource p_source)
    {
    InputStream l_x;
    Reader      l_y;
    
    l_x = p_source.getByteStream();
    l_y = p_source.getCharacterStream();
    
    if(p_source instanceof ReferenceCountedInputSource)
      {
      if(l_x != null)
        {
        ((ReferenceCountedInputStream)l_x).add_ref();
        }
      if(l_y != null)
        {
        ((ReferenceCountedReader)l_y).add_ref();
        }
      }
    
    this.setInputStream(l_x);
    this.setReader(l_y);
    this.setPublicId(p_source.getPublicId());
    this.setSystemId(p_source.getSystemId());
    }


/**
 * Increase the reference counter of this object by one.
 * You must call <code>add_ref</code> before you assign a variable to point
 * on this object (except when calling the constructor and storing the
 * result to exactly one variable).
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  add_ref () throws AlreadyDisposedException
    {
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_ref_count++;
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    }
  
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  release () throws AlreadyDisposedException
    {
    this.do_release();
    }
  
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws AlreadyDisposedException If the object has already been disposed.
 * @return  The <code>IOException</code> that was thrown during closing of
 *          the object, or <code>null</code> if everything was ok. 
 */

  final IOException do_release () throws AlreadyDisposedException
    {
    InputStream l_is;
    Reader      l_r;
    IOException l_ioe;
    
    synchronized(this)
      {
      if(this.m_living)
        {        
        if( (--this.m_ref_count) <= 0)
          {
          this.m_living = false;
          l_ioe = null;    
          l_is  = getInputStream();
          if(l_is != null)
            {
            try
              {
              l_is.close();        
              }
            catch(IOException l_ioe2)
              {
              l_ioe = l_ioe2;
              }
            finally
              {
              super.setInputStream(null);
              }      
            }
          
          l_r = getReader();
          if(l_r != null)
            {
            try
              {
              l_r.close();        
              }
            catch(IOException l_ioe2)
              {
              l_ioe = l_ioe2;
              }
            finally
              {
              super.setReader(null);
              }
            }
          
          return l_ioe;
          }
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    
    return null;
    }
  
  
  
/**
 * Returns a new instance of the io object.
 * @return  Another port to the io object.
 */

    @Override
  public  final Object  clone ()
    {
    this.add_ref();
    return this;
    }

/**
 * Cleanup this input source.
 * @throws  Throwable From the super implementation.
 */

    @Override
  protected final void  finalize  ()  throws Throwable
    {
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_ref_count = 1;
        this.do_release();
        this.m_living    = false;
        }
      
      this.m_ref_count = 0;
      }
    super.finalize();
    }
  }

File Information:

file name:ReferenceCountedStreamSource.java
package:org.sfc.xml
qualified name:org.sfc.xml.ReferenceCountedStreamSource.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/xml/ReferenceCountedStreamSource.java
size:7.310 KB (7486 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-03-07 04:20:28 GMT+0000
last access: 2018-04-24 03:10:40 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-24 03:10:40 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo