Java Byte Array Create toBytes(Byte[] values)

Here you can find the source of toBytes(Byte[] values)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(Byte[] values) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final byte DEFAULT_BYTE = 0;

    public static byte[] toBytes(Byte[] values) {
        return toBytes(values, DEFAULT_BYTE);
    }/*from w  w w  .ja va 2s  .co  m*/

    public static byte[] toBytes(Byte[] values, int defaultValue) {
        byte[] results = new byte[0];
        if (values != null) {
            results = new byte[values.length];
            for (int i = 0; i < results.length; i++) {
                Byte element = values[i];
                try {
                    results[i] = (element != null ? element.byteValue()
                            : (byte) defaultValue);
                } catch (Exception ex) {
                    // ex.printStackTrace();
                }
            }
        }
        return results;
    }

    public static Byte[] toBytes(byte[] values) {
        return toBytes(values, DEFAULT_BYTE);
    }

    public static Byte[] toBytes(byte[] values, int defaultValue) {
        Byte[] results = new Byte[0];
        if (values != null) {
            results = new Byte[values.length];
            for (int i = 0; i < results.length; i++) {
                byte element = values[i];
                try {
                    results[i] = new Byte(element);
                } catch (Exception ex) {
                    results[i] = (byte) defaultValue;
                    // ex.printStackTrace();
                }
            }
        }
        return results;
    }
}

Related

  1. toBytes(boolean[] input)
  2. toBytes(byte data)
  3. toBytes(byte[] b, int offset, long val)
  4. toBytes(byte[] buffer, int pos, String string)
  5. toBytes(byte[] ipAddress, int port)
  6. toBytes(char c)
  7. toBytes(char value)
  8. toBytes(char[] cbuf, byte[] bs)
  9. toBytes(char[] chars)