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

import java.util.Stack;

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

public class BytesToInteger extends PostfixMathCommand {
    public BytesToInteger() {
        numberOfParameters = 1;
    }

//  Byte 문자를 Integer 형 Binary(4Bytes)로
    public void run(Stack inStack) throws ParseException {
    	checkStack(inStack);

        Object p1 = inStack.pop();
        
        int i = 0;

        if (p1 instanceof byte[]) {
            i = Integer.parseInt(new String((byte[]) p1));
        } else if (p1 instanceof String) {
            i = Integer.parseInt((String) p1);
        } else if (p1 instanceof Number) {
            i = ((Number) p1).intValue();
        } else {
            throw new ParseException("Invalid parameter type");
        }

        byte[] bytes =  new byte[] {
                (byte)(i >>> 24),
                (byte)(i >>> 16),
                (byte)(i >>> 8),
                (byte) i};

        inStack.push(bytes);
    	
    }
}
