Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

In this page you can find the example usage for java.nio ByteBuffer order.

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

Usage

From source file:Main.java

public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength)
        throws IOException {
    // End of central directory record (EOCD)
    // Offset    Bytes     Description[23]
    // 0           4       End of central directory signature = 0x06054b50
    // 4           2       Number of this disk
    // 6           2       Disk where central directory starts
    // 8           2       Number of central directory records on this disk
    // 10          2       Total number of central directory records
    // 12          4       Size of central directory (bytes)
    // 16          4       Offset of start of central directory, relative to start of archive
    // 20          2       Comment length (n)
    // 22          n       Comment
    // For a zip with no archive comment, the
    // end-of-central-directory record will be 22 bytes long, so
    // we expect to find the EOCD marker 22 bytes from the end.

    final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4);
    zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN);
    fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive)
    fileChannel.read(zipCentralDirectoryStart);
    final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0);
    return centralDirStartOffset;
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

public static String encodeBase64(double[] data, boolean swapByteOrder) throws IOException {

    byte[] decodedData = doubleToByte(data);

    if (swapByteOrder) {
        ByteBuffer buf = ByteBuffer.wrap(decodedData);
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.get(decodedData);//from  ww  w .  j a v a 2  s.  co m
    }

    Base64 codec = new Base64();
    byte[] encodedData = codec.encode(decodedData);

    String result = new String(encodedData);

    return result;
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

public static double[] decodeBase64(String dataString, boolean swapByteOrder) throws IOException {

    byte[] encodedData = dataString.getBytes();

    Base64 codec = new Base64();
    byte[] decodedData = codec.decode(encodedData);

    if (swapByteOrder) {
        ByteBuffer buf = ByteBuffer.wrap(decodedData);
        buf = buf.order(ByteOrder.LITTLE_ENDIAN);
        buf.get(decodedData);/*  w w  w.  j  a  v  a 2s. c  o m*/
    }

    double[] result = byteToDouble(decodedData);

    return result;
}

From source file:info.gehrels.flockDBClient.ByteHelper.java

static ByteBuffer asByteBufferOrNull(long... destinationIds) {
    ByteBuffer buf = null;
    if (isNotEmpty(destinationIds)) {
        buf = ByteBuffer.wrap(new byte[destinationIds.length * (Long.SIZE / 8)]);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        for (long destinationId : destinationIds) {
            buf.putLong(destinationId);/*from   ww  w . j a va 2  s . c o m*/
        }
        buf.rewind();
    }
    return buf;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct float buffer with native byte order.
 *
 * @param size/*from   ww w  .j a  v  a 2 s. c  om*/
 *            The data
 * @return The created direct float buffer
 */

public static FloatBuffer createDirectFloatBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * FLOAT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asFloatBuffer();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct short buffer with native byte order.
 *
 * @param size/*from   w  ww  .  ja  v  a2 s . c o  m*/
 *            The data
 * @return The created direct short buffer
 */

public static ShortBuffer createDirectShortBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * SHORT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asShortBuffer();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct integer buffer with native byte order.
 *
 * @param size/*  w w  w  .  j  a  v  a  2 s.  c  o m*/
 *            The data
 * @return The created direct integer buffer
 */

public static IntBuffer createDirectIntegerBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * INTEGER_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asIntBuffer();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified short buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away.//w w  w .j ava 2  s.c o m
 *
 * @param buffer
 *            The short buffer to convert
 * @return The converted short buffer or the source buffer if no conversion
 *         is needed
 */

public static ShortBuffer convertToNativeEndian(final ShortBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final ShortBuffer shorts = bytes.asShortBuffer();
    shorts.put(buffer).rewind();
    return shorts;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified integer buffer to native endian and returns this
 * new buffer. If buffer is already in correct endian format then it is
 * returned right away.//  w  w  w . j a v  a 2  s.com
 *
 * @param buffer
 *            The integer buffer to convert
 * @return The converted integer buffer or the source buffer if no
 *         conversion is needed
 */

public static IntBuffer convertToNativeEndian(final IntBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final IntBuffer ints = bytes.asIntBuffer();
    ints.put(buffer).rewind();
    return ints;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified float buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away.//from w  ww .  j  a  v  a  2  s . c  om
 *
 * @param buffer
 *            The float buffer to convert
 * @return The converted float buffer or the source buffer if no conversion
 *         is needed
 */

public static FloatBuffer convertToNativeEndian(final FloatBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    if (log.isTraceEnabled())
        log.trace("Converting endianess of " + buffer.capacity() + " floats");

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final FloatBuffer floats = bytes.asFloatBuffer();
    floats.put(buffer).rewind();
    return floats;
}