Java Code Examples for java.nio.ByteBuffer#getInt()

The following examples show how to use java.nio.ByteBuffer#getInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageFramerTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void dontCompressIfNotRequested() {
  allocator = new BytesWritableBufferAllocator(100, Integer.MAX_VALUE);
  framer = new MessageFramer(sink, allocator, statsTraceCtx)
      .setCompressor(new Codec.Gzip())
      .setMessageCompression(false);
  writeKnownLength(framer, new byte[1000]);
  framer.flush();
  // The GRPC header is written first as a separate frame
  verify(sink).deliverFrame(frameCaptor.capture(), eq(false), eq(true), eq(1));

  // Check the header
  ByteWritableBuffer buffer = frameCaptor.getAllValues().get(0);
  // We purposefully don't check the last byte of length, since that depends on how exactly it
  // compressed.

  assertEquals(0x0, buffer.data[0]);
  ByteBuffer byteBuf = ByteBuffer.wrap(buffer.data, 1, 4);
  byteBuf.order(ByteOrder.BIG_ENDIAN);
  int length = byteBuf.getInt();
  assertEquals(1000, length);

  assertEquals(buffer.data.length - 5 , length);
  checkStats(1000, 1000);
}
 
Example 2
Source File: StringRangeBlockIndex.java    From yosegi with Apache License 2.0 5 votes vote down vote up
@Override
public void setFromBinary( final byte[] buffer , final int start , final int length ) {
  ByteBuffer wrapBuffer = ByteBuffer.wrap( buffer );
  wrapBuffer.position( start );
  int minSize = wrapBuffer.getInt();
  int maxSize = wrapBuffer.getInt();

  char[] minChars = new char[minSize];
  char[] maxChars = new char[maxSize];
  CharBuffer viewCharBuffer = wrapBuffer.asCharBuffer();
  viewCharBuffer.get( minChars );
  viewCharBuffer.get( maxChars );
  min = new String( minChars );
  max = new String( maxChars );
}
 
Example 3
Source File: MessageHeader.java    From usb4java-javax-examples with The Unlicense 5 votes vote down vote up
/**
 * Constructs a new ADB message header from the specified byte array.
 * 
 * @param bytes
 *            The ADB message header as bytes.
 */
public MessageHeader(byte[] bytes)
{
    if (bytes.length != SIZE)
        throw new IllegalArgumentException("ADB message header must be "
            + SIZE + " bytes large, not " + bytes.length + " bytes");
    ByteBuffer buffer = ByteBuffer.wrap(bytes).
        order(ByteOrder.LITTLE_ENDIAN);
    this.command = buffer.getInt();
    this.arg0 = buffer.getInt();
    this.arg1 = buffer.getInt();
    this.dataLength = buffer.getInt();
    this.dataChecksum = buffer.getInt();
    this.magic = buffer.getInt();
}
 
Example 4
Source File: XmlStartElementChunk.java    From android-arscblamer with Apache License 2.0 5 votes vote down vote up
protected XmlStartElementChunk(ByteBuffer buffer, @Nullable Chunk parent) {
  super(buffer, parent);
  namespace = buffer.getInt();
  name = buffer.getInt();
  attributeStart = (buffer.getShort() & 0xFFFF);
  int attributeSize = (buffer.getShort() & 0xFFFF);
  Preconditions.checkState(attributeSize == XmlAttribute.SIZE,
      "attributeSize is wrong size. Got %s, want %s", attributeSize, XmlAttribute.SIZE);
  attributeCount = (buffer.getShort() & 0xFFFF);

  // The following indices are 1-based and need to be adjusted.
  idIndex = (buffer.getShort() & 0xFFFF) - 1;
  classIndex = (buffer.getShort() & 0xFFFF) - 1;
  styleIndex = (buffer.getShort() & 0xFFFF) - 1;
}
 
Example 5
Source File: RocketMQSerializable.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 5 votes vote down vote up
public static RemotingCommand rocketMQProtocolDecode(final byte[] headerArray) {
    RemotingCommand cmd = new RemotingCommand();
    ByteBuffer headerBuffer = ByteBuffer.wrap(headerArray);
    // int code(~32767)
    cmd.setCode(headerBuffer.getShort());
    // LanguageCode language
    cmd.setLanguage(LanguageCode.valueOf(headerBuffer.get()));
    // int version(~32767)
    cmd.setVersion(headerBuffer.getShort());
    // int opaque
    cmd.setOpaque(headerBuffer.getInt());
    // int flag
    cmd.setFlag(headerBuffer.getInt());
    // String remark
    int remarkLength = headerBuffer.getInt();
    if (remarkLength > 0) {
        byte[] remarkContent = new byte[remarkLength];
        headerBuffer.get(remarkContent);
        cmd.setRemark(new String(remarkContent, CHARSET_UTF8));
    }

    // HashMap<String, String> extFields
    int extFieldsLength = headerBuffer.getInt();
    if (extFieldsLength > 0) {
        byte[] extFieldsBytes = new byte[extFieldsLength];
        headerBuffer.get(extFieldsBytes);
        cmd.setExtFields(mapDeserialize(extFieldsBytes));
    }
    return cmd;
}
 
Example 6
Source File: DataDecoder.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void parse(ByteBuffer b, Data data)
{
    int size = b.getInt();
    ByteBuffer buf = b.slice();
    buf.limit(size);
    b.position(b.position()+size);
    int count = buf.getInt();
    data.putMap();
    parseChildren(data, buf, count);
}
 
Example 7
Source File: ceiling_t.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void unpack(ByteBuffer b) throws IOException {
    b.order(ByteOrder.LITTLE_ENDIAN);
    super.unpack(b); // Call thinker reader first
    type = values[b.getInt()];
    super.sectorid = b.getInt(); // sector pointer.
    bottomheight = b.getInt();
    topheight = b.getInt();
    speed = b.getInt();
    crush = (b.getInt() != 0);
    direction = b.getInt();
    tag = b.getInt();
    olddirection = b.getInt();
}
 
Example 8
Source File: PCAPFileReader.java    From JavaLinuxNet with Apache License 2.0 5 votes vote down vote up
/**
 * Read IP Packet or return NULL in case of
 *  - end of file
 *  - packet to small to retrieve IP header
 *  - unsupported datalink type , could not find ip header location
 * @return
 * @throws IOException
 */
public IpPacket readPacket() throws IOException {
    ByteBuffer packetHeader=ByteBuffer.allocate(PCAP_PACKETHEADER_SIZE);
    try {
        inputFile.read(packetHeader.array());
        packetHeader = packetHeader.order(fileFormat);
        //get length of packet.
        int captureLength = packetHeader.getInt(8);
        logger.debug("packet Size: {}", captureLength);
        if (captureLength < dataLinkHeaderSize + 20) {  //we need atleast ip header + some extra data
            if (captureLength!=0) { //size=0 means end of stream
                logger.warn("captured packet to small: {}", captureLength);
            }
            return null;
        }

        ByteBuffer rawPacket = ByteBuffer.allocate(captureLength);
        inputFile.read(rawPacket.array());
        rawPacket = rawPacket.order(fileFormat);
        //remove to ip header
        rawPacket.position(dataLinkHeaderSize);
        IpPacket packet = IPFactory.processRawPacket(rawPacket.slice(), captureLength - dataLinkHeaderSize);
        return packet;
    }finally {
        packetHeader=null;
    }
}
 
Example 9
Source File: PlainAccessValidatorTest.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
@Test
public void validateQueryMessageByKeyTest() {
    QueryMessageRequestHeader queryMessageRequestHeader=new QueryMessageRequestHeader();
    queryMessageRequestHeader.setTopic("topicC");
    RemotingCommand remotingCommand = RemotingCommand.createRequestCommand(RequestCode.QUERY_MESSAGE,queryMessageRequestHeader);
    aclClient.doBeforeRequest("", remotingCommand);
    remotingCommand.addExtField(MixAll.UNIQUE_MSG_QUERY_FLAG, "false");
    ByteBuffer buf = remotingCommand.encodeHeader();
    buf.getInt();
    buf = ByteBuffer.allocate(buf.limit() - buf.position()).put(buf);
    buf.position(0);
    PlainAccessResource accessResource = (PlainAccessResource) plainAccessValidator.parse(RemotingCommand.decode(buf), "192.168.1.1:9876");
    plainAccessValidator.validate(accessResource);
}
 
Example 10
Source File: QueryClassificationsAndTypesStackFrame.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public ObjectIdentifier getOid(EClass eClass, EAttribute attribute, Object value, DatabaseInterface databaseInterface, int pid, int rid) throws BimserverDatabaseException {
	if (attribute.getEAnnotation("singleindex") != null) {
		String indexTableName = attribute.getEContainingClass().getEPackage().getName() + "_" + eClass.getName() + "_" + attribute.getName();
		byte[] queryBytes = null;
		if (value instanceof String) {
			queryBytes = ((String)value).getBytes(Charsets.UTF_8);
		} else if (value instanceof Integer) {
			queryBytes = BinUtils.intToByteArray((Integer)value);
		} else {
			throw new BimserverDatabaseException("Unsupported type " + value);
		}
		ByteBuffer valueBuffer = ByteBuffer.allocate(queryBytes.length + 8);
		valueBuffer.putInt(pid);
		valueBuffer.putInt(-rid);
		valueBuffer.put(queryBytes);
		byte[] firstDuplicate = databaseInterface.get(indexTableName, valueBuffer.array());
		if (firstDuplicate != null) {
			ByteBuffer buffer = ByteBuffer.wrap(firstDuplicate);
			buffer.getInt(); // pid
			long oid = buffer.getLong();
			
			return new ObjectIdentifier(oid, (short)oid);
		}
	} else {
		throw new UnsupportedOperationException(eClass.getName() + "." + attribute.getName() + " does not have a singleindex");
	}
	return null;
}
 
Example 11
Source File: RpcRequestMessage.java    From incubator-crail with Apache License 2.0 5 votes vote down vote up
public void update(ByteBuffer buffer) {
	try {
		fileInfo.update(buffer);
		int tmp = buffer.getInt();
		close = (tmp == 1) ? true : false;
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
}
 
Example 12
Source File: QueryOidsAndTypesStackFrame.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean process() throws BimserverDatabaseException, QueryException {
	if (typeRecordIterator == null) {
		return true;
	}
	if (record == null) {
		currentObject = null;
		typeRecordIterator.close();
		return true;
	}

	currentObject = null;
	
	ByteBuffer nextKeyStart = ByteBuffer.allocate(12);
	getQueryObjectProvider().incReads();
	ByteBuffer keyBuffer = ByteBuffer.wrap(record.getKey());
	int keyPid = keyBuffer.getInt();
	long keyOid = keyBuffer.getLong();
	int keyRid = -keyBuffer.getInt();
	ByteBuffer valueBuffer = ByteBuffer.wrap(record.getValue());
	GetResult map = getMap(eClass, eClass, valueBuffer, keyPid, keyOid, keyRid);
	if (map == GetResult.CONTINUE_WITH_NEXT_OID) {
		if (oidIterator.hasNext()) {
			nextKeyStart.position(0);
			nextKeyStart.putInt(getReusable().getPid());
			nextKeyStart.putLong(oidIterator.next());
			record = typeRecordIterator.next(nextKeyStart.array());
		} else {
			record = null;
		}
	} else {
		record = typeRecordIterator.next();
	}

	processPossibleIncludes(currentObject, eClass, getQueryPart());
	
	if (record == null) {
		typeRecordIterator.close();
	}
	return record == null;
}
 
Example 13
Source File: Vec4ui.java    From glm with MIT License 4 votes vote down vote up
public Vec4ui(ByteBuffer bb, int offset) {
    this(bb.getInt(offset + 0 * Integer.BYTES), bb.getInt(offset + 1 * Integer.BYTES),
            bb.getInt(offset + 2 * Integer.BYTES), bb.getInt(offset + 3 * Integer.BYTES));
}
 
Example 14
Source File: VendorSpecificInformationOption.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
public int getEnterpriseNumber() {
    ByteBuffer buf = ByteBuffer.wrap(getData());
    return buf.getInt(0);
}
 
Example 15
Source File: ChronicleMapBuilder.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
/**
 * @return size of the self bootstrapping header
 */
private int waitUntilReady(RandomAccessFile raf, File file, boolean recover)
        throws IOException {
    FileChannel fileChannel = raf.getChannel();

    ByteBuffer sizeWordBuffer = ByteBuffer.allocate(4);
    sizeWordBuffer.order(LITTLE_ENDIAN);

    // 60 * 10, 100 ms wait = 1 minute total wait
    int attempts = 60 * 10;
    int lastReadHeaderSize = -1;
    for (int attempt = 0; attempt < attempts; attempt++) {
        if (raf.length() >= SELF_BOOTSTRAPPING_HEADER_OFFSET) {

            sizeWordBuffer.clear();
            readFully(fileChannel, SIZE_WORD_OFFSET, sizeWordBuffer);
            if (sizeWordBuffer.remaining() == 0) {
                int sizeWord = sizeWordBuffer.getInt(0);
                lastReadHeaderSize = SizePrefixedBlob.extractSize(sizeWord);
                if (SizePrefixedBlob.isReady(sizeWord))
                    return lastReadHeaderSize;
            }
            // The only possible reason why not 4 bytes are read, is that the file is
            // truncated between length() and read() calls, then continue to wait
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            if (recover) {
                if (lastReadHeaderSize == -1) {
                    throw new ChronicleHashRecoveryFailedException(e);
                } else {
                    return lastReadHeaderSize;
                }
            } else {
                throw new IOException(e);
            }
        }
    }
    if (recover) {
        if (lastReadHeaderSize == -1) {
            throw new ChronicleHashRecoveryFailedException(
                    "File header is not recoverable, file=" + file);
        } else {
            return lastReadHeaderSize;
        }
    } else {
        throw new IOException("Unable to wait until the file=" + file +
                " is ready, likely the process which created the file crashed or hung " +
                "for more than 1 minute");
    }
}
 
Example 16
Source File: RowStoreUtil.java    From tajo with Apache License 2.0 4 votes vote down vote up
public Tuple toTuple(byte[] bytes) {
  nullFlags.clear();
  ByteBuffer bb = ByteBuffer.wrap(bytes);
  Tuple tuple = new VTuple(schema.size());
  Column col;
  TajoDataTypes.DataType type;

  bb.limit(headerSize);
  nullFlags.fromByteBuffer(bb);
  bb.limit(bytes.length);

  for (int i = 0; i < schema.size(); i++) {
    if (nullFlags.get(i)) {
      tuple.put(i, DatumFactory.createNullDatum());
      continue;
    }

    col = schema.getColumn(i);
    type = col.getDataType();
    switch (type.getType()) {
    case BOOLEAN:
      tuple.put(i, DatumFactory.createBool(bb.get()));
      break;
    case BIT:
      byte b = bb.get();
      tuple.put(i, DatumFactory.createBit(b));
      break;

    case CHAR:
      byte[] _str = new byte[type.getLength()];
      bb.get(_str);
      tuple.put(i, DatumFactory.createChar(_str));
      break;

    case INT2:
      short s = bb.getShort();
      tuple.put(i, DatumFactory.createInt2(s));
      break;

    case INT4:
    case DATE:
      int i_ = bb.getInt();
      tuple.put(i, DatumFactory.createFromInt4(type, i_));
      break;

    case INT8:
    case TIME:
    case TIMESTAMP:
      long l = bb.getLong();
      tuple.put(i, DatumFactory.createFromInt8(type, l));
      break;

    case INTERVAL:
      int month = bb.getInt();
      long milliseconds = bb.getLong();
      tuple.put(i, new IntervalDatum(month, milliseconds));
      break;

    case FLOAT4:
      float f = bb.getFloat();
      tuple.put(i, DatumFactory.createFloat4(f));
      break;

    case FLOAT8:
      double d = bb.getDouble();
      tuple.put(i, DatumFactory.createFloat8(d));
      break;

    case TEXT:
      byte[] _string = new byte[bb.getInt()];
      bb.get(_string);
      tuple.put(i, DatumFactory.createText(_string));
      break;

    case BLOB:
      byte[] _bytes = new byte[bb.getInt()];
      bb.get(_bytes);
      tuple.put(i, DatumFactory.createBlob(_bytes));
      break;

    default:
      throw new TajoRuntimeException(
          new UnsupportedException("data type '" + col.getDataType().getType().name() + "'"));
    }
  }
  return tuple;
}
 
Example 17
Source File: ReadClient.java    From disni with Apache License 2.0 4 votes vote down vote up
private void run() throws Exception {
	System.out.println("ReadClient, size " + size + ", loop " + loop);

	ReadClient.ReadClientEndpoint endpoint = group.createEndpoint();
		InetAddress ipAddress = InetAddress.getByName(host);
		InetSocketAddress address = new InetSocketAddress(ipAddress, port);		
	endpoint.connect(address, 1000);
	System.out.println("ReadClient, client connected, address " + host + ", port " + port);

	//in our custom endpoints we make sure CQ events get stored in a queue, we now query that queue for new CQ events.
	//in this case a new CQ event means we have received some data, i.e., a message from the server
	endpoint.pollUntil();
	ByteBuffer recvBuf = endpoint.getRecvBuf();
	//the message has been received in this buffer
	//it contains some RDMA information sent by the server
	recvBuf.clear();
	long addr = recvBuf.getLong();
	int length = recvBuf.getInt();
	int lkey = recvBuf.getInt();
	recvBuf.clear();
	System.out.println("ReadClient, receiving rdma information, addr " + addr + ", length " + length + ", key " + lkey);
	System.out.println("ReadClient, preparing read operation...");

	//the RDMA information above identifies a RDMA buffer at the server side
	//let's issue a one-sided RDMA read opeation to fetch the content from that buffer
	IbvSendWR sendWR = endpoint.getSendWR();
	sendWR.setWr_id(1001);
	sendWR.setOpcode(IbvSendWR.IBV_WR_RDMA_READ);
	sendWR.setSend_flags(IbvSendWR.IBV_SEND_SIGNALED);
	sendWR.getRdma().setRemote_addr(addr);
	sendWR.getRdma().setRkey(lkey);

	//post the operation on the endpoint
	SVCPostSend postSend = endpoint.newPostSend();
	long start = System.nanoTime();
	for (int i = 0; i < loop; i++){
		postSend.execute();
		endpoint.pollUntil();

	}
	long end = System.nanoTime();
	long duration = end - start;
	long latency = duration / loop / 1000;


	//let's prepare a final message to signal everything went fine
	sendWR.setWr_id(1002);
	sendWR.setOpcode(IbvSendWR.IBV_WR_SEND);
	sendWR.setSend_flags(IbvSendWR.IBV_SEND_SIGNALED);
	sendWR.getRdma().setRemote_addr(addr);
	sendWR.getRdma().setRkey(lkey);

	//post that operation
	endpoint.newPostSend().execute().free();
	endpoint.pollUntil();

	//close everything
	endpoint.close();
	group.close();

	System.out.println("ReadClient, latency " + latency);
}
 
Example 18
Source File: XmlNamespaceChunk.java    From android-chunk-utils with Apache License 2.0 4 votes vote down vote up
protected XmlNamespaceChunk(ByteBuffer buffer, @Nullable Chunk parent) {
  super(buffer, parent);
  prefix = buffer.getInt();
  uri = buffer.getInt();
}
 
Example 19
Source File: UDPConnectionSet.java    From TorrentEngine with GNU General Public License v3.0 2 votes vote down vote up
protected void
receiveDataCommand(
	int				sequence,
	ByteBuffer		buffer,
	int				header_length )

	throws IOException
{
	int	connection_id = buffer.getInt();

	UDPConnection	connection 		= null;
	boolean			new_connection 	= false;

	synchronized( connections ){

		if ( failed ){

			throw( new IOException( "Connection set has failed" ));
		}

		connection = (UDPConnection)connections.get( new Integer( connection_id ));

		if ( connection == null ){

			connection = (UDPConnection)connections.remove( new Integer( -1 ));

			if ( connection != null ){

				connection.setID( connection_id );

				connections.put( new Integer( connection_id ), connection );
			}
		}

		if ( connection == null ){

			if ( connections.size() == 128 ){

				throw( new IOException( "Connection limit reached" ));
			}

			connection	= new UDPConnection( this, connection_id );

			connections.put( new Integer( connection.getID()), connection );

			new_connection	= true;
		}
	}

	buffer.position( header_length );

	if ( new_connection ){

		manager.accept( local_port, remote_address, connection );
	}

	if ( manager.trace() ){
		trace( connection, "receiveData: seq=" + sequence + ",data="+ buffer.remaining());
	}

	connection.receive( buffer );
}
 
Example 20
Source File: Utils.java    From luxun with Apache License 2.0 2 votes vote down vote up
/**
 * Read an unsigned integer from the current position in the buffer,
 * incrementing the position by 4 bytes
 * 
 * @param buffer The buffer to read from
 * @return The integer read, as a long to avoid signedness
 */
public static long getUnsignedInt(ByteBuffer buffer) {
    return buffer.getInt() & 0xffffffffL;
}