Package java.nio

Examples of java.nio.ByteBuffer


        public void run() {
            this.setName("ReceiverThread");
            Message msg;
            Object o;
            ByteBuffer buf;
            TotOrderRequest req;
            while(running) {
                try {
                    o=channel.receive(0);
                    if(o instanceof Message) {
View Full Code Here


//        y=in.readInt();
//        val=in.readInt();
//    }

    public byte[] toBuffer() {
        ByteBuffer buf=ByteBuffer.allocate(SIZE);
        buf.put(type);
        buf.putInt(x);
        buf.putInt(y);
        buf.putInt(val);
        return buf.array();
    }
View Full Code Here

  @Override
  public void handleWrite(SelectionKey key) {
    logger.debug("handle write...");
    DynamicByteBuffer dbb = (DynamicByteBuffer) key.attachment();
    logger.debug("pending data about to be written");
    ByteBuffer toSend = dbb.getByteBuffer();
    SocketChannel channel = ((SocketChannel) key.channel());
    try {
      toSend.flip()// prepare for write
      long bytesWritten = channel.write(toSend);
      if (IOLoop.INSTANCE.hasKeepAliveTimeout(channel)) {
        prolongKeepAliveTimeout(channel);
      }
      logger.debug("sent {} bytes to wire", bytesWritten);
      if (!toSend.hasRemaining()) {
        logger.debug("sent all data in toSend buffer");
        closeOrRegisterForRead(key)// should probably only be done if the HttpResponse is finished
      } else {
        toSend.compact()// make room for more data be "read" in
      }
    } catch (IOException e) {
      logger.error("Failed to send data to client: {}", e.getMessage());
      Closeables.closeQuietly(channel);
    }
View Full Code Here

   * Clears the buffer (prepares for reuse) attached to the given SelectionKey.
   * @return A cleared (position=0, limit=capacity) ByteBuffer which is ready for new reads
   */
  private ByteBuffer reuseAttachment(SelectionKey key) {
    Object o = key.attachment();
    ByteBuffer attachment = null;
    if (o instanceof DynamicByteBuffer) {
      attachment = ((DynamicByteBuffer)o).getByteBuffer();
    } else {
      attachment = (ByteBuffer) o;
    }
    if (attachment.capacity() < READ_BUFFER_SIZE) {
      attachment = ByteBuffer.allocate(READ_BUFFER_SIZE);
    }
    attachment.clear()// prepare for reuse
    return attachment;
  }
View Full Code Here

    return attachment;
  }


  private HttpRequest getHttpRequest(SelectionKey key, SocketChannel clientChannel) {
    ByteBuffer buffer = (ByteBuffer) key.attachment();
    try {
      clientChannel.read(buffer);
    } catch (IOException e) {
      logger.warn("Could not read buffer: {}", e.getMessage());
      Closeables.closeQuietly(clientChannel);
    }
    buffer.flip();
   
    return doGetHttpRequest(key, clientChannel, buffer);
  }
View Full Code Here

   * Should only be invoked by the IOLoop
   */
  @Override
  public void handleRead(SelectionKey key) throws IOException {
    logger.debug("handle read...");
    ByteBuffer buffer = ByteBuffer.allocate(DEFAULT_BYTEBUFFER_SIZE);
    int read = ((SocketChannel) key.channel()).read(buffer);
    if (read == -1) {  // EOF
      reachedEOF = true;
      IOLoop.INSTANCE.updateHandler(channel, interestOps &= ~SelectionKey.OP_READ);
      return;
    }
    readBuffer.append(new String(buffer.array(), 0, buffer.position(), Charsets.ISO_8859_1));
    logger.debug("readBuffer size: {}", readBuffer.length());
    checkReadState();
  }
View Full Code Here

      }
    }
   
    private void fillPseudoHeader(EncodeContext cxt, byte[] pseudoBlock, int icmpPrtlBytes)
    throws EncodeException {
      ByteBuffer byteBuffer = ByteBuffer.wrap(pseudoBlock);
      //取得上层IPV6协议的头部字段
      IRecordSetValue ipv6Header = (IRecordSetValue)cxt.getFieldStackMap().peekField(
          Ipv6TypeEVExtFactory.HEADER_FIELD);
      if (ipv6Header == null) {
        throw new EncodeException(targetTypeMeta,
            MetaException.CODE.FIELD_NOT_FOUND_IN_STACK_ERROR,
            Ipv6TypeEVExtFactory.HEADER_FIELD);
      }
      IOctetstringValue address;
      //Source Address
      address = (IOctetstringValue)ipv6Header.getField(
          Ipv6TypeEVExtFactory.IPV6_HEADER_SOURCE_ADDRESS);
      assert address != null;
      byteBuffer.put(address.getValue());
      //Destination Address
      address = (IOctetstringValue)ipv6Header.getField(
          Ipv6TypeEVExtFactory.IPV6_HEADER_DESTINATION_ADDRESS);
      assert address != null;
      byteBuffer.put(address.getValue());
      //Upper Layer Packet Length
      byteBuffer.putInt(icmpPrtlBytes);
      //3octet zero
      byteBuffer.position(byteBuffer.position() + 3);
      //Next Header
      byteBuffer.put(ICMP_NEXT_HEADER);
    }
View Full Code Here

   
    private byte[] createV4PseudoHeader(EncodeContext cxt, IContainerValue udpPrtl,
        IRecordSetValue ipv4Header, int upperPrtlBytes, int startOffset)
    throws BitBufferException {
      byte[] pseudoBlock = new byte[V4_PSEUDO_HEADER_BYTES + upperPrtlBytes];
      ByteBuffer pseudoBuffer = ByteBuffer.wrap(pseudoBlock);
      //填充虚头部数据
      IOctetstringValue oStr;
      //ipv4 source address
      oStr = (IOctetstringValue)ipv4Header.getField(
          Ipv4TypeEVExtFactory.IPV4_HEADER_SOURCE_ADDRESS);
      pseudoBuffer.put(oStr.getValue());
      //ipv4 destination address
      oStr = (IOctetstringValue)ipv4Header.getField(
          Ipv4TypeEVExtFactory.IPV4_HEADER_DESTINATION_ADDRESS);
      pseudoBuffer.put(oStr.getValue());
      //zero
      pseudoBuffer.position(pseudoBuffer.position() + 1);
      //ipv4 protocol
      IIntegerValue iValue = (IIntegerValue)ipv4Header.getField(
          Ipv4TypeEVExtFactory.IPV4_HEADER_PROTOCOL);
      pseudoBuffer.put((byte)iValue.getInteger());
      //segment length
      pseudoBuffer.putShort((short)upperPrtlBytes);
      //取得UDP数据并填充
      IReadWritableBitBuffer encodeBuffer = cxt.getBuffer();
      encodeBuffer.position(startOffset);
      encodeBuffer.getByte(pseudoBlock, V4_PSEUDO_HEADER_BYTES,  upperPrtlBytes);
      return pseudoBlock;
View Full Code Here

    private byte[] createV6PseudoHeader(EncodeContext cxt, IContainerValue udpPrtl,
        IRecordSetValue ipv6Header, int upperPrtlBytes, int startOffset)
    throws BitBufferException {
      byte[] pseudoBlock = new byte[V6_PSEUDO_HEADER_BYTES + upperPrtlBytes];
      ByteBuffer pseudoBuffer = ByteBuffer.wrap(pseudoBlock);
      IOctetstringValue address;
      //Source Address
      address = (IOctetstringValue)ipv6Header.getField(
          Ipv6TypeEVExtFactory.IPV6_HEADER_SOURCE_ADDRESS);
      assert address != null;
      pseudoBuffer.put(address.getValue());
      //Destination Address
      address = (IOctetstringValue)ipv6Header.getField(
          Ipv6TypeEVExtFactory.IPV6_HEADER_DESTINATION_ADDRESS);
      assert address != null;
      pseudoBuffer.put(address.getValue());
      //Upper Layer Packet Length
      pseudoBuffer.putInt(upperPrtlBytes);
      //3octet zero
      pseudoBuffer.position(pseudoBuffer.position() + 3);
      //Next Header
      if (isUdp) {
        pseudoBuffer.put(UDP_NEXT_HEADER);
      } else {
        pseudoBuffer.put(TCP_NEXT_HEADER);
      }
      //取得Segment数据并填充
      IReadWritableBitBuffer encodeBuffer = cxt.getBuffer();
      encodeBuffer.position(startOffset);
      encodeBuffer.getByte(pseudoBlock, V6_PSEUDO_HEADER_BYTES,  upperPrtlBytes);
View Full Code Here

    }



    private static byte[] createPayload(int size, int seqno) {
        ByteBuffer buf=ByteBuffer.allocate(size);
        buf.putInt(seqno);
        return buf.array();
    }
View Full Code Here

TOP

Related Classes of java.nio.ByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.