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

org.refcodes.io.ByteArrayReceiverImpl Maven / Gradle / Ivy

Go to download

Artifact with commonly used I/O functionality and for connection related issues such as receiving or transmitting data in a unified way.

There is a newer version: 3.3.8
Show newest version
package org.refcodes.io;

import java.util.List;

import org.refcodes.component.OpenException;

/**
 * The Class ByteArrayReceiverImpl.
 */
public class ByteArrayReceiverImpl extends AbstractByteReceiver implements ByteArrayReceiver {

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

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

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

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

	/**
	 * Instantiates a new byte array receiver impl.
	 *
	 * @param aDatagrams the datagrams
	 */
	public ByteArrayReceiverImpl( byte[] aDatagrams ) {
		super( aDatagrams.length );
		try {
			open();
			pushDatagrams( aDatagrams );
		}
		catch ( OpenException ignore ) {}
	}

	/**
	 * Instantiates a new byte array receiver impl.
	 *
	 * @param aDatagrams the datagrams
	 */
	public ByteArrayReceiverImpl( List aDatagrams ) {
		this( toPrimitiveType( aDatagrams.toArray( new Byte[aDatagrams.size()] ) ) );
	}

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

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

	/**
	 * {@inheritDoc}
	 */
	@Override
	public byte[] getBytes() {
		return toPrimitiveType( _datagramQueue.toArray( new Byte[_datagramQueue.size()] ) );
	}

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

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

	private static byte[] toPrimitiveType( Byte[] aBytes ) {
		if ( aBytes == null ) return null;
		byte[] thePrimitives = new byte[aBytes.length];
		for ( int i = 0; i < aBytes.length; i++ ) {
			thePrimitives[i] = aBytes[i].byteValue();
		}
		return thePrimitives;
	}

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy