Example usage for java.nio ByteOrder BIG_ENDIAN

List of usage examples for java.nio ByteOrder BIG_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder BIG_ENDIAN.

Prototype

ByteOrder BIG_ENDIAN

To view the source code for java.nio ByteOrder BIG_ENDIAN.

Click Source Link

Document

This constant represents big endian.

Usage

From source file:Main.java

public static byte[] getIntArray(int val) {
    ByteBuffer typeBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    typeBuffer.putInt(val);
    return typeBuffer.array();
}

From source file:Main.java

public static void putMessageLength(byte[] message) {
    ByteBuffer lengthBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    lengthBuffer.putInt(message.length);

    byte[] lenArray = lengthBuffer.array();

    for (int i = 0; i < 4; i++)
        message[i] = lenArray[i];/*  ww w . j  a v  a2 s  . co m*/
}

From source file:Main.java

public static int readInt(byte[] message, int start) {
    /* Read in the type */
    ByteBuffer typeBuffer = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
    typeBuffer.put(message, start, 4).position(0);
    return typeBuffer.getInt();
}

From source file:Main.java

public static byte[] intToByteArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putInt(value);/*  w w  w .  ja  va 2s .  c o m*/
    return buffer.array();
}

From source file:Main.java

public static byte[] shortToByteArray(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putShort(value);/*from   w  w  w  . j  a va2  s  .  c o  m*/
    return buffer.array();
}

From source file:Main.java

public static float[] byteToFloatArrayBigEndian(byte[] data) {
    float[] floats = new float[data.length / BYTES_PER_FLOAT];
    ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN).asFloatBuffer().get(floats);
    return floats;
}

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }/* w  ww.ja va2  s. c o  m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static int byteArrayToInt(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }//  w w  w.jav  a 2s .  c o  m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

private static IntBuffer getImageAsARGBIntBuffer(BufferedImage image) {
    DataBuffer buffer = image.getRaster().getDataBuffer();

    if (buffer instanceof DataBufferInt) {
        return IntBuffer.wrap(((DataBufferInt) buffer).getData());
    } else if (buffer instanceof DataBufferByte) {
        return ByteBuffer.wrap(((DataBufferByte) buffer).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer();
    } else {/*from  www .  j  a v  a  2s .c om*/
        int width = image.getWidth();
        int height = image.getHeight();
        int[] pixels = new int[width * height];
        image.getRGB(0, 0, width, height, pixels, 0, width);
        return IntBuffer.wrap(pixels);
    }
}

From source file:Main.java

public static void pokeInt(byte[] dst, int offset, int value, ByteOrder order) {
    if (order == ByteOrder.BIG_ENDIAN) {
        dst[offset++] = (byte) ((value >> 24) & 0xff);
        dst[offset++] = (byte) ((value >> 16) & 0xff);
        dst[offset++] = (byte) ((value >> 8) & 0xff);
        dst[offset] = (byte) ((value >> 0) & 0xff);
    } else {// w w  w .ja va  2 s  .c o  m
        dst[offset++] = (byte) ((value >> 0) & 0xff);
        dst[offset++] = (byte) ((value >> 8) & 0xff);
        dst[offset++] = (byte) ((value >> 16) & 0xff);
        dst[offset] = (byte) ((value >> 24) & 0xff);
    }
}