You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Pablo Palazon <pp...@dsi.uclm.es> on 2008/10/07 13:27:49 UTC
Bit access to IoBuffer
Hello!
I know that I can access to IoBuffer with byte level, I only can get 1
byte each time. Is it possible to get only 1 bit??. My problem is like
this:
I have a data format with 26 bits (3 bytes and 2 bits more), there are 5
fields (3 fields of 1 byte and 2 fields of 1 bit each one). The 2 fields
of 1 bit are in the middle. If I get this data with byte level, I'll
lost information for next messages, because I get so more information in
this messages.
I'd like to know if is possible to get only one bit in the IoBuffer, or
if you know one better solution for IoBuffer. Thanks in advance
Re: Bit access to IoBuffer
Posted by Emmanuel Lecharny <el...@gmail.com>.
Pablo Palazon wrote:
> But imagine this situation, 2 messages it's this:
>
> 10100111 11111111 11
> 11000011 01011010 00
>
> I need to decode this message, and I need to get 3 bytes, and I get:
>
> 10100111 11111111 11|110000
>
> I can get last 2 bits with mask, but I'll get the next messages, I lost
> 110000 from the first byte, because I get this before. Before get first
> byte from next messages, I next to rewind 6 bits, and I'll get correct
> byte: 11000011
>
> If I don't do this, for get correct byte, I'll get 1 byte + 2 bits and
> concatenate because this messages isn't byte multiple.
>
You just have to store the remaining bits in the session, so that you
can get them when you receive the following bytes. This is exactly what
we are doing for decoding Ldap messages (except that we aren't decoding
bits), as we may receive them in more than one PDU.
So the idea is to write a stateful decoder, with a 'container' storing
the received bits until you reach a message limit. You can store this
container in the current session, assuming that the next bits will be
received on the same session a few ms later.
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org
Re: Bit access to IoBuffer
Posted by Emmanuel Lecharny <el...@gmail.com>.
Pablo Palazon wrote:
> El mar, 07-10-2008 a las 17:28 +0200, Emmanuel Lecharny escribió:
>
>> Andres Quijano wrote:
>>
>>> You'll have to mask the rest of the byte for the next message...
>>>
>> You just have to store the current position in the stored bytes.
>>
>>> maybe
>>> create a class where you can add bits to it until it reaches the msg
>>> length, you'll have to keep count of which bits belong to which
>>> message
>>>
>>>
>> As soon as you have read a message, you can discard the associated
>> bytes, except maybe the last one, which may be shared with the next
>> message. I don't think you need to add bits to any structure, you just
>> ave to keep the bytes and the starting position of the next message.
>>
>> For instance, let's say that your messages are 10 bits long. You receive
>> 1 byte :
>> 1001 0010
>>
>> As you didn't received the 10 bits, you store the byte into the
>> container, and store the container into the session. (here, the
>> container will just store the byte 0x92)
>>
>> Then you receive the next byte :
>> 0100 1010
>>
>> This is obvioulsy the end of the first message, and the beginning of the
>> second message (which is not terminated).
>>
>> As you now have more than 10 bits to decode (the first byte plus the 2
>> higher bits of the second byte), you can decode the first message, and
>> discard the first byte.
>> The remaining byte (0x4A) will be stored into the container, and we will
>> also store the current position (2) representing the starting position
>> of the next message.
>>
>> Then you receive a third byte :
>> 1011 0000
>>
>> as you still have the previous byte (0x4A) you can decode the message
>> 001010-1011. The second byte can be discarded, the third byte is stored
>> into the container, and a new position (4) is also stored there.
>>
>> Etc...
>>
>>
>
> Ok, then at last messages we'll receive 0's until complete last byte, it
> isn't?.
>
Yop. But it can be anything else, depending on how you deal with message
boundaries (messages can be fixed size, or can be variable, with some
information about the message length.
> In my server, each request it's a messages, then for each message the
> last byte will contain six 0 bits (there are 26 bits for each message
> and I'll receive 4 bytes).
>
Not necessarily, if you receive 2 messages in a row : the second message
might very well start immediately after the last message's last bit. (of
course, if your client send more than one message and if the sender does
gather the messages).
It all depends on how the messages are emitted.
FYI, you raised an interesting point with those bits based message :
using an ASN.1 PER encoded messages, you will have to deal with bits,
instead of bytes. Typically the case for fastInfoset, fast webservices,
H.323 protocol, multiple UMTS protocols ...
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org
Re: Bit access to IoBuffer
Posted by Pablo Palazon <pp...@dsi.uclm.es>.
El mar, 07-10-2008 a las 17:28 +0200, Emmanuel Lecharny escribió:
> Andres Quijano wrote:
> > You'll have to mask the rest of the byte for the next message...
> You just have to store the current position in the stored bytes.
> > maybe
> > create a class where you can add bits to it until it reaches the msg
> > length, you'll have to keep count of which bits belong to which
> > message
> >
> As soon as you have read a message, you can discard the associated
> bytes, except maybe the last one, which may be shared with the next
> message. I don't think you need to add bits to any structure, you just
> ave to keep the bytes and the starting position of the next message.
>
> For instance, let's say that your messages are 10 bits long. You receive
> 1 byte :
> 1001 0010
>
> As you didn't received the 10 bits, you store the byte into the
> container, and store the container into the session. (here, the
> container will just store the byte 0x92)
>
> Then you receive the next byte :
> 0100 1010
>
> This is obvioulsy the end of the first message, and the beginning of the
> second message (which is not terminated).
>
> As you now have more than 10 bits to decode (the first byte plus the 2
> higher bits of the second byte), you can decode the first message, and
> discard the first byte.
> The remaining byte (0x4A) will be stored into the container, and we will
> also store the current position (2) representing the starting position
> of the next message.
>
> Then you receive a third byte :
> 1011 0000
>
> as you still have the previous byte (0x4A) you can decode the message
> 001010-1011. The second byte can be discarded, the third byte is stored
> into the container, and a new position (4) is also stored there.
>
> Etc...
>
Ok, then at last messages we'll receive 0's until complete last byte, it
isn't?.
In my server, each request it's a messages, then for each message the
last byte will contain six 0 bits (there are 26 bits for each message
and I'll receive 4 bytes).
Thanks your so much for your help!!
Re: Bit access to IoBuffer
Posted by Emmanuel Lecharny <el...@gmail.com>.
Andres Quijano wrote:
> You'll have to mask the rest of the byte for the next message...
You just have to store the current position in the stored bytes.
> maybe
> create a class where you can add bits to it until it reaches the msg
> length, you'll have to keep count of which bits belong to which
> message
>
As soon as you have read a message, you can discard the associated
bytes, except maybe the last one, which may be shared with the next
message. I don't think you need to add bits to any structure, you just
ave to keep the bytes and the starting position of the next message.
For instance, let's say that your messages are 10 bits long. You receive
1 byte :
1001 0010
As you didn't received the 10 bits, you store the byte into the
container, and store the container into the session. (here, the
container will just store the byte 0x92)
Then you receive the next byte :
0100 1010
This is obvioulsy the end of the first message, and the beginning of the
second message (which is not terminated).
As you now have more than 10 bits to decode (the first byte plus the 2
higher bits of the second byte), you can decode the first message, and
discard the first byte.
The remaining byte (0x4A) will be stored into the container, and we will
also store the current position (2) representing the starting position
of the next message.
Then you receive a third byte :
1011 0000
as you still have the previous byte (0x4A) you can decode the message
001010-1011. The second byte can be discarded, the third byte is stored
into the container, and a new position (4) is also stored there.
Etc...
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org
Re: Bit access to IoBuffer
Posted by Andres Quijano <tu...@gmail.com>.
You'll have to mask the rest of the byte for the next message... maybe
create a class where you can add bits to it until it reaches the msg
length, you'll have to keep count of which bits belong to which
message
On Tue, Oct 7, 2008 at 11:41 AM, Pablo Palazon <pp...@dsi.uclm.es> wrote:
> El mar, 07-10-2008 a las 13:54 +0200, Emmanuel Lecharny escribió:
>> Pablo Palazon wrote:
>> > Hello!
>> >
>> > I know that I can access to IoBuffer with byte level, I only can get 1
>> > byte each time. Is it possible to get only 1 bit??. My problem is like
>> > this:
>> >
>> > I have a data format with 26 bits (3 bytes and 2 bits more), there are 5
>> > fields (3 fields of 1 byte and 2 fields of 1 bit each one). The 2 fields
>> > of 1 bit are in the middle. If I get this data with byte level, I'll
>> > lost information for next messages, because I get so more information in
>> > this messages.
>> >
>> Whatever you do, the bits will be transefered into bytes. It's up to you
>> to manage the bits on the server. You won't loose anything.
>> > I'd like to know if is possible to get only one bit in the IoBuffer,
>> Of course : get a byte, and using the correct mask ( byte & 0x80 for
>> instance to get the higner bit in the byte), you will be able to get a
>> single bit.
>
> Yes, It's right.
>
>> If you have some message finishing in the middle of a byte, then you
>> will have to handle this by splitting the byte in two parts. This is
>> certainly manageable.
>
> But imagine this situation, 2 messages it's this:
>
> 10100111 11111111 11
> 11000011 01011010 00
>
> I need to decode this message, and I need to get 3 bytes, and I get:
>
> 10100111 11111111 11|110000
>
> I can get last 2 bits with mask, but I'll get the next messages, I lost
> 110000 from the first byte, because I get this before. Before get first
> byte from next messages, I next to rewind 6 bits, and I'll get correct
> byte: 11000011
>
> If I don't do this, for get correct byte, I'll get 1 byte + 2 bits and
> concatenate because this messages isn't byte multiple.
>
> Thanks you so much!!
Re: Bit access to IoBuffer
Posted by Pablo Palazon <pp...@dsi.uclm.es>.
El mar, 07-10-2008 a las 13:54 +0200, Emmanuel Lecharny escribió:
> Pablo Palazon wrote:
> > Hello!
> >
> > I know that I can access to IoBuffer with byte level, I only can get 1
> > byte each time. Is it possible to get only 1 bit??. My problem is like
> > this:
> >
> > I have a data format with 26 bits (3 bytes and 2 bits more), there are 5
> > fields (3 fields of 1 byte and 2 fields of 1 bit each one). The 2 fields
> > of 1 bit are in the middle. If I get this data with byte level, I'll
> > lost information for next messages, because I get so more information in
> > this messages.
> >
> Whatever you do, the bits will be transefered into bytes. It's up to you
> to manage the bits on the server. You won't loose anything.
> > I'd like to know if is possible to get only one bit in the IoBuffer,
> Of course : get a byte, and using the correct mask ( byte & 0x80 for
> instance to get the higner bit in the byte), you will be able to get a
> single bit.
Yes, It's right.
> If you have some message finishing in the middle of a byte, then you
> will have to handle this by splitting the byte in two parts. This is
> certainly manageable.
But imagine this situation, 2 messages it's this:
10100111 11111111 11
11000011 01011010 00
I need to decode this message, and I need to get 3 bytes, and I get:
10100111 11111111 11|110000
I can get last 2 bits with mask, but I'll get the next messages, I lost
110000 from the first byte, because I get this before. Before get first
byte from next messages, I next to rewind 6 bits, and I'll get correct
byte: 11000011
If I don't do this, for get correct byte, I'll get 1 byte + 2 bits and
concatenate because this messages isn't byte multiple.
Thanks you so much!!
Re: Bit access to IoBuffer
Posted by Emmanuel Lecharny <el...@gmail.com>.
Pablo Palazon wrote:
> Hello!
>
> I know that I can access to IoBuffer with byte level, I only can get 1
> byte each time. Is it possible to get only 1 bit??. My problem is like
> this:
>
> I have a data format with 26 bits (3 bytes and 2 bits more), there are 5
> fields (3 fields of 1 byte and 2 fields of 1 bit each one). The 2 fields
> of 1 bit are in the middle. If I get this data with byte level, I'll
> lost information for next messages, because I get so more information in
> this messages.
>
Whatever you do, the bits will be transefered into bytes. It's up to you
to manage the bits on the server. You won't loose anything.
> I'd like to know if is possible to get only one bit in the IoBuffer,
Of course : get a byte, and using the correct mask ( byte & 0x80 for
instance to get the higner bit in the byte), you will be able to get a
single bit.
If you have some message finishing in the middle of a byte, then you
will have to handle this by splitting the byte in two parts. This is
certainly manageable.
--
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org