List of usage examples for java.lang Long rotateRight
public static long rotateRight(long i, int distance)
From source file:Main.java
public static void main(String[] args) { long n = 64; n = Long.rotateRight(n, 4); System.out.println(n); }
From source file:org.apache.zookeeper.graph.JsonGenerator.java
/** Assumes entries are sorted by timestamp. *//* w ww . j ava 2 s .c o m*/ public JsonGenerator(LogIterator iter) { servers = new HashSet<Integer>(); Pattern stateChangeP = Pattern.compile("- (LOOKING|FOLLOWING|LEADING)"); Pattern newElectionP = Pattern.compile("New election. My id = (\\d+), Proposed zxid = (\\d+)"); Pattern receivedProposalP = Pattern.compile( "Notification: (\\d+) \\(n.leader\\), (\\d+) \\(n.zxid\\), (\\d+) \\(n.round\\), .+ \\(n.state\\), (\\d+) \\(n.sid\\), .+ \\(my state\\)"); Pattern exceptionP = Pattern.compile("xception"); root = new JSONObject(); Matcher m = null; JSONArray events = new JSONArray(); root.put("events", events); long starttime = Long.MAX_VALUE; long endtime = 0; int leader = 0; long curEpoch = 0; boolean newEpoch = false; while (iter.hasNext()) { LogEntry ent = iter.next(); if (ent.getTimestamp() < starttime) { starttime = ent.getTimestamp(); } if (ent.getTimestamp() > endtime) { endtime = ent.getTimestamp(); } if (ent.getType() == LogEntry.Type.TXN) { events.add(txnEntry((TransactionEntry) ent)); } else { Log4JEntry e = (Log4JEntry) ent; servers.add(e.getNode()); if ((m = stateChangeP.matcher(e.getEntry())).find()) { JSONObject stateChange = new JSONObject(); stateChange.put("type", "stateChange"); stateChange.put("time", e.getTimestamp()); stateChange.put("server", e.getNode()); stateChange.put("state", m.group(1)); events.add(stateChange); if (m.group(1).equals("LEADING")) { leader = e.getNode(); } } else if ((m = newElectionP.matcher(e.getEntry())).find()) { Iterator<Integer> iterator = servers.iterator(); long zxid = Long.valueOf(m.group(2)); int count = (int) zxid;// & 0xFFFFFFFFL; int epoch = (int) Long.rotateRight(zxid, 32);// >> 32; if (leader != 0 && epoch > curEpoch) { JSONObject stateChange = new JSONObject(); stateChange.put("type", "stateChange"); stateChange.put("time", e.getTimestamp()); stateChange.put("server", leader); stateChange.put("state", "INIT"); events.add(stateChange); leader = 0; } while (iterator.hasNext()) { int dst = iterator.next(); if (dst != e.getNode()) { JSONObject msg = new JSONObject(); msg.put("type", "postmessage"); msg.put("src", e.getNode()); msg.put("dst", dst); msg.put("time", e.getTimestamp()); msg.put("zxid", m.group(2)); msg.put("count", count); msg.put("epoch", epoch); events.add(msg); } } } else if ((m = receivedProposalP.matcher(e.getEntry())).find()) { // Pattern.compile("Notification: \\d+, (\\d+), (\\d+), \\d+, [^,]*, [^,]*, (\\d+)");//, LOOKING, LOOKING, 2 int src = Integer.valueOf(m.group(4)); long zxid = Long.valueOf(m.group(2)); int dst = e.getNode(); long epoch2 = Long.valueOf(m.group(3)); int count = (int) zxid;// & 0xFFFFFFFFL; int epoch = (int) Long.rotateRight(zxid, 32);// >> 32; if (leader != 0 && epoch > curEpoch) { JSONObject stateChange = new JSONObject(); stateChange.put("type", "stateChange"); stateChange.put("time", e.getTimestamp()); stateChange.put("server", leader); stateChange.put("state", "INIT"); events.add(stateChange); leader = 0; } if (src != dst) { JSONObject msg = new JSONObject(); msg.put("type", "delivermessage"); msg.put("src", src); msg.put("dst", dst); msg.put("time", e.getTimestamp()); msg.put("zxid", zxid); msg.put("epoch", epoch); msg.put("count", count); msg.put("epoch2", epoch2); events.add(msg); } } else if ((m = exceptionP.matcher(e.getEntry())).find()) { JSONObject ex = new JSONObject(); ex.put("type", "exception"); ex.put("server", e.getNode()); ex.put("time", e.getTimestamp()); ex.put("text", e.getEntry()); events.add(ex); } } JSONObject ex = new JSONObject(); ex.put("type", "text"); ex.put("time", ent.getTimestamp()); String txt = ent.toString(); ex.put("text", txt); events.add(ex); } // System.out.println("pending messages: "+pendingMessages.size()); root.put("starttime", starttime); root.put("endtime", endtime); JSONArray serversarray = new JSONArray(); root.put("servers", serversarray); Iterator<Integer> iterator = servers.iterator(); while (iterator.hasNext()) { serversarray.add(iterator.next()); } }
From source file:record.wave.WaveWriter.java
/** * Creates a little-endian 4-byte buffer containing an unsigned 32-bit * integer value derived from the 4 least significant bytes of the argument. * /*from ww w. j av a 2 s . c o m*/ * The buffer's position is set to 0 to prepare it for writing to a channel. */ protected static ByteBuffer getUnsignedIntegerBuffer(long size) { ByteBuffer buffer = ByteBuffer.allocate(4); buffer.put((byte) (size & 0xFFl)); buffer.put((byte) (Long.rotateRight(size & 0xFF00l, 8))); buffer.put((byte) (Long.rotateRight(size & 0xFF0000l, 16))); /* This side-steps an issue with right shifting a signed long by 32 * where it produces an error value. Instead, we right shift in two steps. */ buffer.put((byte) Long.rotateRight(Long.rotateRight(size & 0xFF000000l, 16), 8)); buffer.position(0); return buffer; }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Creates a little-endian 4-byte buffer containing an unsigned 32-bit * integer value derived from the 4 least significant bytes of the argument. * * The buffer's position is set to 0 to prepare it for writing to a channel. */// w w w . ja v a2 s . c om protected static ByteBuffer getUnsignedIntegerBuffer(long size) { ByteBuffer buffer = ByteBuffer.allocate(4); buffer.put((byte) (size & 0xFFl)); buffer.put((byte) (Long.rotateRight(size & 0xFF00l, 8))); buffer.put((byte) (Long.rotateRight(size & 0xFF0000l, 16))); /* This side-steps an issue with right shifting a signed long by 32 * where it produces an error value. Instead, we right shift in two steps. */ buffer.put((byte) Long.rotateRight(Long.rotateRight(size & 0xFF000000l, 16), 8)); buffer.position(0); return buffer; }