// Path: src/main/java/org/unitedid/yhsm/utility/Utils.java
// public static byte[] concatAllArrays(byte[] data1, byte[]... remaining) {
//     int bufLength = data1.length;
// 
//     for (byte[] arr : remaining) {
//         if (arr.length == 0) {
//             return data1;
//         }
// 
//         bufLength += arr.length;
//     }
// 
//     int offset = data1.length;
//     byte[] buffer = Arrays.copyOf(data1, bufLength);
// 
//     for (byte[] arr : remaining) {
//         System.arraycopy(arr, 0, buffer, offset, arr.length);
//         offset += arr.length;
//     }
// 
//     return buffer;
// }


import jssc.SerialPort;
import jssc.SerialPortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static org.unitedid.yhsm.utility.Utils.concatAllArrays;

    public byte[] read(int readNumBytes) {
        byte[] data = new byte[readNumBytes];
        try {
            data = device.readBytes(readNumBytes);
            readBytes += data.length;
        } catch (SerialPortException e) {
            e.printStackTrace();
        }

        return data;
    }

    public int available() {
        try {
            return device.getInputBufferBytesCount();
        } catch (SerialPortException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean drain() {
        byte[] buffer = new byte[0];
        while(available() > 0) {
            byte[] b = read(1);
            if ((char)b[0] == '\r') {
                log.info("Drained: {}", new String(buffer, 0, buffer.length)); //TODO: Do we really need to log this? If not the loop can be simplified.
                buffer = new byte[0];
            } else {
// Path: src/main/java/org/unitedid/yhsm/utility/Utils.java
// public static byte[] concatAllArrays(byte[] data1, byte[]... remaining) {
//     int bufLength = data1.length;
// 
//     for (byte[] arr : remaining) {
//         if (arr.length == 0) {
//             return data1;
//         }
// 
//         bufLength += arr.length;
//     }
// 
//     int offset = data1.length;
//     byte[] buffer = Arrays.copyOf(data1, bufLength);
// 
//     for (byte[] arr : remaining) {
//         System.arraycopy(arr, 0, buffer, offset, arr.length);
//         offset += arr.length;
//     }
// 
//     return buffer;
// }


// Path: src/main/java/org/unitedid/yhsm/internal/DeviceHandler.java
import jssc.SerialPort;
import jssc.SerialPortException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static org.unitedid.yhsm.utility.Utils.concatAllArrays;


    public byte[] read(int readNumBytes) {
        byte[] data = new byte[readNumBytes];
        try {
            data = device.readBytes(readNumBytes);
            readBytes += data.length;
        } catch (SerialPortException e) {
            e.printStackTrace();
        }

        return data;
    }

    public int available() {
        try {
            return device.getInputBufferBytesCount();
        } catch (SerialPortException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean drain() {
        byte[] buffer = new byte[0];
        while(available() > 0) {
            byte[] b = read(1);
            if ((char)b[0] == '\r') {
                log.info("Drained: {}", new String(buffer, 0, buffer.length)); //TODO: Do we really need to log this? If not the loop can be simplified.
                buffer = new byte[0];
            } else {
                buffer = concatAllArrays(buffer, b);