Java Utililty Methods Byte Array from

List of utility methods to do Byte Array from

Description

The list of methods to do Byte Array from are organized into topic(s).

Method

byte[]getBytesFromHexaText(String text)
get Bytes From Hexa Text
Scanner scanner = new Scanner(text);
scanner.nextLine(); 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (scanner.hasNextLine()) {
    String line = scanner.nextLine().trim();
    if (line.startsWith(";")) {
        continue;
    line = line.replace(" ", "");
    for (int i = 0; i < line.length() / 2; i++) {
        String hexStr = line.substring(i * 2, (i + 1) * 2);
        byte b = (byte) Integer.parseInt(hexStr, 16);
        baos.write(b);
byte[] data = baos.toByteArray();
return data;
byte[]getBytesFromImage(File file)
Gets the Bytes from an Image using an FileInputStream
FileInputStream fileInputStream = null;
try {
    fileInputStream = new FileInputStream(file);
    byte[] data = new byte[(int) file.length()];
    fileInputStream.read(data);
    fileInputStream.close();
    return data;
} catch (IOException e) {
...
byte[]getBytesFromInt(int value)
byte array from long
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(value);
dos.flush();
return bos.toByteArray();
byte[]getBytesFromList(List values)
byte array from list
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
for (Object value : values) {
    if (value instanceof String)
        dos.writeUTF((String) value);
    else if (value instanceof Long)
        dos.writeLong((Long) value);
    else if (value instanceof Integer)
...
byte[]getBytesFromObject(Serializable data)
get Bytes From Object
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream objos = new ObjectOutputStream(os);
objos.writeObject(data);
objos.flush();
byte[] result = os.toByteArray();
os.close();
objos.close();
return result;
...
byte[]getBytesFromObject(Serializable obj)
get Bytes From Object
if (obj == null) {
    return null;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bo);
oos.writeObject(obj);
return bo.toByteArray();
byte[]getBytesFromResource(String resource)
get Bytes From Resource
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(resource);
try (BufferedInputStream stream = new BufferedInputStream(is)) {
    int len = stream.available();
    byte[] bytes = new byte[len];
    stream.read(bytes, 0, len);
    return bytes;
byte[]getBytesFromStream(int length, ByteArrayInputStream bais)
get Bytes From Stream
byte[] bytes = new byte[length];
bais.read(bytes, 0, bytes.length);
return bytes;
byte[]getBytesFromString(final String str, final int length, final String coding)
Converts a string to an array of bytes with the length specified
byte[] result = new byte[length];
int len = str.length();
if (len > length)
    len = length;
try {
    System.arraycopy(str.getBytes(coding), 0, result, 0, len);
} catch (UnsupportedEncodingException ex) {
    System.arraycopy(str.getBytes(), 0, result, 0, len);
...
byte[]getBytesFromText(String text, String charset)
Converts a text into a byte array using the given charset, if possible.
try {
    return text.getBytes(charset);
} catch (UnsupportedEncodingException e) {
return text.getBytes();