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

org.rcsb.mmtf.encoder.ArrayConverters Maven / Gradle / Ivy

There is a newer version: 0.1.1
Show newest version
package org.rcsb.mmtf.encoder;


import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.rcsb.mmtf.utils.CodecUtils;

/**
 * A class of array converters.
 * e.g. converting integer arrays to byte arrays.
 * @author Anthony Bradley
 *
 */
public class ArrayConverters {


	/**
	 * Convert an integer array to byte array, where each integer is encoded by a 
	 * single byte.
	 * @param intArray the input array of integers
	 * @return the byte array of the integers
	 */
	public static byte[] convertIntegersToBytes(int[] intArray) {
		
		ByteBuffer bb = ByteBuffer.allocate(intArray.length);
		
		for(int i=0; i < intArray.length; ++i)
		{
			bb.put((byte)intArray[i]);
		}

		return bb.array();
	}
	
	/**
	 * Convert an integer array to byte array, where each integer is encoded by a
	 * two bytes.
	 * @param intArray the input array of integers
	 * @return the byte array of the integers
	 */
	public static byte[] convertIntegersToTwoBytes(int[] intArray) {

		ByteBuffer bb = ByteBuffer.allocate(intArray.length * 2);
		
		for(int i=0; i < intArray.length; ++i)
		{
			bb.putShort((short)intArray[i]);
		}

		return bb.array();
	}

	/**
	 * Convert an integer array to byte array, where each integer is encoded by a
	 * four bytes.
	 * @param intArray the input array of integers
	 * @return the byte array of the integers
	 */
	public static byte[] convertIntegersToFourByte(int[] intArray) {
		
		ByteBuffer bb = ByteBuffer.allocate(intArray.length * 4);
		
		for(int i=0; i < intArray.length; ++i)
		{
			bb.putInt(intArray[i]);
		}

		return bb.array();
	}
	
	/**
	 * Convert an integer array to a float array by multiplying by a float.
	 * @param floatArray the input float array to be converted to ints
	 * @param floatMultiplier the float divider to multiply the floats by.
	 * @return an int array converted from the input.
	 */
	public static int[] convertFloatsToInts(float[] floatArray, float floatMultiplier) {
		// Assign the output array to write
		int[] outArray = new int[floatArray.length];
		for (int i=0; i splitIntegers(int[] inputArray) {
		// set the two output arrays
		List fourByteInts = new ArrayList<>();
		List twoByteInts = new ArrayList<>();
		// First element goes in the four byte integer array.
		fourByteInts.add(inputArray[0]);
		// Set the counter
		int counter =0;
		for(int i=1;iShort.MAX_VALUE || inputArray[i] < Short.MIN_VALUE){
				// Add the counter
				fourByteInts.add(counter);
				// Add the new four byte integer
				fourByteInts.add(inputArray[i]);				
				// Counter set to zero
				counter = 0;
			}
			else{
				// Little number added to little list
				twoByteInts.add(inputArray[i]);
				// Add to the counter
				counter++;
			}
		}
		// Finally add the counter to the big list 
		fourByteInts.add(counter);
		// Now add these to a list - big first
		List outputList = new ArrayList<>();
		outputList.add(CodecUtils.convertToIntArray(fourByteInts));
		outputList.add(CodecUtils.convertToIntArray(twoByteInts));
		return outputList;
	}

	/**
	 * Convert a char array to an integer array using the ASCII code for characters
	 * @param charArray the input character array
	 * @return an integer array of ASCII decoded chars
	 */
	public static int[] convertCharToIntegers(char[] charArray) {
		int[] outArray = new int[charArray.length];
		for (int i=0; i1){
			byteArr[chainIndex*4+1] = (byte) outChar[1];
		}
		else{
			byteArr[chainIndex*4+1] = (byte) 0;
		}
		if(chainIdLen>2){
			byteArr[chainIndex*4+2] = (byte) outChar[2];
		}				
		else{
			byteArr[chainIndex*4+2] = (byte) 0;
		}
		if(chainIdLen>3){
			byteArr[chainIndex*4+3] = (byte) outChar[3];
		}				
		else{
			byteArr[chainIndex*4+3] =  (byte) 0;
		}		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy