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:org.apache.nifi.processors.evtx.parser.BinaryReader.java

/**
 * Reads 2 bytes in big endian order and returns the int value
 *
 * @return the value/* w ww  . j  a v  a  2s  .c o m*/
 */
public int readWordBE() {
    byte[] bytes = new byte[4];
    readBytes(bytes, 2, 2);
    return ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).getInt();
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java

/**
 * Converts a {@code long} to a {@code byte[]}. A long is composed of eight
 * bytes. Writes eight bytes containing the given long value, in the current
 * byte order. Numbering them from 0 (the most important) to 7 (the least
 * important), the resulting array will have all them placed in the same
 * position.//  w ww  .ja  v a  2 s. c  o m
 * 
 * @return the converted list.
 * @param x
 *            the long to convert
 * @return the resulting array.
 */
public static byte[] longToBytes(long x) {
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putLong(x);
    return buffer.array();
}

From source file:com.slytechs.file.snoop.SnoopFileCapture.java

public ByteOrder order() {
    return ByteOrder.BIG_ENDIAN;
}

From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java

private void writeMMHeaderAndSummaryMD(JSONObject summaryMD) throws IOException {
    if (summaryMD.has("Comment")) {
        summaryMD.remove("Comment");
    }/*from   w  ww  . j  a  v a  2s. c o m*/
    String summaryMDString = summaryMD.toString();
    int mdLength = summaryMDString.length();
    ByteBuffer buffer = ByteBuffer.allocate(40).order(BYTE_ORDER);
    if (BYTE_ORDER.equals(ByteOrder.BIG_ENDIAN)) {
        buffer.asCharBuffer().put(0, (char) 0x4d4d);
    } else {
        buffer.asCharBuffer().put(0, (char) 0x4949);
    }
    buffer.asCharBuffer().put(1, (char) 42);
    buffer.putInt(4, 40 + mdLength);
    //8 bytes for file header +
    //8 bytes for index map offset header and offset +
    //8 bytes for display settings offset header and display settings offset
    //8 bytes for comments offset header and comments offset
    //8 bytes for summaryMD header  summary md length + 
    //1 byte for each character of summary md     
    buffer.putInt(32, SUMMARY_MD_HEADER);
    buffer.putInt(36, mdLength);
    ByteBuffer[] buffers = new ByteBuffer[2];
    buffers[0] = buffer;
    buffers[1] = ByteBuffer.wrap(getBytesFromString(summaryMDString));
    fileChannel_.write(buffers);
    filePosition_ += buffer.position() + mdLength;
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static byte[] encodeUUID(UUID uuid) {
    return ByteBuffer.allocate(2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN)
            .putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()).array();
}

From source file:org.kalypso.grid.BinaryGeoGrid.java

/**
 * @param fillGrid/* w w w.  jav  a  2  s.com*/
 *          If set to <code>true</code>, the grid will be initially filled with no-data values. Else, the grid values
 *          are undetermined.
 */
public BinaryGeoGrid(final FileChannel channel, final int sizeX, final int sizeY, final int scale,
        final Coordinate origin, final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS,
        final boolean fillGrid) throws GeoGridException {
    super(origin, offsetX, offsetY, sourceCRS);

    m_readBuffer = ByteBuffer.allocate(4 * sizeX * BUFFER_LINES);
    m_readBuffer.order(ByteOrder.BIG_ENDIAN);

    /* create write buffer, also marks this grid as writable */
    m_writeBuffer = ByteBuffer.allocate(4);
    m_writeBuffer.order(ByteOrder.BIG_ENDIAN);

    try {
        m_channel = channel;
        m_binFile = null;

        m_header = new BinaryGeoGridHeader(sizeX, sizeY, scale);

        m_unscaledMin = null;
        m_unscaledMax = null;

        /* Initialize grid */
        // m_randomAccessFile.setLength( HEADER_SIZE + sizeX * sizeY * 4 + 2 * 4 );
        m_channel.truncate(BinaryGeoGridHeader.HEADER_SIZE + sizeX * sizeY * 4 + 2 * 4);

        /* Read header */
        m_channel.position(0);
        m_header.write(m_channel);

        /* Set everything to non-data */
        if (fillGrid) {
            final ByteBuffer buffer = ByteBuffer.allocate(sizeX * 4);
            for (int y = 0; y < sizeY; y++) {
                buffer.rewind();
                for (int x = 0; x < sizeX; x++)
                    buffer.putInt(NO_DATA);
                m_channel.write(buffer);
            }
        }

        /* Read statistical data */
        saveStatistically();
    } catch (final IOException e) {
        throw new GeoGridException("Failed to initiate random access file", e);
    }
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static UUID decodeUUID(byte[] bytes, int offset) {
    ByteBuffer buf = ByteBuffer.wrap(bytes, offset, 2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN);
    long mostSigBits = buf.getLong();
    long leastSigBits = buf.getLong();
    return new UUID(mostSigBits, leastSigBits);
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java

/**
 * Converts an {@code int} to a {@code byte[]}. An int is composed of four
 * bytes. Writes four bytes containing the given int value, in the current
 * byte order. Numbering them from 0 (the most important) to 5 (the least
 * important), the resulting array will have all them placed in the same
 * position.//from  w  ww  . j  a  va  2 s .  c  o  m
 * 
 * @param x
 *            the int to convert.
 * @return the resulting array.
 */
public static byte[] intToBytes(int x) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putInt(x);
    return buffer.array();
}

From source file:com.palantir.atlasdb.ptobject.EncodingUtils.java

public static UUID decodeFlippedUUID(byte[] bytes, int offset) {
    ByteBuffer buf = ByteBuffer.wrap(bytes, offset, 2 * Longs.BYTES).order(ByteOrder.BIG_ENDIAN);
    long mostSigBits = -1L ^ buf.getLong();
    long leastSigBits = -1L ^ buf.getLong();
    return new UUID(mostSigBits, leastSigBits);
}

From source file:ome.io.nio.PixelsService.java

private PixelsPyramidMinMaxStore performWrite(final Pixels pixels, final File pixelsPyramidFile,
        final BfPyramidPixelBuffer pixelsPyramid, final File pixelsFile, final String pixelsFilePath,
        final String originalFilePath) {

    final PixelBuffer source;
    final Dimension tileSize;
    final PixelsPyramidMinMaxStore minMaxStore;

    if (pixelsFile.exists()) {
        minMaxStore = null;//w  ww .  j  a  v  a  2 s  .  c o m
        source = createRomioPixelBuffer(pixelsFilePath, pixels, false);
        // FIXME: This should be configuration or service driven
        // FIXME: Also implemented in RenderingBean.getTileSize()
        tileSize = new Dimension(Math.min(pixels.getSizeX(), sizes.getTileWidth()),
                Math.min(pixels.getSizeY(), sizes.getTileHeight()));
    } else {
        minMaxStore = new PixelsPyramidMinMaxStore(pixels.getSizeC());
        int series = getSeries(pixels);
        BfPixelBuffer bfPixelBuffer = createMinMaxBfPixelBuffer(originalFilePath, series, minMaxStore);
        pixelsPyramid
                .setByteOrder(bfPixelBuffer.isLittleEndian() ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
        source = bfPixelBuffer;
        // If the tile sizes we've been given are completely ridiculous
        // then reset them to WIDTHxHEIGHT. Currently these conditions are:
        //  * TileWidth == ImageWidth
        //  * TileHeight == ImageHeight
        //  * Smallest tile dimension divided by the largest resolution
        //    level factor is < 1.
        // -- Chris Allan (ome:#5224).
        final Dimension sourceTileSize = source.getTileSize();
        final double tileWidth = sourceTileSize.getWidth();
        final double tileHeight = sourceTileSize.getHeight();
        final boolean tileDimensionTooSmall;
        double factor = Math.pow(2, 5);
        if (((tileWidth / factor) < 1.0) || ((tileHeight / factor) < 1.0)) {
            tileDimensionTooSmall = true;
        } else {
            tileDimensionTooSmall = false;
        }
        if (tileWidth == source.getSizeX() || tileHeight == source.getSizeY() || tileDimensionTooSmall) {
            tileSize = new Dimension(Math.min(pixels.getSizeX(), sizes.getTileWidth()),
                    Math.min(pixels.getSizeY(), sizes.getTileHeight()));
        } else {
            tileSize = sourceTileSize;
        }
    }
    log.info("Destination pyramid tile size: " + tileSize);

    try {
        final double totalTiles = source.getSizeZ() * source.getSizeC() * source.getSizeT()
                * (Math.ceil(source.getSizeX() / tileSize.getWidth()))
                * (Math.ceil(source.getSizeY() / tileSize.getHeight()));
        final int tenPercent = Math.max((int) totalTiles / 10, 1);
        Utils.forEachTile(new TileLoopIteration() {
            public void run(int z, int c, int t, int x, int y, int w, int h, int tileCount) {
                if (log.isInfoEnabled() && tileCount % tenPercent == 0) {
                    log.info(String.format("Pyramid creation for Pixels:%d %d/%d (%d%%).", pixels.getId(),
                            tileCount + 1, (int) totalTiles, (int) (tileCount / totalTiles * 100)));
                }
                try {
                    PixelData tile = source.getTile(z, c, t, x, y, w, h);
                    pixelsPyramid.setTile(tile.getData().array(), z, c, t, x, y, w, h);
                } catch (IOException e1) {
                    log.error("FAIL -- Error during tile population", e1);
                    try {
                        pixelsPyramidFile.delete();
                        FileUtils.touch(pixelsPyramidFile); // ticket:5189
                    } catch (Exception e2) {
                        log.warn("Error clearing empty or incomplete pixel " + "buffer.", e2);
                    }
                    return;
                }
            }
        }, source, (int) tileSize.getWidth(), (int) tileSize.getHeight());

        log.info("SUCCESS -- Pyramid created for pixels id:" + pixels.getId());

    }

    finally {
        if (source != null) {
            try {
                source.close();
            } catch (IOException e) {
                log.error("Error closing pixel pyramid.", e);
            }
        }
    }
    return minMaxStore;
}