package com.eactive.eai.transformer.function.generic;

import java.util.Stack;

import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.PostfixMathCommand;

public class IntegerToBytes extends PostfixMathCommand {
    public IntegerToBytes() {
        numberOfParameters = 1;
    }
//  Integer 형 Binary(4Bytes) 를 Byte 문자 로
    public void run(Stack inStack) throws ParseException {

    	checkStack(inStack);

        Object p1 = inStack.pop();
        

        if (p1 instanceof byte[]) {
            byte[] bytes = (byte[]) p1;

            if (bytes.length != 4)
            	throw new ParseException("'IntegerToBytes' function argument must be 4 bytes binary");

            int i = (bytes[0] << 24)
	              + ((bytes[1] & 0xFF) << 16)
	              + ((bytes[2] & 0xFF) << 8)
	              + (bytes[3] & 0xFF);
            
            inStack.push(new Integer(i));        
        } else {
            throw new ParseException("Invalid parameter type");
        }        
    }
}
