Example usage for java.nio ByteBuffer getLong

List of usage examples for java.nio ByteBuffer getLong

Introduction

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

Prototype

public abstract long getLong();

Source Link

Document

Returns the long at the current position and increases the position by 8.

Usage

From source file:edu.umn.cs.spatialHadoop.core.ZCurvePartitioner.java

@Override
public void readFields(DataInput in) throws IOException {
    mbr.readFields(in);/*from   w  ww . ja  va 2 s .  c om*/
    int partitionCount = in.readInt();
    zSplits = new long[partitionCount];
    int bufferLength = 8 * partitionCount;
    byte[] buffer = new byte[bufferLength];
    in.readFully(buffer);
    ByteBuffer bbuffer = ByteBuffer.wrap(buffer);
    for (int i = 0; i < partitionCount; i++) {
        zSplits[i] = bbuffer.getLong();
    }
    if (bbuffer.hasRemaining())
        throw new RuntimeException("Error reading STR partitioner");
}

From source file:com.datatorrent.contrib.hdht.HDHTDynamicPartitioningTest.java

public long getLong(byte[] value) throws IOException {
    ByteBuffer bb = ByteBuffer.wrap(value);
    return bb.getLong();
}

From source file:org.apache.bookkeeper.bookie.EntryLogTest.java

@Test(timeout = 60000)
public void testMissingLogId() throws Exception {
    File tmpDir = createTempDir("entryLogTest", ".dir");
    File curDir = Bookie.getCurrentDirectory(tmpDir);
    Bookie.checkDirectoryStructure(curDir);

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();
    conf.setLedgerDirNames(new String[] { tmpDir.toString() });
    Bookie bookie = new Bookie(conf);
    // create some entries
    int numLogs = 3;
    int numEntries = 10;
    long[][] positions = new long[2 * numLogs][];
    for (int i = 0; i < numLogs; i++) {
        positions[i] = new long[numEntries];

        EntryLogger logger = new EntryLogger(conf, bookie.getLedgerDirsManager());
        for (int j = 0; j < numEntries; j++) {
            positions[i][j] = logger.addEntry(i, generateEntry(i, j));
        }// w w  w .j av  a  2s  . com
        logger.flush();
    }
    // delete last log id
    File lastLogId = new File(curDir, "lastId");
    lastLogId.delete();

    // write another entries
    for (int i = numLogs; i < 2 * numLogs; i++) {
        positions[i] = new long[numEntries];

        EntryLogger logger = new EntryLogger(conf, bookie.getLedgerDirsManager());
        for (int j = 0; j < numEntries; j++) {
            positions[i][j] = logger.addEntry(i, generateEntry(i, j));
        }
        logger.flush();
    }

    EntryLogger newLogger = new EntryLogger(conf, bookie.getLedgerDirsManager());
    for (int i = 0; i < (2 * numLogs + 1); i++) {
        File logFile = new File(curDir, Long.toHexString(i) + ".log");
        assertTrue(logFile.exists());
    }
    for (int i = 0; i < 2 * numLogs; i++) {
        for (int j = 0; j < numEntries; j++) {
            String expectedValue = "ledger-" + i + "-" + j;
            byte[] value = newLogger.readEntry(i, j, positions[i][j]);
            ByteBuffer buf = ByteBuffer.wrap(value);
            long ledgerId = buf.getLong();
            long entryId = buf.getLong();
            byte[] data = new byte[buf.remaining()];
            buf.get(data);
            assertEquals(i, ledgerId);
            assertEquals(j, entryId);
            assertEquals(expectedValue, new String(data));
        }
    }
}

From source file:com.yifanlu.PSXperiaTool.PSXperiaTool.java

private void patchGame() throws IOException {
    /*/* ww w. j  a va2s  . c o m*/
     * Custom patch format (config/game-patch.bin) is as follows:
     * 0x8 byte little endian: Address in game image to start patching
     * 0x8 byte little endian: Length of patch
     * If there are more patches, repeat after reading the length of patch
     * Note that all games will be patched the same way, so if a game is broken before patching, it will still be broken!
     */
    nextStep("Patching game.");
    File gamePatch = new File(mTempDir, "/config/game-patch.bin");
    if (!gamePatch.exists())
        return;
    Logger.info("Making a copy of game.");
    File tempGame = new File(mTempDir, "game.iso");
    FileUtils.copyFile(mInputFile, tempGame);
    RandomAccessFile game = new RandomAccessFile(tempGame, "rw");
    InputStream patch = new FileInputStream(gamePatch);
    while (true) {
        byte[] rawPatchAddr = new byte[8];
        byte[] rawPatchLen = new byte[8];
        if (patch.read(rawPatchAddr) + patch.read(rawPatchLen) < rawPatchAddr.length + rawPatchLen.length)
            break;
        ByteBuffer bb = ByteBuffer.wrap(rawPatchAddr);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        long patchAddr = bb.getLong();
        bb = ByteBuffer.wrap(rawPatchLen);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        long patchLen = bb.getLong();

        game.seek(patchAddr);
        while (patchLen-- > 0) {
            game.write(patch.read());
        }
    }
    mInputFile = tempGame;
    game.close();
    patch.close();
    Logger.debug("Done patching game.");
}

From source file:com.sm.store.utils.FileStore.java

private void reset() throws IOException {
    ByteBuffer buf = ByteBuffer.allocate(RECORD_SIZE);
    long pos = OFFSET + (long) (totalRecord - 1) * RECORD_SIZE;
    indexChannel.read(buf, pos);//from ww w  .  j  a va  2  s .com
    buf.rewind();
    byte status = buf.get();
    long keyLen = buf.getLong();
    byte[] keys = readChannel(keyLen, keyChannel);
    long data = buf.getLong();
    long block2version = buf.getLong();
    CacheBlock block = new CacheBlock(totalRecord, data, block2version, status);
    byte[] datas = readChannel(block.getDataOffset2Len(), dataChannel);

}

From source file:org.apache.asterix.experiment.builder.AbstractExperiment8Builder.java

protected String getPointLookUpAQL(int round) {
    ByteBuffer bb = ByteBuffer.allocate(8);
    bb.put((byte) 0);
    bb.put((byte) randGen.nextInt(N_PARTITIONS));
    bb.putShort((short) 0);
    bb.putInt(randGen.nextInt((int) (((1 + round) * dataInterval) / 1000)));
    bb.flip();/*from  w ww  .j a  va2 s . co  m*/
    long key = bb.getLong();
    return pointQueryTemplate.replaceAll("\\$KEY\\$", Long.toString(key));
}

From source file:au.org.ala.layers.grid.GridCacheBuilder.java

static void nextRowOfFloats(float[] row, String datatype, boolean byteOrderLSB, int ncols, RandomAccessFile raf,
        byte[] b, float noDataValue) throws IOException {
    int size = 4;
    if (datatype.charAt(0) == 'U') {
        size = 1;//w  w w.j  av  a2  s  .  c  om
    } else if (datatype.charAt(0) == 'B') {
        size = 1;
    } else if (datatype.charAt(0) == 'S') {
        size = 2;
    } else if (datatype.charAt(0) == 'I') {
        size = 4;
    } else if (datatype.charAt(0) == 'L') {
        size = 8;
    } else if (datatype.charAt(0) == 'F') {
        size = 4;
    } else if (datatype.charAt(0) == 'D') {
        size = 8;
    }

    raf.read(b, 0, size * ncols);
    ByteBuffer bb = ByteBuffer.wrap(b);
    if (byteOrderLSB) {
        bb.order(ByteOrder.LITTLE_ENDIAN);
    } else {
        bb.order(ByteOrder.BIG_ENDIAN);
    }

    int i;
    int length = ncols;
    if (datatype.charAt(0) == 'U') {
        for (i = 0; i < length; i++) {
            float ret = bb.get();
            if (ret < 0) {
                ret += 256;
            }
            row[i] = ret;
        }
    } else if (datatype.charAt(0) == 'B') {

        for (i = 0; i < length; i++) {
            row[i] = (float) bb.get();
        }
    } else if (datatype.charAt(0) == 'S') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getShort();
        }
    } else if (datatype.charAt(0) == 'I') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getInt();
        }
    } else if (datatype.charAt(0) == 'L') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getLong();
        }
    } else if (datatype.charAt(0) == 'F') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getFloat();
        }
    } else if (datatype.charAt(0) == 'D') {
        for (i = 0; i < length; i++) {
            row[i] = (float) bb.getDouble();
        }
    } else {
        logger.info("UNKNOWN TYPE: " + datatype);
    }

    for (i = 0; i < length; i++) {
        if (row[i] == noDataValue) {
            row[i] = Float.NaN;
        }
    }
}

From source file:com.sm.store.client.RemoteClientImpl.java

@Override
public long getSeqNo(Key key) {
    StoreParas paras = getSequence(OpType.GetSeqNo, key);
    ByteBuffer buf = ByteBuffer.wrap((byte[]) paras.getValue().getData());
    return buf.getLong();
}

From source file:com.sm.store.client.RemoteClientImpl.java

@Override
public long getSeqNoBlock(Key key, int block) {
    StoreParas paras = getSequence(OpType.GetSeqNoBlock, key, block);
    ByteBuffer buf = ByteBuffer.wrap((byte[]) paras.getValue().getData());
    return buf.getLong();
}

From source file:com.alibaba.jstorm.utils.JStormUtils.java

public static long bytesToLong(byte[] bytes) {
    ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE);
    buffer.put(bytes);//from w w w  .jav  a  2s.c o m
    buffer.flip();// need flip
    return buffer.getLong();
}