Byte Array Utils

This is a Util Class for byte array managing

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class ByteArrayUtils {
    static String HEXES = "0123456789ABCDEF";

    // Concatenated Byte Array

    public static byte[] combine(byte[][] messages) {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            for (byte[] bs : messages) {
                outputStream.write(bs);
            }
        } catch (Exception ert) {

        }
        byte[] concatenatedArray = outputStream.toByteArray();
        try {
            outputStream.close();
        } catch (IOException e) {
            System.out.println(e);
        }
        return concatenatedArray;
    }
  //Get a part of large  Byte Array
    public static byte[] getDividedPart(byte[] input, int start, int endpoint) {
        return Arrays.copyOfRange(input, start, endpoint);
    }

    //byte array to hex array conversion
    public static String getHex(byte[] raw) {
        if (raw == null) {
            return null;
        }
        StringBuilder hex = new StringBuilder(2 * raw.length);
        for (byte b : raw) {
            hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(
                    HEXES.charAt((b & 0x0F)));
        }
        return hex.toString();
    }

}

Reactions

Post a Comment

0 Comments