List of usage examples for java.nio ByteBuffer getLong
public abstract long getLong();
From source file:GetData.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); // Allocation automatically zeroes the ByteBuffer: int i = 0;//from w w w. j a v a2 s . com while (i++ < bb.limit()) if (bb.get() != 0) System.out.println("nonzero"); System.out.println("i = " + i); bb.rewind(); // Store and read a char array: bb.asCharBuffer().put("Howdy!"); char c; while ((c = bb.getChar()) != 0) System.out.print(c + " "); System.out.println(); bb.rewind(); // Store and read a short: bb.asShortBuffer().put((short) 471142); System.out.println(bb.getShort()); bb.rewind(); // Store and read an int: bb.asIntBuffer().put(99471142); System.out.println(bb.getInt()); bb.rewind(); // Store and read a long: bb.asLongBuffer().put(99471142); System.out.println(bb.getLong()); bb.rewind(); // Store and read a float: bb.asFloatBuffer().put(99471142); System.out.println(bb.getFloat()); bb.rewind(); // Store and read a double: bb.asDoubleBuffer().put(99471142); System.out.println(bb.getDouble()); bb.rewind(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("primes.txt"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(1024); buf.position(buf.limit());//from w w w .j a va2 s .c o m while (true) { if (buf.remaining() < 8) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } int strLength = (int) buf.getDouble(); if (buf.remaining() < 2 * strLength) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } byte[] strChars = new byte[2 * strLength]; buf.get(strChars); if (buf.remaining() < 8) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); } inFile.close(); }
From source file:Lock.java
public static void main(String args[]) throws IOException, InterruptedException { RandomAccessFile file = null; // The file we'll lock FileChannel f = null; // The channel to the file FileLock lock = null; // The lock object we hold try { // The finally clause closes the channel and releases the lock // We use a temporary file as the lock file. String tmpdir = System.getProperty("java.io.tmpdir"); String filename = Lock.class.getName() + ".lock"; File lockfile = new File(tmpdir, filename); // Create a FileChannel that can read and write that file. // Note that we rely on the java.io package to open the file, // in read/write mode, and then just get a channel from it. // This will create the file if it doesn't exit. We'll arrange // for it to be deleted below, if we succeed in locking it. file = new RandomAccessFile(lockfile, "rw"); f = file.getChannel();// w w w . ja v a2 s.c om // Try to get an exclusive lock on the file. // This method will return a lock or null, but will not block. // See also FileChannel.lock() for a blocking variant. lock = f.tryLock(); if (lock != null) { // We obtained the lock, so arrange to delete the file when // we're done, and then write the approximate time at which // we'll relinquish the lock into the file. lockfile.deleteOnExit(); // Just a temporary file // First, we need a buffer to hold the timestamp ByteBuffer bytes = ByteBuffer.allocate(8); // a long is 8 bytes // Put the time in the buffer and flip to prepare for writing // Note that many Buffer methods can be "chained" like this. bytes.putLong(System.currentTimeMillis() + 10000).flip(); f.write(bytes); // Write the buffer contents to the channel f.force(false); // Force them out to the disk } else { // We didn't get the lock, which means another instance is // running. First, let the user know this. System.out.println("Another instance is already running"); // Next, we attempt to read the file to figure out how much // longer the other instance will be running. Since we don't // have a lock, the read may fail or return inconsistent data. try { ByteBuffer bytes = ByteBuffer.allocate(8); f.read(bytes); // Read 8 bytes from the file bytes.flip(); // Flip buffer before extracting bytes long exittime = bytes.getLong(); // Read bytes as a long // Figure out how long that time is from now and round // it to the nearest second. long secs = (exittime - System.currentTimeMillis() + 500) / 1000; // And tell the user about it. System.out.println("Try again in about " + secs + " seconds"); } catch (IOException e) { // This probably means that locking is enforced by the OS // and we were prevented from reading the file. } // This is an abnormal exit, so set an exit code. System.exit(1); } // Simulate a real application by sleeping for 10 seconds. System.out.println("Starting..."); Thread.sleep(10000); System.out.println("Exiting."); } finally { // Always release the lock and close the file // Closing the RandomAccessFile also closes its FileChannel. if (lock != null && lock.isValid()) lock.release(); if (file != null) file.close(); } }
From source file:org.apache.hadoop.hdfs.hoss.db.FileBlockStore.java
public static void main(String[] args) { final int BLOCK_SIZE = 64; final int TOTAL = 10000000; final FileBlockStore fbs = new FileBlockStore("./data/block", BLOCK_SIZE, false); fbs.delete();// w w w. java2s . c o m // fbs.enableMmap(); // Test MMAPED? fbs.open(); long start = System.currentTimeMillis(); for (int i = 0; i < TOTAL; i++) { final WriteBuffer wbuf = fbs.set(i); final ByteBuffer buf = wbuf.buf(); StringSerializer.fromStringToBuffer(buf, "hehe" + i); buf.putLong(i); buf.flip(); wbuf.save(); } // fbs.sync(); System.out.println("write time: " + (System.currentTimeMillis() - start) / 1000); start = System.currentTimeMillis(); for (int j = 0; j < TOTAL; j++) { final ByteBuffer buf = fbs.get(j); if (buf == null) { System.out.println("Error trying read block " + j + " blocks=" + fbs.numBlocks()); break; } StringSerializer.fromBufferToString(buf); buf.getLong(); //System.out.print("\t" + hehe + "\t" + offset); } System.out.println("read time: " + (System.currentTimeMillis() - start) / 1000); fbs.close(); }
From source file:Main.java
public static long bytearray2long(byte[] b) { //TODO optimize with creating a new ByteBuffer object ByteBuffer buf = ByteBuffer.wrap(b, 0, 8); return buf.getLong(); }
From source file:Main.java
public static UUID getUuidFromByteArrayBigEndian(byte[] bytes) { ByteBuffer bb = ByteBuffer.wrap(bytes); long high = bb.getLong(); long low = bb.getLong(); UUID uuid = new UUID(high, low); return uuid;/*from ww w .java 2 s. c o m*/ }
From source file:Main.java
public static UUID toUUID(byte[] bytes) { ByteBuffer bb = ByteBuffer.wrap(bytes); long firstLong = bb.getLong(); long secondLong = bb.getLong(); return new UUID(firstLong, secondLong); }
From source file:Main.java
/** * Converts the given byte array, which is expected to contain the UUID, into a UUID instance. * * @param byteArray The byte array containing the UUID. * @return A newly created UUID instance or null in case of a failure. *//*from w w w . ja va 2 s .c om*/ public static UUID byteArrayToUuid(byte[] byteArray) { if (byteArray != null && byteArray.length >= UUID_LENGTH_IN_BYTES) { ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); long mostSignificantBits = byteBuffer.getLong(); long leastSignificantBits = byteBuffer.getLong(); return new UUID(mostSignificantBits, leastSignificantBits); } return null; }
From source file:Main.java
public static String bytesToUuid(byte[] bytes) { ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); long lsb = bb.getLong(); long msb = bb.getLong(); UUID uuid = new UUID(msb, lsb); return uuidToString(uuid); }
From source file:com.hengyi.japp.tools.UuidUtils.java
public static String decodeBase64Uuid(String compressedUuid) { byte[] byUuid = Base64.decodeBase64(compressedUuid); ByteBuffer bb = ByteBuffer.wrap(byUuid); UUID uuid = new UUID(bb.getLong(), bb.getLong()); return uuid.toString(); }