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

org.refcodes.codec.BaseEncoderImpl 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.ByteConsumer;
import org.refcodes.io.ByteReceiver;
import org.refcodes.io.ByteSender;
import org.refcodes.io.ByteSenderDecorator;

/**
 * Vanilla plain implementation of the {@link BaseEncoder} interface to be used
 * with {@link ByteSender} instances.
 */
public class BaseEncoderImpl extends AbstractConnectableAutomaton implements BaseEncoder {

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

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

	private static final int BYTE_MASK = 0xFF;
	private static final int BYTES_PER_INT = 4;
	private static final int BITS_PER_BYTE = 8;
	private static final int BITS_PER_INT = BITS_PER_BYTE * BYTES_PER_INT;

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

	private ByteSender _byteSender;
	private BaseMetrics _baseCodecMetrics = BaseConfig.BASE64;
	private byte[] _buffer = new byte[_baseCodecMetrics.getBytesPerInt()];
	private int _index = 0;

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

	/**
	 * Constructs the {@link BaseEncoder} instance using the provided
	 * {@link ByteConsumer} to receive the encoded data.
	 * 
	 * @param aByteConsumer The {@link ByteConsumer} to be fed with the encoded
	 *        data.
	 */
	public BaseEncoderImpl( ByteConsumer aByteConsumer ) {

		try {
			open( new ByteSenderDecorator( aByteConsumer ) );
		}
		catch ( OpenException ignore ) {}
	}

	/**
	 * Constructs the {@link BaseEncoder} instance using the provided
	 * {@link ByteSender} to receive the encoded data.
	 * 
	 * @param aByteSender The {@link ByteSender} to be fed with the encoded
	 *        data.
	 * 
	 * @throws OpenException in case opening or accessing an open line
	 *         (connection, junction, link) caused problems.
	 */
	public BaseEncoderImpl( ByteSender aByteSender ) throws OpenException {
		open( aByteSender );
	}

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

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

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

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

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void writeDatagram( byte aDatagram ) throws OpenException {
		_buffer[_index % _baseCodecMetrics.getBytesPerInt()] = aDatagram;
		_index++;
		if ( _index % _baseCodecMetrics.getBytesPerInt() == 0 ) {
			toEncodedText( 0 );
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void writeDatagrams( byte[] aDatagram ) throws OpenException {
		for ( byte eData : aDatagram ) {
			writeDatagram( eData );
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void writeDatagrams( byte[] aDatagram, int aOffset, int aLength ) throws OpenException {
		for ( int i = aOffset; i < aOffset + aLength; i++ ) {
			writeDatagram( aDatagram[i] );
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void flush() throws OpenException {
		_byteSender.flush();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public synchronized void close() throws CloseException {
		int theMod = _index % _baseCodecMetrics.getBytesPerInt();
		int theTrailingBytes = (theMod == 0) ? 0 : _baseCodecMetrics.getBytesPerInt() - theMod;
		try {
			if ( theTrailingBytes != 0 ) {
				while ( _index % _baseCodecMetrics.getBytesPerInt() != 0 ) {
					_buffer[_index % _baseCodecMetrics.getBytesPerInt()] = 0;
					_index++;
				}
				toEncodedText( theTrailingBytes );
				for ( int i = 0; i < theTrailingBytes; i++ ) {
					_byteSender.writeDatagram( (byte) (_baseCodecMetrics.getPaddingChar() & 0xFF) );
				}
			}
		}
		catch ( IOException e ) {
			throw new CloseException( "Unable to finish off encoding.", e );
		}

		try {
			_byteSender.flush();
			_byteSender.close();
		}
		catch ( IOException e ) {
			throw new CloseException( "Unable to close the output stream <" + _byteSender + ">.", e );
		}
		super.close();
	}

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

	/**
	 * Open.
	 *
	 * @param aConnection the connection
	 * @throws OpenException the open exception
	 */
	protected void open( ByteConsumer aConnection ) throws OpenException {
		if ( aConnection instanceof ByteReceiver ) {
			_byteSender = (ByteSender) aConnection;

		}
		else {
			_byteSender = new ByteSenderDecorator( aConnection );
		}
		if ( !_byteSender.isOpened() ) {
			if ( _byteSender instanceof Openable ) {
				((Openable) _byteSender).open();
			}
			else {
				throw new OpenException( "The provided connection is in status <" + _byteSender.getConnectionStatus() + "> but does not provide the <" + Openable.class.getName() + "> interface." );
			}
		}
		open();
	}

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

	/**
	 * To encoded text.
	 *
	 * @param aTrailingBytes the trailing bytes
	 * @throws OpenException the open exception
	 */
	private void toEncodedText( int aTrailingBytes ) throws OpenException {
		int theWord = toWord( _buffer, 0, _baseCodecMetrics );
		theWord <<= (BITS_PER_BYTE * (BYTES_PER_INT - _baseCodecMetrics.getBytesPerInt()));
		for ( int i = 0; i < _baseCodecMetrics.getDigitsPerInt() - aTrailingBytes; i++ ) {
			int eByte = (theWord >> (BITS_PER_INT - _baseCodecMetrics.getBitsPerDigit())) & _baseCodecMetrics.getDigitMask();
			try {
				_byteSender.writeDatagram( (byte) (_baseCodecMetrics.toChar( eByte ) & 0xFF) );
			}
			catch ( IOException e ) {
				throw new OpenException( "Unable to write to the provided output stream <" + _byteSender + ">: " + ExceptionUtility.toMessage( e ), e );
			}
			theWord <<= _baseCodecMetrics.getBitsPerDigit();
		}
	}

	/**
	 * To word.
	 *
	 * @param aDecodedData the decoded data
	 * @param aOffset the offset
	 * @param aBaseMetrics the base metrics
	 * @return the int
	 */
	private static int toWord( byte[] aDecodedData, int aOffset, BaseMetrics aBaseMetrics ) {
		int eWord = 0;
		for ( int i = 0; i < aBaseMetrics.getBytesPerInt(); i++ ) {
			eWord <<= BITS_PER_BYTE;
			if ( aOffset + i < aDecodedData.length ) {
				eWord |= (int) aDecodedData[aOffset + i] & BYTE_MASK;
			}
		}
		return eWord;
	}

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

	/**
	 * Vanilla plain implementation of the {@link BaseEncoderConsumer} interface
	 * to be used with {@link ByteConsumer} ({@link ByteSender}) instances.
	 */
	public static class BaseEncoderConsumerImpl extends BaseEncoderImpl implements BaseEncoderConsumer {

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy