You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Jan Andersson <ja...@minq.se> on 2005/02/03 14:11:43 UTC

[mina] ByteBuffer allocation

I have a working prototype of an IoHandlerFilter that uses SSL to 
encrypt/decrypt
data. The implementation uses the new SSLEngine utilities provided with 
Java  1.5.

I have a few questions regarding ByteBuffer allocation.

In dataRead(Handler nextHandler, IoSession session, ByteBuffer buf) I 
send the
ByteBuffer to my SSL Utility to decrypt the buffer. Depending on SSL 
state this
either involves initial SSL handshaking or simply decryption of the 
data.
Encryptinon is handled in filterWrite(IoSession session, ByteBuffer 
buf).
I have 2 cases where I allocate org.apache.mina.common.ByteBuffers:

1) To write data during SSL handshake i allocate ByteBuffer(s) using
    ByteBuffer.allocate(), copies the encrypted data and write it to the 
net using
    session.write().

2) In filterWrite(IoSession session, ByteBuffer buf) I encrypt the 
provided
    ByteBuffer (if not doing initial handshake where provided data is 
already
    encrypted). I allocate a ByteBuffer, copies the encrypted data to 
this
    buffer and return it

Now to my questions:

- case 1) where i call session.write(), where would be the best place to
   release allocated ByteBuffer?

- for case 2) I allocate a new buffer, since the encrypted data may be 
larger
   that the provided ByteBuffer. Where would be the best place to 
release allocated
   ByteBuffer in this case?

I guess the best solution would be (for case 2) to make sure Mina 
allokates buffers
of a configurable size, somehough? But for case 1) I have to allocate 
and release
ByteBuffers...

One thing that could help, might be to provide a method in 
org.apache.mina.common.ByteBuffers
that allows me to create a org.apache.mina.common.ByteBuffer from a 
java.nio.ByteBuffer.
This would *not* use the cache/stacks provided, but allow me to create 
a Mina-style
ByteBuffer from a java.nio.ByteBuffer (that is allocated/released by 
me).

Withh the hope that this did make some sence ;)

/Janne


Re: [mina] ByteBuffer allocation

Posted by Trustin Lee <tr...@gmail.com>.
Hi Janne,

On Thu, 3 Feb 2005 15:44:42 +0100, Jan Andersson <ja...@minq.se> wrote:
> Trustin,
> 
> >> - case 1) where i call session.write(), where would be the best place
> >> to
> >>    release allocated ByteBuffer?
> >
> > You don't need to release it because MINA releases it for you. :)
> 
> Great.
> 
> >> - for case 2) I allocate a new buffer, since the encrypted data may be
> >> larger
> >>    that the provided ByteBuffer. Where would be the best place to
> >> release allocated
> >>    ByteBuffer in this case?
> >
> > You don't need to release it, either.  But I forgot to release old
> > buffer (the one passes as a parameter).  I'll fix it.
> 
> Ok.
> 
> >> I guess the best solution would be (for case 2) to make sure Mina
> >> allokates buffers
> >> of a configurable size, somehough? But for case 1) I have to allocate
> >> and release
> >> ByteBuffers...
> >
> > MINA can create ByteBuffer with max 8KB.  So if you encrypt 8KB
> > ByteBuffer, you might not be able to allocate which fits with the
> > encrypted data.  I'll have to make ByteBuffer.allocate can allocate
> > more bigger buffers.
> 
> Great. The buffers used by SSLEngine must be sized appropriately to
> hold the
> maximum record that can be produced. Calls to
> SSLSession.getPacketBufferSize()
> and SSLSession.getApplicationBufferSize() should be used to determine
> the
> appropriate buffer sizes. I'm not sure, but I beleive it's 16KB in my
> case.

This issue has been fixed.  Please look at:
http://issues.apache.org/jira/browse/DIRMINA-4

> 
> >> One thing that could help, might be to provide a method in
> >> org.apache.mina.common.ByteBuffers
> >> that allows me to create a org.apache.mina.common.ByteBuffer from a
> >> java.nio.ByteBuffer.
> >> This would *not* use the cache/stacks provided, but allow me to create
> >> a Mina-style
> >> ByteBuffer from a java.nio.ByteBuffer (that is allocated/released by
> >> me).
> >
> > Is there any reason that you need to wrap java.nio.ByteBuffer?  Could
> > you explain about this a little more?
> 
> No, ignore this for now ;)
> 
> Thanks,
> /Janne
> 
> 

Cheers,
Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/

Re: [mina] ByteBuffer allocation

Posted by Jan Andersson <ja...@minq.se>.
Trustin,

>> - case 1) where i call session.write(), where would be the best place 
>> to
>>    release allocated ByteBuffer?
>
> You don't need to release it because MINA releases it for you. :)

Great.

>> - for case 2) I allocate a new buffer, since the encrypted data may be
>> larger
>>    that the provided ByteBuffer. Where would be the best place to
>> release allocated
>>    ByteBuffer in this case?
>
> You don't need to release it, either.  But I forgot to release old
> buffer (the one passes as a parameter).  I'll fix it.

Ok.

>> I guess the best solution would be (for case 2) to make sure Mina
>> allokates buffers
>> of a configurable size, somehough? But for case 1) I have to allocate
>> and release
>> ByteBuffers...
>
> MINA can create ByteBuffer with max 8KB.  So if you encrypt 8KB
> ByteBuffer, you might not be able to allocate which fits with the
> encrypted data.  I'll have to make ByteBuffer.allocate can allocate
> more bigger buffers.

Great. The buffers used by SSLEngine must be sized appropriately to 
hold the
maximum record that can be produced. Calls to 
SSLSession.getPacketBufferSize()
and SSLSession.getApplicationBufferSize() should be used to determine 
the
appropriate buffer sizes. I'm not sure, but I beleive it's 16KB in my 
case.

>> One thing that could help, might be to provide a method in
>> org.apache.mina.common.ByteBuffers
>> that allows me to create a org.apache.mina.common.ByteBuffer from a
>> java.nio.ByteBuffer.
>> This would *not* use the cache/stacks provided, but allow me to create
>> a Mina-style
>> ByteBuffer from a java.nio.ByteBuffer (that is allocated/released by
>> me).
>
> Is there any reason that you need to wrap java.nio.ByteBuffer?  Could
> you explain about this a little more?

No, ignore this for now ;)

Thanks,
/Janne


Re: [mina] ByteBuffer allocation

Posted by Trustin Lee <tr...@gmail.com>.
Hi.

On Thu, 3 Feb 2005 14:11:43 +0100, Jan Andersson <ja...@minq.se> wrote:
> I have a working prototype of an IoHandlerFilter that uses SSL to
> encrypt/decrypt
> data. The implementation uses the new SSLEngine utilities provided with
> Java  1.5.
> 
> I have a few questions regarding ByteBuffer allocation.
> 
> In dataRead(Handler nextHandler, IoSession session, ByteBuffer buf) I
> send the
> ByteBuffer to my SSL Utility to decrypt the buffer. Depending on SSL
> state this
> either involves initial SSL handshaking or simply decryption of the
> data.
> Encryptinon is handled in filterWrite(IoSession session, ByteBuffer
> buf).
> I have 2 cases where I allocate org.apache.mina.common.ByteBuffers:
> 
> 1) To write data during SSL handshake i allocate ByteBuffer(s) using
>     ByteBuffer.allocate(), copies the encrypted data and write it to the
> net using
>     session.write().
> 
> 2) In filterWrite(IoSession session, ByteBuffer buf) I encrypt the
> provided
>     ByteBuffer (if not doing initial handshake where provided data is
> already
>     encrypted). I allocate a ByteBuffer, copies the encrypted data to
> this
>     buffer and return it
> 
> Now to my questions:
> 
> - case 1) where i call session.write(), where would be the best place to
>    release allocated ByteBuffer?

You don't need to release it because MINA releases it for you. :)

> - for case 2) I allocate a new buffer, since the encrypted data may be
> larger
>    that the provided ByteBuffer. Where would be the best place to
> release allocated
>    ByteBuffer in this case?

You don't need to release it, either.  But I forgot to release old
buffer (the one passes as a parameter).  I'll fix it.

> I guess the best solution would be (for case 2) to make sure Mina
> allokates buffers
> of a configurable size, somehough? But for case 1) I have to allocate
> and release
> ByteBuffers...

MINA can create ByteBuffer with max 8KB.  So if you encrypt 8KB
ByteBuffer, you might not be able to allocate which fits with the
encrypted data.  I'll have to make ByteBuffer.allocate can allocate
more bigger buffers.

> One thing that could help, might be to provide a method in
> org.apache.mina.common.ByteBuffers
> that allows me to create a org.apache.mina.common.ByteBuffer from a
> java.nio.ByteBuffer.
> This would *not* use the cache/stacks provided, but allow me to create
> a Mina-style
> ByteBuffer from a java.nio.ByteBuffer (that is allocated/released by
> me).

Is there any reason that you need to wrap java.nio.ByteBuffer?  Could
you explain about this a little more?

Trustin
-- 
what we call human nature is actually human habit
--
http://gleamynode.net/