Common

Input/Output

Reflection

BytesHelper Tutorial

This tutorial provides an overview of the BytesHelper class, which is part of the lightweight-component/aj-util library. BytesHelper offers a collection of utility methods for working with byte arrays in Java. This guide will cover the purpose of each method and provide usage examples.

Introduction

The BytesHelper class provides several useful methods for manipulating byte arrays, including:

These methods can be helpful in various scenarios, such as network programming, file I/O, and data processing.

Methods

1. subBytes(byte[] data, int off, int length)

Extracts a subarray from a given byte array.

Example:

byte[]original={0x01,0x02,0x03,0x04,0x05};
        byte[]sub=BytesHelper.subBytes(original,1,3); // Extract from index 1, length 3
// sub will be {0x02, 0x03, 0x04}

2. byteIndexOf(byte[] data, byte[] search, int start)

Finds the index of a byte array (search) within another byte array (data), starting the search from a specified index (start).

Example:

byte[]data={0x01,0x02,0x03,0x04,0x05};
        byte[]search={0x03,0x04};
        int index=BytesHelper.byteIndexOf(data,search,0); // Search from the beginning
// index will be 2

3. byteIndexOf(byte[] data, byte[] search)

Finds the index of a byte array (search) within another byte array (data), starting the search from the beginning (index 0). This is an overloaded version of the previous method.

Example:

byte[]data={0x01,0x02,0x03,0x04,0x05};
        byte[]search={0x03,0x04};
        int index=BytesHelper.byteIndexOf(data,search); // Search from the beginning
// index will be 2

4. concat(byte[] a, byte[] b)

Concatenates two byte arrays into a new byte array.

Example:

byte[]a={0x01,0x02};
        byte[]b={0x03,0x04};
        byte[]combined=BytesHelper.concat(a,b);
// combined will be {0x01, 0x02, 0x03, 0x04}

5. bytesToHexStr(byte[] bytes)

Converts a byte array to its hexadecimal string representation.

Example:

byte[]bytes={0x1A,0x2B,0x3C};
        String hexString=BytesHelper.bytesToHexStr(bytes);
// hexString will be "1A2B3C"

6. parseHexStr2Byte(String hex) (Hypothetical - Not found in the provided code, but good to include if it existed or you implemented it)

Converts a hexadecimal string to a byte array. This method appears in the test code, so it's likely part of the library, or intended to be.

Example:

String hexString="1A2B3C";
        byte[]bytes=BytesHelper.parseHexStr2Byte(hexString);
// bytes will be {0x1A, 0x2B, 0x3C}

Unit Test Example (Based on the provided test code):

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestBytesHelper {
    @Test
    public void testParseHexStr2Byte() {
        byte[] bs = BytesHelper.parseHexStr2Byte("1A2B3C");
        assert bs != null;
        assertEquals(0x1A, bs[0]);
    }
}

Conclusion

The BytesHelper class provides a convenient set of utilities for common byte array manipulations in Java. By using these methods, you can simplify your code and avoid writing repetitive byte array handling logic. Remember to consult the library's Javadoc for the most up-to-date information and additional methods.