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

com.hps.integrator.infrastructure.utils.MessageReader Maven / Gradle / Ivy

Go to download

The SecureSubmit Java SDK simplifies processing of credit card transactions using Heartland Payment Systems' Portico Payment Gateway

There is a newer version: v2.5.2
Show newest version
package com.hps.integrator.infrastructure.utils;

import com.hps.integrator.abstractions.IByteConstant;
import com.hps.integrator.infrastructure.emums.ControlCodes;

import java.io.UnsupportedEncodingException;

public class MessageReader {
    byte[] buffer;
    int position = 0;
    long length = 0;

    public long getLength() { return length; }

    public MessageReader(byte[] bytes){
        buffer = bytes;
        length = bytes.length;
    }

    public boolean canRead(){
        return position < length;
    }

    public byte peek() {
        return buffer[position];
    }

    public ControlCodes readCode(){
        return readEnum(ControlCodes.class);
        //return ControlCode.convert(buffer[position++]);
    }

    public  & IByteConstant> T readEnum(Class enumType){
        ReverseByteEnumMap map = new ReverseByteEnumMap(enumType);
        return map.get(buffer[position++]);
    }

    public byte readByte() {
        return buffer[position++];
    }

    public byte[] readBytes(int length){
        byte[] rvalue = new byte[length];

        try {
            for (int i = 0; i < length; i++)
                rvalue[i] = buffer[position++];
        }
        catch(ArrayIndexOutOfBoundsException e) {
            // eat this exception and return what we have
        }

        return rvalue;
    }

    public char readChar(){
        return (char)buffer[position++];
    }

    public String readString(int length){
        String rvalue = "";

        for(int i = 0; i < length; i++)
            rvalue += (char)buffer[position++];

        return rvalue;
    }

    public String readToCode(ControlCodes code) {
        return readToCode(code, true);
    }
    public String readToCode(ControlCodes code, boolean removeCode){
        String rvalue = "";

        try {
            byte value;
            while((value = peek()) != code.getByte()) {
                if(HpsEnumUtils.isDefined(ControlCodes.class, value)) {
                    ControlCodes byteCode = HpsEnumUtils.parse(ControlCodes.class, buffer[position++]);
                    if(byteCode == ControlCodes.ETX)
                        break;
                    else rvalue += byteCode.toString();
                } else rvalue += (char)buffer[position++];
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            removeCode = false;
        }

        // pop the code off
        if(removeCode)
            readByte();

        return rvalue;
    }

    public void purge(){
        buffer = new byte[0];
        length = 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy