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[]getBytes(byte value)
get Bytes
byte[] byteArray = new byte[1];
byteArray[0] = value;
return byteArray;
byte[]getBytes(byte[] buffer, int offset)
get Bytes
byte[] b = new byte[buffer.length - offset];
System.arraycopy(buffer, offset, b, 0, buffer.length - offset);
return b;
byte[]getBytes(Class clazz)
get Bytes
ByteArrayOutputStream result = new ByteArrayOutputStream();
try (InputStream in = getResourceAsStrem(getBytecodeRelativePath(clazz))) {
    byte[] buffer = new byte[1024];
    for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
        result.write(buffer, 0, read);
return result.toByteArray();
...
byte[]getBytes(Class clazz)
get Bytes
String resourcePath = clazz.getName().replace('.', '/') + ".class";
ClassLoader loader = clazz.getClassLoader();
InputStream in;
if (loader == null) {
    in = ClassLoader.getSystemResourceAsStream(resourcePath);
} else {
    in = loader.getResourceAsStream(resourcePath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[100];
while (true) {
    int len = in.read(buf);
    if (len < 0) {
        break;
    out.write(buf, 0, len);
in.close();
return out.toByteArray();
byte[]getBytes(Class cls, String resourceName)
get Bytes
java.io.InputStream is = cls.getResourceAsStream(resourceName);
try {
    return getBytes(is);
} finally {
    is.close();
byte[]getBytes(final InputStream is)
Reads an InputStream and returns the read byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int a = 0;
while ((a = is.read(buffer)) != -1) {
    baos.write(buffer, 0, a);
baos.close();
buffer = null;
...
byte[]getBytes(final Serializable obj)
get Bytes
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    try {
        oos.writeObject(obj);
    } finally {
        oos.close();
} finally {
    bos.close();
return bos.toByteArray();
byte[]getBytes(final String data, String charset)
Converts the specified string to a byte array.
if (data == null) {
    throw new IllegalArgumentException("data may not be null");
if (charset == null || charset.length() == 0) {
    throw new IllegalArgumentException("charset may not be null or empty");
try {
    return data.getBytes(charset);
...
byte[]getBytes(final String data, String charset)
get Bytes
if (data == null) {
    throw new IllegalArgumentException("data may not be null");
if (charset == null || charset.length() == 0) {
    throw new IllegalArgumentException("charset may not be null or empty");
try {
    return data.getBytes(charset);
...
byte[]getBytes(final String value, final String characterEncoding)
get Bytes
if (null == value) {
    return EMPTY_BUFFER;
return value.getBytes(characterEncoding);