Example usage for org.apache.commons.net.util Base64 encodeBase64StringUnChunked

List of usage examples for org.apache.commons.net.util Base64 encodeBase64StringUnChunked

Introduction

In this page you can find the example usage for org.apache.commons.net.util Base64 encodeBase64StringUnChunked.

Prototype

public static String encodeBase64StringUnChunked(byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm, without using chunking.

Usage

From source file:org.openhab.binding.max.internal.command.MCommand.java

@Override
public String getCommandString() {
    int deviceCount = 0;
    int roomCount = 0;

    ByteArrayOutputStream message = new ByteArrayOutputStream();

    try {//from  www  .  j  av a  2  s  . c  o  m
        byte[] header = { MAGIC_NR, M_VERSION, (byte) rooms.size() };
        message.write(header);

        Set<Integer> sortedRooms = new TreeSet<>();
        for (RoomInformation room : rooms) {
            sortedRooms.add(room.getPosition());
        }

        for (Integer roomPos : sortedRooms) {
            for (RoomInformation room : rooms) {
                if (room.getPosition() == roomPos) {
                    if (roomCount < MAX_GROUP_COUNT) {
                        byte[] roomName = StringUtils.abbreviate(room.getName(), MAX_NAME_LENGTH)
                                .getBytes(StandardCharsets.UTF_8);
                        byte[] nameLength = new byte[] { (byte) roomName.length };
                        byte[] rfAddress = Utils.hexStringToByteArray(room.getRFAddress());
                        message.write(roomPos.byteValue());
                        message.write(nameLength);
                        message.write(roomName);
                        message.write(rfAddress);
                    } else {
                        logger.warn("{} exceeds max number of rooms ({}). Ignored", room, MAX_GROUP_COUNT);
                    }
                    roomCount++;
                }
            }
        }
        for (Device di : devices) {
            if (deviceCount < MAX_DEVICES_COUNT) {
                deviceCount++;
            } else {
                logger.warn("{} exceeds max number of devices ({}). Ignored", di, MAX_DEVICES_COUNT);
            }
        }
        message.write((byte) deviceCount);
        for (Device di : devices) {
            if (deviceCount > 0) {
                byte[] deviceType = { (byte) di.getType().getValue() };
                byte[] rfAddress = Utils.hexStringToByteArray(di.getRFAddress());
                byte[] deviceName = StringUtils.abbreviate(di.getName(), MAX_NAME_LENGTH)
                        .getBytes(StandardCharsets.UTF_8);
                byte[] nameLength = { (byte) deviceName.length };
                byte[] serialNumber = di.getSerialNumber().getBytes(StandardCharsets.UTF_8);
                byte[] roomId = { (byte) di.getRoomId() };

                message.write(deviceType);
                message.write(rfAddress);
                message.write(serialNumber);
                message.write(nameLength);
                message.write(deviceName);
                message.write(roomId);
            } else {
                logger.warn("{} exceeds max number of devices ({}). Ignored", di, MAX_DEVICES_COUNT);
            }
            deviceCount--;
        }

        byte[] dst = { 0x01 };
        message.write(dst);

    } catch (IOException e) {
        logger.debug("Error while generating m: command: {}", e.getMessage(), e);

    }

    final String encodedString = Base64.encodeBase64StringUnChunked(message.toByteArray());
    final StringBuilder commandStringBuilder = new StringBuilder();
    int parts = (int) Math.round(encodedString.length() / MAX_MSG_LENGTH + 0.5);
    for (int i = 0; i < parts; i++) {
        String partString = StringUtils.abbreviate(encodedString.substring((i) * MAX_MSG_LENGTH),
                MAX_MSG_LENGTH);
        commandStringBuilder.append("m:").append(String.format("%02d", i)).append(",").append(partString)
                .append('\r').append('\n');
    }

    return commandStringBuilder.toString();
}

From source file:org.openhab.binding.max.internal.command.M_Command.java

@Override
public String getCommandString() {
    int deviceCount = 0;
    int roomCount = 0;

    ByteArrayOutputStream message = new ByteArrayOutputStream();

    try {/*  www.j  av  a 2  s. co  m*/
        byte[] header = { MAGIC_NR, M_VERSION, (byte) rooms.size() };
        message.write(header);

        TreeSet<Integer> sortedRooms = new TreeSet<Integer>();
        for (RoomInformation room : rooms) {
            sortedRooms.add(room.getPosition());

        }

        for (Integer roomPos : sortedRooms) {
            for (RoomInformation room : rooms) {
                if (room.getPosition() == roomPos) {
                    if (roomCount < MAX_GROUP_COUNT) {
                        byte[] roomName = StringUtils.abbreviate(room.getName(), MAX_NAME_LENGTH)
                                .getBytes("UTF-8");
                        byte[] nameLength = new byte[] { (byte) roomName.length };
                        byte[] rfAddress = Utils.hexStringToByteArray(room.getRFAddress());
                        message.write(roomPos.byteValue());
                        message.write(nameLength);
                        message.write(roomName);
                        message.write(rfAddress);
                    } else {
                        logger.warn("{} exceeds max number of rooms ({}). Ignored", room.toString(),
                                MAX_GROUP_COUNT);
                    }
                    roomCount++;
                }
            }
        }
        for (Device di : devices) {
            if (deviceCount < MAX_DEVICES_COUNT) {
                deviceCount++;
            } else {
                logger.warn("{} exceeds max number of devices ({}). Ignored", di.toString(), MAX_DEVICES_COUNT);
            }
        }
        message.write((byte) deviceCount);
        for (Device di : devices) {
            if (deviceCount > 0) {
                byte[] deviceType = { (byte) di.getType().getValue() };
                byte[] rfAddress = Utils.hexStringToByteArray(di.getRFAddress());
                byte[] deviceName = StringUtils.abbreviate(di.getName(), MAX_NAME_LENGTH).getBytes("UTF-8");
                byte[] nameLength = { (byte) deviceName.length };
                byte[] serialNumber = di.getSerialNumber().getBytes();
                byte[] roomId = { (byte) di.getRoomId() };

                message.write(deviceType);
                message.write(rfAddress);
                message.write(serialNumber);
                message.write(nameLength);
                message.write(deviceName);
                message.write(roomId);
            } else {
                logger.warn("{} exceeds max number of devices ({}). Ignored", di.toString(), MAX_DEVICES_COUNT);
            }
            deviceCount--;
        }

        byte[] dst = { 0x01 };
        message.write(dst);

    } catch (IOException e) {
        logger.debug("Error while generating m: command: {}", e.getMessage(), e);

    }

    String encodedString = Base64.encodeBase64StringUnChunked(message.toByteArray());

    String commandString = "";
    int parts = (int) Math.round(encodedString.length() / MAX_MSG_LENGTH + 0.5);
    for (int i = 0; i < parts; i++) {
        String partString = StringUtils.abbreviate(encodedString.substring((i) * MAX_MSG_LENGTH),
                MAX_MSG_LENGTH);
        commandString = commandString + "m:" + String.format("%02d", i) + "," + partString + '\r' + '\n';
    }

    return commandString;
}

From source file:org.openhab.binding.max.internal.command.TCommand.java

@Override
public String getCommandString() {
    final int updateForced = forceUpdate ? FORCE_UPDATE : NO_FORCE_UPDATE;
    byte[] commandArray = null;
    for (String rfAddress : rfAddresses) {
        commandArray = ArrayUtils.addAll(Utils.hexStringToByteArray(rfAddress), commandArray);
    }// ww w. j  a v a  2  s.  c om
    String encodedString = Base64.encodeBase64StringUnChunked(commandArray);

    return "t:" + String.format("%02d", rfAddresses.size()) + "," + updateForced + "," + encodedString + '\r'
            + '\n';
}

From source file:org.openhab.binding.max.internal.command.T_Command.java

@Override
public String getCommandString() {
    int updateForced = 0;
    if (forceUpdate) {
        updateForced = FORCE_UPDATE;//  w  w  w.  ja  v  a2s . com
    } else {
        updateForced = NO_FORCE_UPDATE;
    }
    byte[] commandArray = null;
    for (String rfAddress : rfAddresses) {
        commandArray = ArrayUtils.addAll(Utils.hexStringToByteArray(rfAddress), commandArray);
    }
    String encodedString = Base64.encodeBase64StringUnChunked(commandArray);

    String cmd = "t:" + String.format("%02d", rfAddresses.size()) + "," + updateForced + "," + encodedString
            + '\r' + '\n';
    return cmd;
}