org.littleshoot.mina.common.ByteBuffer.flip()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(126)

本文整理了Java中org.littleshoot.mina.common.ByteBuffer.flip()方法的一些代码示例,展示了ByteBuffer.flip()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.flip()方法的具体详情如下:
包路径:org.littleshoot.mina.common.ByteBuffer
类名称:ByteBuffer
方法名:flip

ByteBuffer.flip介绍

暂无

代码示例

代码示例来源:origin: org.littleshoot/mina-port

public ByteBuffer flip() {
  buf.flip();
  return this;
}

代码示例来源:origin: org.littleshoot/sip-stack

private ByteBuffer createDelimiterBuf(final String delimiter)
  {
  ByteBuffer tmp = ByteBuffer.allocate(delimiter.length());
  try
    {
    tmp.putString(delimiter, charset.newEncoder());
    tmp.flip();
    return tmp;
    }
  catch (CharacterCodingException e)
    {
    LOG.error("Bad charset?", e);
    return null;
    }
  }

代码示例来源:origin: org.littleshoot/mina-port

public void write(ByteBuffer src) {
  synchronized (mutex) {
    if (closed) {
      return;
    }
    if (buf.hasRemaining()) {
      this.buf.compact();
      this.buf.put(src);
      this.buf.flip();
    } else {
      this.buf.clear();
      this.buf.put(src);
      this.buf.flip();
      mutex.notifyAll();
    }
  }
}

代码示例来源:origin: org.littleshoot/mina-util

public void write(final ByteBuffer src)
  {
  m_log.debug("Writing data to input stream...");
  m_rawBytesReceived += src.remaining();
  m_log.debug("Received raw bytes: {}", m_rawBytesReceived);
  synchronized (m_mutex)
    {
    if (m_closed)
      {
      m_log.debug("InputStream closed...");
      return;
      }
    if (m_buf.hasRemaining())
      {
      m_log.debug("Copying buffer data...");
      this.m_buf.compact();
      this.m_buf.put(src);
      this.m_buf.flip();
      m_mutex.notifyAll();
      }
    else
      {
      m_log.debug("Nothing remaining in buffer...");
      this.m_buf.clear();
      this.m_buf.put(src);
      this.m_buf.flip();
      m_mutex.notifyAll();
      }
    }
  }

代码示例来源:origin: org.littleshoot/mina-port

@Override
public void write(int b) throws IOException {
  ByteBuffer buf = ByteBuffer.allocate(1);
  buf.put((byte) b);
  buf.flip();
  write(buf);
}

代码示例来源:origin: org.littleshoot/mina-util

@Override
public void write(final int b) throws IOException
  {
  final ByteBuffer buf = ByteBuffer.allocate(1);
  buf.put((byte) b);
  buf.flip();
  write(buf);
  }
}

代码示例来源:origin: org.littleshoot/mina-port

public void mergeAll() {
  int sum = 0;
  final int size = bufferQueue.size();
  if (size < 2) {
    // no need to merge!
    return;
  }
  // Get the size of merged BB
  for (Object o : bufferQueue) {
    sum += ((ByteBuffer) o).remaining();
  }
  // Allocate a new BB that will contain all fragments
  ByteBuffer newBuf = ByteBuffer.allocate(sum);
  // and merge all.
  for (;;) {
    ByteBuffer buf = bufferQueue.poll();
    if (buf == null) {
      break;
    }
    newBuf.put(buf);
    buf.release();
  }
  // Push the new buffer finally.
  newBuf.flip();
  bufferQueue.offer(newBuf);
}

代码示例来源:origin: org.littleshoot/sip-stack

public ByteBuffer encode(final SipMessage message)
  {
  final ByteBuffer buffer = ByteBuffer.allocate(300);
  buffer.setAutoExpand(true);
  
  final SipMessageVisitor visitor = new EncoderVisitor(buffer);
  message.accept(visitor);
  buffer.flip();
  
  return buffer;
  }

代码示例来源:origin: org.littleshoot/mina-util

/**
 * Combines the remaining data from the given <code>Collection</code> of
 * <code>ByteBuffer</code>s into a single consolidated 
 * <code>ByteBuffer</code>.
 * 
 * @param buffers The <code>Collection</code> of <code>ByteBuffer</code>s
 * to make into a single buffer.
 * @return A new <code>ByteBuffer</code> combining the remaining data of
 * the <code>Collection</code> of <code>ByteBuffer</code>s.
 */
public static ByteBuffer combine(final Collection<ByteBuffer> buffers)
  {
  final ByteBuffer buf = ByteBuffer.allocate(remaining(buffers));
  for (final ByteBuffer curBuf : buffers)
    {
    buf.put(curBuf);
    }
  buf.flip();
  return buf;
  }

代码示例来源:origin: org.littleshoot/mina-util

private static ByteBuffer createBuffer(final ByteBuffer buffer)
  {
  // We calculate this here because the final split buffer will not
  // necessarily have a size equal to the chunk size -- it will
  // usually be smaller.
  final ByteBuffer data = ByteBuffer.allocate(
    buffer.limit() - buffer.position());
  
  data.put(buffer);
  data.flip();
  return data;
  }

代码示例来源:origin: org.littleshoot/mina-port

public void encode(IoSession session, Object message,
      ProtocolEncoderOutput out) throws Exception {
    if (!(message instanceof Serializable)) {
      throw new NotSerializableException();
    }

    ByteBuffer buf = ByteBuffer.allocate(64);
    buf.setAutoExpand(true);
    buf.putObject(message);

    int objectSize = buf.position() - 4;
    if (objectSize > maxObjectSize) {
      buf.release();
      throw new IllegalArgumentException(
          "The encoded object is too big: " + objectSize + " (> "
              + maxObjectSize + ')');
    }

    buf.flip();
    out.write(buf);
  }
}

代码示例来源:origin: org.littleshoot/mina-port

public void encode(IoSession session, Object message,
    ProtocolEncoderOutput out) throws Exception {
  CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
  if (encoder == null) {
    encoder = charset.newEncoder();
    session.setAttribute(ENCODER, encoder);
  }
  String value = message.toString();
  ByteBuffer buf = ByteBuffer.allocate(value.length())
      .setAutoExpand(true);
  buf.putString(value, encoder);
  if (buf.position() > maxLineLength) {
    throw new IllegalArgumentException("Line length: " + buf.position());
  }
  buf.putString(delimiter.getValue(), encoder);
  buf.flip();
  out.write(buf);
}

代码示例来源:origin: org.littleshoot/tcp-framing

/**
 * Encodes the TCP frame into a {@link ByteBuffer}.
 * 
 * @param frame The frame to encode.
 * @return The encoded frame in a {@link ByteBuffer}.
 */
public ByteBuffer encode(final TcpFrame frame)
  {
  final int length = frame.getLength();
  final ByteBuffer buf = ByteBuffer.allocate(2 + length);
  MinaUtils.putUnsignedShort(buf, length);
  buf.put(frame.getData());
  buf.flip();
  //m_log.debug("Encoded TCP Frame as buffer: {}", buf);
  return buf;
  }

代码示例来源:origin: org.littleshoot/stun-stack

buf.flip();
m_log.debug("Encoded STUN message as buf: {}", buf);
return buf;

代码示例来源:origin: org.littleshoot/mina-port

private void readSession(DatagramSessionImpl session) {
  ByteBuffer readBuf = ByteBuffer.allocate(session.getReadBufferSize());
  try {
    int readBytes = session.getChannel().read(readBuf.buf());
    if (readBytes > 0) {
      readBuf.flip();
      ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit());
      newBuf.put(readBuf);
      newBuf.flip();
      session.increaseReadBytes(readBytes);
      session.getFilterChain().fireMessageReceived(session, newBuf);
    }
  } catch (IOException e) {
    session.getFilterChain().fireExceptionCaught(session, e);
  } finally {
    readBuf.release();
  }
}

代码示例来源:origin: org.littleshoot/mina-port

private void readSession(DatagramChannel channel, RegistrationRequest req)
    throws Exception {
  ByteBuffer readBuf = ByteBuffer
      .allocate(((DatagramSessionConfig) req.config
          .getSessionConfig()).getReceiveBufferSize());
  try {
    SocketAddress remoteAddress = channel.receive(readBuf.buf());
    if (remoteAddress != null) {
      DatagramSessionImpl session = (DatagramSessionImpl) newSession(
          remoteAddress, req.address);
      readBuf.flip();
      ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit());
      newBuf.put(readBuf);
      newBuf.flip();
      session.increaseReadBytes(newBuf.remaining());
      session.getFilterChain().fireMessageReceived(session, newBuf);
    }
  } finally {
    readBuf.release();
  }
}

代码示例来源:origin: org.littleshoot/stun-stack

private static UUID createTransactionId()
  {
  final UUID id = UUID.randomUUID();
  final byte[] idBytes = id.getRawBytes();
  final ByteBuffer idBuf = ByteBuffer.wrap(idBytes);
  
  // Lower the limit to make room for the magic cookie.
  idBuf.limit(idBytes.length - 4);
  
  final ByteBuffer newIdBuf = ByteBuffer.allocate(16);
  MinaUtils.putUnsignedInt(newIdBuf, MAGIC_COOKIE);
  newIdBuf.put(idBuf);
  newIdBuf.flip();
  
  return new UUID(MinaUtils.toByteArray(newIdBuf));
  }

代码示例来源:origin: org.littleshoot/mina-port

ByteBuffer wb = ByteBuffer.allocate(rb.remaining());
wb.put(rb);
wb.flip();
rb.reset();
messageCopy = wb;

代码示例来源:origin: org.littleshoot/mina-port

buf.flip();

代码示例来源:origin: org.littleshoot/mina-util

final ByteBuffer product = this.m_buffer;
this.m_buffer = null;
return finishDecode(product.flip(), out);

相关文章