You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Sungwon Jung <th...@gmail.com> on 2007/09/16 17:21:11 UTC

ByteBuffer.allocate() question

Hello. I'm using MINA 1.1.2

I have a question about ByteBuffer.allocate();

Let me show you code.

--
ByteBuffer msg = ByteBuffer.allocate( 9, false );
msg.setAutoExpand( true );

msg.put( (byte)0x01 );		// 1 byte
msg.putShort( (short)0x01 );		// 2 byte
msg.put( (byte)0x01 );		// 1 byte
msg.put( (byte)0x01 );		// 1 byte
msg.putInt( 0 );			// 4 byte
msg.put( (byte)0x01 );		// 1 byte
msg.put( (byte)0x01 );		// 1 byte
msg.put( (byte)0x01 );		// 1 byte
msg.put( (byte)0x01 );		// 1 byte

logger.debug( msg.limit() );		// output is 16

but, ByteBuffer msg = ByteBuffer.allocate( 8, false );

logger.debug( msg.limit() );		// output is 13


what's the reason?

Re: ByteBuffer.allocate() question

Posted by Jeroen Brattinga <je...@gmail.com>.
The msg.setAutoExpand(true) causes the buffer to automatically change 
the capacity and limit. If you need the exact size, you have to take 
care of it yourself (i.e. determine the buffer size beforehand, allocate 
it and setAutoExpand(false) ).


Jeroen Brattinga

Sungwon Jung wrote:
> Hello. I'm using MINA 1.1.2
>
> I have a question about ByteBuffer.allocate();
>
> Let me show you code.
>
> --
> ByteBuffer msg = ByteBuffer.allocate( 9, false );
> msg.setAutoExpand( true );
>
> msg.put( (byte)0x01 );		// 1 byte
> msg.putShort( (short)0x01 );		// 2 byte
> msg.put( (byte)0x01 );		// 1 byte
> msg.put( (byte)0x01 );		// 1 byte
> msg.putInt( 0 );			// 4 byte
> msg.put( (byte)0x01 );		// 1 byte
> msg.put( (byte)0x01 );		// 1 byte
> msg.put( (byte)0x01 );		// 1 byte
> msg.put( (byte)0x01 );		// 1 byte
>
> logger.debug( msg.limit() );		// output is 16
>
> but, ByteBuffer msg = ByteBuffer.allocate( 8, false );
>
> logger.debug( msg.limit() );		// output is 13
>
>
> what's the reason?
>
>