All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.refcodes.codec.BaseDecoderImpl Maven / Gradle / Ivy

Go to download

Artifact with encoding and decoding (not in terms of encryption/decryption) implementations (codecs) such as BASE64 encoding / decoding.

There is a newer version: 3.3.8
Show newest version
// /////////////////////////////////////////////////////////////////////////////
// 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")
// -----------------------------------------------------------------------------
// 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.codec;

import java.io.IOException;

import org.refcodes.component.AbstractConnectableAutomaton;
import org.refcodes.component.CloseException;
import org.refcodes.component.OpenException;
import org.refcodes.component.Openable;
import org.refcodes.exception.ExceptionUtility;
import org.refcodes.io.ByteProvider;
import org.refcodes.io.ByteReceiver;
import org.refcodes.io.ByteReceiverDecorator;

/**
 * Vanilla plain implementation of the {@link BaseDecoder} interface to be used
 * with {@link ByteReceiver} instances.
 */
public class BaseDecoderImpl extends AbstractConnectableAutomaton implements BaseDecoder {

	// /////////////////////////////////////////////////////////////////////////
	// STATICS:
	// /////////////////////////////////////////////////////////////////////////

	private static final int BYTE_MASK = 0xFF;
	private static final int BITS_PER_BYTE = 8;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTANTS:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	private ByteReceiver _byteReceiver;
	private BaseMetrics _baseCodecMetrics = BaseConfig.BASE64;
	private int _trailingBytes = 0;
	int _word = 0;
	private byte[] _decodedBytes = new byte[_baseCodecMetrics.getBytesPerInt()];
	private int _readIndex = 0;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Constructs the {@link BaseDecoder} reading the data to be decoded from
	 * the provided {@link ByteProvider}.
	 * 
	 * @param aByteProvider The {@link ByteProvider} from which to read the
	 *        data.
	 */
	public BaseDecoderImpl( ByteProvider aByteProvider ) {
		try {
			open( new ByteReceiverDecorator( aByteProvider ) );
		}
		catch ( OpenException ignore ) {}
	}

	/**
	 * Constructs the {@link BaseDecoder} reading the data to be decoded from
	 * the provided {@link ByteReceiver}.
	 * 
	 * @param aByteReceiver The {@link ByteReceiver} from which to read the
	 *        data.
	 * 
	 * @throws OpenException in case opening or accessing an open line
	 *         (connection, junction, link) caused problems.
	 */
	public BaseDecoderImpl( ByteReceiver aByteReceiver ) throws OpenException {
		open( aByteReceiver );
	}

	/**
	 * Explicit default constructor.
	 */
	protected BaseDecoderImpl() {}

	// /////////////////////////////////////////////////////////////////////////
	// INJECTION:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// METHODS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BaseMetrics getBaseMetrics() {
		return _baseCodecMetrics;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setBaseMetrics( BaseMetrics aBaseMetrics ) {
		_baseCodecMetrics = aBaseMetrics;
		_decodedBytes = new byte[_baseCodecMetrics.getBytesPerInt()];
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public BaseDecoder withBaseMetrics( BaseMetrics aBaseMetrics ) {
		setBaseMetrics( aBaseMetrics );
		return this;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean hasDatagram() throws OpenException {
		return _byteReceiver.hasDatagram();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public byte[] readDatagrams() throws OpenException, InterruptedException {

		if ( _readIndex > 0 ) {
			// TODO: Write test code for BaseDecodeInputStreamReceiverTest!
			byte[] theDecodedBytes = new byte[_decodedBytes.length - _readIndex];
			int l = 0;
			for ( int i = _readIndex; i < _decodedBytes.length; i++ ) {
				theDecodedBytes[l++] = _decodedBytes[i];
			}
			_readIndex = 0;
			return theDecodedBytes;
		}

		try {
			char eRead;
			for ( int l = 0; l < _baseCodecMetrics.getDigitsPerInt(); l++ ) {
				_word <<= _baseCodecMetrics.getBitsPerDigit();
				eRead = (char) _byteReceiver.readDatagram();
				if ( eRead != _baseCodecMetrics.getPaddingChar() ) {
					_word |= (int) _baseCodecMetrics.toValue( eRead ) & BYTE_MASK;
				}
				else {
					_trailingBytes++;
				}
			}
			for ( int i = 0; i < _baseCodecMetrics.getBytesPerInt(); i++ ) {
				int theIndex = _baseCodecMetrics.getBytesPerInt() - i - 1;
				_decodedBytes[theIndex] = (byte) _word;
				_word >>= BITS_PER_BYTE;
			}
			_word = 0;
		}
		catch ( IOException e ) {
			throw new OpenException( "Unable to read from the provided receiver <" + _byteReceiver + ">: " + ExceptionUtility.toMessage( e ), e );
		}

		if ( _trailingBytes != 0 ) {
			int theLastBlockSize = _baseCodecMetrics.getBytesPerInt() - _trailingBytes;
			byte[] theDecodedBytes = new byte[theLastBlockSize];
			for ( int i = 0; i < theLastBlockSize; i++ ) {
				theDecodedBytes[i] = _decodedBytes[i];
			}
			return theDecodedBytes;
		}

		return _decodedBytes;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public byte readDatagram() throws OpenException, InterruptedException {
		// TODO: Write test code for BaseDecodeInputStreamReceiverTest!
		if ( _readIndex == 0 ) {
			readDatagrams();
		}
		byte theDecodedByte = _decodedBytes[_readIndex++];
		if ( _readIndex >= _decodedBytes.length ) _readIndex = 0;
		return theDecodedByte;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void releaseAll() {
		synchronized ( this ) {
			notifyAll();
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public synchronized void close() throws CloseException {
		try {
			_byteReceiver.close();
		}
		catch ( IOException e ) {
			throw new CloseException( "Unable to close the receiver <" + _byteReceiver + ">", e );
		}
		super.close();
	}

	// /////////////////////////////////////////////////////////////////////////
	// HOOKS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Open.
	 *
	 * @param aConnection the connection
	 * @throws OpenException the open exception
	 */
	protected void open( ByteProvider aConnection ) throws OpenException {
		if ( aConnection instanceof ByteReceiver ) {
			_byteReceiver = (ByteReceiver) aConnection;
		}
		else {
			_byteReceiver = new ByteReceiverDecorator( aConnection );
		}
		if ( !_byteReceiver.isOpened() ) {
			if ( _byteReceiver instanceof Openable ) {
				((Openable) _byteReceiver).open();
			}
			else {
				throw new OpenException( "The provided connection is in status <" + _byteReceiver.getConnectionStatus() + "> but does not provide the <" + Openable.class.getName() + "> interface." );
			}
		}
		open();
	}

	// /////////////////////////////////////////////////////////////////////////
	// HELPER:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// INNER CLASSES:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Vanilla plain implementation of the {@link BaseDecoderProvider} interface
	 * to be used with {@link ByteProvider} ({@link ByteReceiver}) instances.
	 */
	public static class BaseDecoderProviderImpl extends BaseDecoderImpl implements BaseDecoderProvider {

		// /////////////////////////////////////////////////////////////////////
		// METHODS:
		// /////////////////////////////////////////////////////////////////////

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void open( ByteProvider aConnection ) throws OpenException {
			super.open( aConnection );
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy