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

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

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

ByteBuffer.allocate介绍

[英]Returns the direct or heap buffer which is capable of the specified size. This method tries to allocate direct buffer first, and then tries heap buffer if direct buffer memory is exhausted. Please use #allocate(int,boolean) to allocate buffers of specific type.
[中]返回能够达到指定大小的直接或堆缓冲区。此方法首先尝试分配直接缓冲区,然后在直接缓冲区内存耗尽时尝试堆缓冲区。请使用#分配(int,boolean)分配特定类型的缓冲区。

代码示例

代码示例来源:origin: org.reddwarfserver.client/sgs-client

/**
 * Default constructor.
 */
CompleteMessageFilter() {
  msgBuf = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE, false);
}

代码示例来源:origin: org.apache.directory.mina/mina-core

/**
 * Returns the direct or heap buffer which is capable of the specified
 * size.  This method tries to allocate direct buffer first, and then
 * tries heap buffer if direct buffer memory is exhausted.  Please use
 * {@link #allocate(int, boolean)} to allocate buffers of specific type.
 *
 * @param capacity the capacity of the buffer
 */
public static ByteBuffer allocate( int capacity )
{
  if( useDirectBuffers )
  {
    try
    {
      // first try to allocate direct buffer
      return allocate( capacity, true );
    }
    catch( OutOfMemoryError e )
    {
      // fall through to heap buffer
    }
  }
  return allocate( capacity, false );
}

代码示例来源:origin: org.apache.directory.mina/mina-core

private Context()
{
  decoder = charset.newDecoder();
  buf = ByteBuffer.allocate( 80 ).setAutoExpand( true );
}

代码示例来源:origin: org.apache.mina/mina-filter-ssl

/**
 * Creates a new Mina byte buffer that is a deep copy of the remaining bytes
 * in the given buffer (between index buf.position() and buf.limit())
 *
 * @param src the buffer to copy
 * @return the new buffer, ready to read from
 */
public static org.apache.mina.common.ByteBuffer copy(java.nio.ByteBuffer src) {
  org.apache.mina.common.ByteBuffer copy = org.apache.mina.common.ByteBuffer
      .allocate(src.remaining());
  copy.put(src);
  copy.flip();
  return copy;
}

代码示例来源:origin: org.apache.directory.mina/mina-core

public IoSessionInputStream()
{
  buf = ByteBuffer.allocate( 16 );
  buf.setAutoExpand( true );
  buf.limit( 0 );
}

代码示例来源:origin: alibaba/tair-java-client

public void encode(IoSession session, Object message,
    ProtocolEncoderOutput out) throws Exception {
  if(!(message instanceof byte[])){
    throw new Exception("must send byte[]");
  }
  byte[] payload=(byte[]) message;
  ByteBuffer buf = ByteBuffer.allocate(payload.length, false);
  buf.put(payload);
  buf.flip();
  out.write(buf);
  if (isDebugEnabled)
    LOGGER.debug(TairUtil.toHex(payload));
}

代码示例来源:origin: org.apache.directory.mina/mina-core

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

代码示例来源:origin: org.apache.directory.mina/mina-core

private void storeRemainingInSession(ByteBuffer buf, IoSession session)
  {
    ByteBuffer remainingBuf = ByteBuffer.allocate( buf.remaining() );
    remainingBuf.setAutoExpand( true );
    remainingBuf.put( buf );
    session.setAttribute( BUFFER, remainingBuf );
  }    
}

代码示例来源:origin: org.reddwarfserver.client/sgs-client

/**
   * Prepends the length of the given byte array as a 2-byte {@code short}
   * in network byte-order, and passes the result to the {@linkplain
   * FilterListener#sendUnfiltered sendUnfiltered} method of the
   * given {@code listener}.
   *
   * @param listener the {@code FilterListener} on which to send the data
   * @param message the data to filter and forward to the listener
   */
  void filterSend(FilterListener listener, byte[] message) {
    ByteBuffer buffer = ByteBuffer.allocate(message.length + 2, false);
    buffer.putShort((short) message.length);
    buffer.put(message);
    buffer.flip();
    // Don't worry about the listener throwing an exception, since
    // this method has no other side effects.
    listener.sendUnfiltered(buffer);
  }
}

代码示例来源:origin: org.apache.directory.mina/mina-core

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.apache.directory.mina/mina-core

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.apache.directory.mina/mina-core

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 );
      ( ( DatagramFilterChain ) session.getFilterChain() ).messageReceived( session, newBuf );
    }
  }
  catch( IOException e )
  {
    ( ( DatagramFilterChain ) session.getFilterChain() ).exceptionCaught( session, e );
  }
  finally
  {
    readBuf.release();
  }
}

代码示例来源:origin: org.apache.directory.mina/mina-core

private void readSession( DatagramSessionImpl session )
{
  ByteBuffer readBuf = ByteBuffer.allocate( session.getReadBufferSize() );
  try
  {
    SocketAddress remoteAddress = session.getChannel().receive(
        readBuf.buf() );
    if( remoteAddress != null )
    {
      readBuf.flip();
      session.setRemoteAddress( remoteAddress );
      ByteBuffer newBuf = ByteBuffer.allocate( readBuf.limit() );
      newBuf.put( readBuf );
      newBuf.flip();
      session.increaseReadBytes( newBuf.remaining() );
      ( ( DatagramFilterChain ) session.getFilterChain() ).messageReceived( session, newBuf );
    }
  }
  catch( IOException e )
  {
    ( ( DatagramFilterChain ) session.getFilterChain() ).exceptionCaught( session, e );
  }
  finally
  {
    readBuf.release();
  }
}

代码示例来源:origin: org.apache.directory.mina/mina-core

ByteBuffer newBuf = ByteBuffer.allocate( sum );

代码示例来源:origin: org.apache.directory.mina/mina-core

private void read( SocketSessionImpl session )
  ByteBuffer buf = ByteBuffer.allocate( session.getReadBufferSize() );
  SocketChannel ch = session.getChannel();

代码示例来源:origin: org.apache.directory.server/mitosis

public final void encode( IoSession session, Object in, ProtocolEncoderOutput out ) throws Exception
{
  BaseMessage m = ( BaseMessage ) in;
  ByteBuffer buf = ByteBuffer.allocate( 16 );
  buf.setAutoExpand( true );
  buf.put( ( byte ) m.getType() );
  buf.putInt( m.getSequence() );
  buf.putInt( 0 ); // placeholder for body length field
  final int bodyStartPos = buf.position();
  encodeBody( m, buf );
  final int bodyEndPos = buf.position();
  final int bodyLength = bodyEndPos - bodyStartPos;
  // fill bodyLength
  buf.position( bodyStartPos - 4 );
  buf.putInt( bodyLength );
  buf.position( bodyEndPos );
  buf.flip();
  out.write( buf );
}

代码示例来源:origin: org.apache.directory.mina/mina-core

rb.mark();
byteCount = rb.remaining();
ByteBuffer wb = ByteBuffer.allocate( rb.remaining() );
wb.put( rb );
wb.flip();

代码示例来源:origin: org.apache.directory.mina/mina-core

ByteBuffer tmp = ByteBuffer.allocate( 2 ).setAutoExpand( true );
tmp.putString( delimiter.getValue(), charset.newEncoder() );
tmp.flip();

相关文章