org.refcodes.io.AbstractInputStreamReceiver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refcodes-io Show documentation
Show all versions of refcodes-io Show documentation
Artifact with commonly used I/O functionality and for connection related
issues such as receiving or transmitting data in a unified way.
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// =============================================================================
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// =============================================================================
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// together with the GPL linking exception applied; as being applied by the GNU
// Classpath ("http://www.gnu.org/software/classpath/license.html")
// =============================================================================
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// =============================================================================
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.io;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.LinkedList;
import org.refcodes.component.AbstractConnectableAutomaton;
import org.refcodes.component.CloseException;
import org.refcodes.component.ConnectionComponent.ConnectionAutomaton;
import org.refcodes.component.ConnectionOpenable;
import org.refcodes.component.ConnectionStatus;
import org.refcodes.component.OpenException;
import org.refcodes.exception.ExceptionUtility;
/**
* Abstract implementation of the {@link Receiver} interface. The
* {@link #open(InputStream)}, {@link #setConnectionStatus(ConnectionStatus)}
* and {@link #isOpenable()} methods are your hooks when extending this class.
*
* @param The type of the datagram to be operated with.
*/
public abstract class AbstractInputStreamReceiver extends AbstractConnectableAutomaton implements Receiver {
// /////////////////////////////////////////////////////////////////////////
// CONSTANTS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private LinkedList _datagramQueue = new LinkedList();
private ObjectInputStream _inputStream = null;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// METHODS:
// /////////////////////////////////////////////////////////////////////////
/**
* Checks for datagram.
*
* @return true, if successful
* @throws OpenException the open exception
*/
@SuppressWarnings("unchecked")
@Override
public boolean hasDatagram() throws OpenException {
if ( !_datagramQueue.isEmpty() ) return true;
if ( isClosed() ) return false;
try {
Object theObject = _inputStream.readObject();
if ( theObject == null ) {
if ( isClosed() ) return false;
close();
synchronized ( _datagramQueue ) {
_datagramQueue.notifyAll();
}
return false;
}
_datagramQueue.add( (DATA) theObject );
return true;
}
catch ( CloseException e ) {
synchronized ( _datagramQueue ) {
_datagramQueue.notifyAll();
}
throw new OpenException( "Unable to test datagram availability, connection status is <" + getConnectionStatus() + ">.", e );
}
catch ( IOException ioe ) {
if ( isClosed() ) return false;
synchronized ( _datagramQueue ) {
_datagramQueue.notifyAll();
}
try {
if ( ExceptionUtility.isThrownAsOfAlreadyClosed( ioe ) ) {
super.close();
}
else {
close();
}
}
catch ( CloseException e ) {
throw new OpenException( "Unable to test datagram availability, connection status is <" + getConnectionStatus() + ">.", e );
}
throw new OpenException( "Unable to test datagram availability, connection status is <" + getConnectionStatus() + ">.", ioe );
}
catch ( ClassNotFoundException e ) {
throw new OpenException( "Datagram read is of unknown type, connection status is <" + getConnectionStatus() + ">.", e );
}
}
/**
* {@inheritDoc}
*/
@Override
public DATA readDatagram() throws OpenException, InterruptedException {
if ( _datagramQueue.isEmpty() && !isOpened() ) {
throw new OpenException( "Unable to read datagram as the connection is NOT OPEN; connection status is <" + getConnectionStatus() + ">." );
}
if ( !hasDatagram() ) {
throw new OpenException( "Unable to read datagram as the connection is NOT OPEN; connection status is <" + getConnectionStatus() + ">." );
}
DATA theDatagram = _datagramQueue.poll();
while ( theDatagram == null ) {
if ( !hasDatagram() ) {
throw new OpenException( "Unable to read datagram as the connection is NOT OPEN; connection status is <" + getConnectionStatus() + ">." );
}
}
return theDatagram;
}
/**
* {@inheritDoc}
*/
@Override
public void releaseAll() {
synchronized ( _datagramQueue ) {
_datagramQueue.notifyAll();
}
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void close() throws CloseException {
if ( !isClosed() ) {
super.close();
try {
_inputStream.close();
}
catch ( IOException e ) {
if ( !ExceptionUtility.isThrownAsOfAlreadyClosed( e ) ) throw new CloseException( "Unable to close receiver, connection status is <" + getConnectionStatus() + ">.", e );
}
}
}
// /////////////////////////////////////////////////////////////////////////
// HOOKS:
// /////////////////////////////////////////////////////////////////////////
/**
* Open, see also {@link ConnectionOpenable#open(Object)}.
*
* @param aInputStream the input stream
* @throws OpenException the open exception
*/
protected synchronized void open( InputStream aInputStream ) throws OpenException {
if ( isOpened() ) {
throw new OpenException( "Unable to open the connection is is is ALREADY OPEN; connection status is " + getConnectionStatus() + "." );
}
try {
if ( !(aInputStream instanceof BufferedInputStream) ) {
_inputStream = new SerializableObjectInputStreamImpl( new BufferedInputStream( aInputStream ) );
}
else {
_inputStream = new SerializableObjectInputStreamImpl( aInputStream );
}
}
catch ( IOException aException ) {
throw new OpenException( "Unable to open the I/O stream receiver as of a causing exception.", aException );
}
setConnectionStatus( ConnectionStatus.OPENED );
}
/**
* Checks if is openable. See also
* {@link ConnectionAutomaton#isOpenable(Object)}.
*
* @param aInputStream the input stream
* @return true, if is openable
*/
protected boolean isOpenable( InputStream aInputStream ) {
if ( aInputStream == null ) {
return false;
}
return !isOpened();
}
// /////////////////////////////////////////////////////////////////////////
// HELPER:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// INNER CLASSES:
// /////////////////////////////////////////////////////////////////////////
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy