List of usage examples for java.nio ByteBuffer getInt
public abstract int getInt();
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
/** * This assumes the next position is the size as an int, and the following is a string * iff the size is not zero.// w w w .j a v a 2s .co m * * @param bb ByteBuffer to extract string from * @return string from the next position, or "" if the size is zero */ public static String toString(ByteBuffer bb) { byte[] bytes = new byte[bb.getInt()]; String s = ""; try { if (bytes.length > 0) { bb.get(bytes); s = new String(bytes, UTF8); } } catch (Exception e) { throw new RuntimeException("ByteBuffer not in expected format," + " failed to parse string bytes", e); } return s; }
From source file:alluxio.util.FormatUtils.java
/** * Parses a {@link ByteBuffer} into a {@link String}. In particular, the function prints the * content of the buffer in 4-byte increments as space separated integers. * * @param buf buffer to use/*from w w w . j ava 2 s. c o m*/ * @return the String representation of the {@link ByteBuffer} */ public static String byteBufferToString(ByteBuffer buf) { StringBuilder sb = new StringBuilder(); for (int k = 0; k < buf.limit() / 4; k++) { if (k != 0) { sb.append(" "); } sb.append(buf.getInt()); } return sb.toString(); }
From source file:com.openteach.diamond.network.waverider.command.Command.java
/** * ByteBuffer??/*from w w w . jav a 2 s.c o m*/ * @param buffer * @return */ public static Command unmarshall(ByteBuffer buffer) { if (buffer.remaining() < getHeaderSize()) { throw new RuntimeException("Wrong command."); } Command command = new Command(); command.setType(buffer.getLong()); command.setLength(buffer.getInt()); // (payLoad)array() command.setPayLoad(buffer.slice()); return command; }
From source file:edu.umass.cs.gigapaxos.paxosutil.PaxosPacketDemultiplexerFast.java
private static PaxosPacket toPaxosPacket(byte[] bytes) throws UnsupportedEncodingException, UnknownHostException { assert (bytes != null); ByteBuffer bbuf = ByteBuffer.wrap(bytes); PaxosPacket.PaxosPacketType type = bbuf.getInt() == PaxosPacketType.PAXOS_PACKET.getInt() ? PaxosPacketType.getPaxosPacketType(bbuf.getInt()) : null;/* ww w . j a v a 2 s. c om*/ if (type == null) fatal(bytes); // bbuf = ByteBuffer.wrap(bytes); bbuf.rewind(); PaxosPacket paxosPacket = null; switch (type) { case REQUEST: // log.info("before new RequestPacket(ByteBuffer)"); paxosPacket = new RequestPacket(bbuf); // log.info("after new RequestPacket(ByteBuffer)"); break; case ACCEPT: paxosPacket = new AcceptPacket(bbuf); break; case BATCHED_COMMIT: paxosPacket = new BatchedCommit(bbuf); break; case BATCHED_ACCEPT_REPLY: paxosPacket = new BatchedAcceptReply(bbuf); break; default: assert (false); } return paxosPacket; }
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
public static Constraint getConstraint(ByteBuffer bb) { Constraint.Type type = Constraint.Type.values()[bb.getInt()]; String p = toString(bb);//from w ww . j a va 2 s. co m switch (type) { case NULL: return null; case LIKE: if (!StringUtils.isEmpty(p)) { return gson.fromJson(p, LikeConstraint.class); } } return null; }
From source file:com.nridge.core.base.std.BufUtl.java
/** * Retrieves the operation code stored within the header of the * <code>ByteBuffer</code> object. * * @param aBuffer Packet byte buffer object. * @return Application specific operation code value. *//*from www .j av a 2 s .c o m*/ public static int getOpCode(ByteBuffer aBuffer) { int opCode; if (aBuffer == null) return -1; else { aBuffer.mark(); aBuffer.position(0); opCode = aBuffer.getInt(); aBuffer.reset(); return opCode; } }
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
/** * Assumes the entire ByteBuffer is a TaskID. * * @param bb// www . j av a 2s .co m * @return Protos.TaskID */ public static Protos.TaskID toTaskId(ByteBuffer bb) { try { return Protos.TaskID.parseFrom(getBytes(bb, bb.getInt())); } catch (Exception e) { throw new RuntimeException("Failed to parse Task ID", e); } }
From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinUtil.java
/** * Reads a size from a reversed byte order, such as block size in the block header * * @param byteSize byte array with a length of exactly 4 * * @return size, returns 0 in case of invalid block size * *//*from w w w. j a v a 2 s . com*/ public static long getSize(byte[] byteSize) { if (byteSize.length != 4) return 0; ByteBuffer converterBuffer = ByteBuffer.wrap(byteSize); converterBuffer.order(ByteOrder.LITTLE_ENDIAN); return convertSignedIntToUnsigned(converterBuffer.getInt()); }
From source file:org.apache.myriad.state.utils.ByteBufferSupport.java
/** * Assumes the entire ByteBuffer is a FrameworkID. * * @param bb//from w ww .ja v a 2 s . co m * @return Protos.FrameworkID */ public static Protos.FrameworkID toFrameworkID(ByteBuffer bb) { try { return Protos.FrameworkID.parseFrom(getBytes(bb, bb.getInt())); } catch (Exception e) { throw new RuntimeException("Failed to parse Framework ID", e); } }
From source file:com.nridge.core.base.std.BufUtl.java
/** * Retrieves the operation version code stored within the header of the * <code>ByteBuffer</code> object. * * @param aBuffer Packet byte buffer object. * @return Application specific operation code version value. *//*from www . j a v a2 s . com*/ @SuppressWarnings({ "UnusedAssignment" }) public static int getVersion(ByteBuffer aBuffer) { int opCode, versionId; if (aBuffer == null) return -1; else { aBuffer.mark(); aBuffer.position(0); opCode = aBuffer.getInt(); versionId = aBuffer.getInt(); aBuffer.reset(); return versionId; } }