/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.util;

import java.nio.ByteBuffer;

public class ArrayUtil {
	/**
	 * verify an array copy before copying
	 * 
	 * @param source
	 * @param srcOffset
	 * @param dest
	 * @param destOffset
	 * @param count
	 * @return true if array can be copied
	 */
	public static boolean blockCopy(byte[] source, int srcOffset, byte[] dest, int destOffset, int count) {
        if ((source == null) || (dest == null))
        	return false;
        
        if ((srcOffset < 0) || (destOffset < 0) || (count <= 0)) 
        	return false;
        
        if ((source.length - srcOffset) < count) 
        	return false;
        
        if ((dest.length - destOffset) < count) 
        	return false;

        System.arraycopy(source, srcOffset, dest, destOffset, count);
        
        return true;
	}

	/**
	 * verify and assist a byte buffer to byte array copy 
	 * 
	 * @param source
	 * @param srcOffset
	 * @param dest
	 * @param destOffset
	 * @param count
	 * @return true if copy allowed
	 */
	public static boolean blockCopy(ByteBuffer source, int srcOffset, byte[] dest, int destOffset, int count) {
        if ((source == null) || (dest == null))
        	return false;
        
        if ((srcOffset < 0) || (destOffset < 0) || (count <= 0)) 
        	return false;
        
        if ((source.limit() - srcOffset) < count) 
        	return false;
        
        if ((dest.length - destOffset) < count) 
        	return false;

        source.position(srcOffset);
        source.get(dest,destOffset, count);

        return true;
	}

	/**
	 * verify and assist a byte array to byte buffer copy 
	 * 
	 * @param source
	 * @param srcOffset
	 * @param dest
	 * @param destOffset
	 * @param count
	 * @return true if copy allowed
	 */
	public static boolean blockCopy(byte[] source, int srcOffset, ByteBuffer dest, int destOffset, int count) {
        if ((source == null) || (dest == null))
        	return false;
        
        if ((srcOffset < 0) || (destOffset < 0) || (count <= 0)) 
        	return false;
        
        if ((source.length - srcOffset) < count) 
        	return false;
        
        if ((dest.limit() - destOffset) < count) 
        	return false;

        dest.position(destOffset);
        dest.put(source, srcOffset, count);
        
        return true;
	}
	
	/**
	 * verify and assist a byte buffer to byte buffer copy 
	 * 
	 * @param source
	 * @param srcOffset
	 * @param dest
	 * @param destOffset
	 * @param count
	 * @return true if copy allowed
	 */
	public static boolean blockCopy(ByteBuffer source, int srcOffset, ByteBuffer dest, int destOffset, int count) {
        if ((source == null) || (dest == null))
        	return false;
        
        if ((srcOffset < 0) || (destOffset < 0) || (count <= 0)) 
        	return false;
        
        if ((source.limit() - srcOffset) < count) 
        	return false;
        
        if ((dest.limit() - destOffset) < count) 
        	return false;
        
        int sl = source.limit();
        source.limit(srcOffset + count);
        dest.position(destOffset);
        dest.put(source);
        source.limit(sl);
        
        return true;
	}
}
