语言

Menu
Sites
Language
junk characters in bytebuffer

HI all 

i am trying to construct a bytebuffer from byte array 
after that when i print the byte buffer it is giving additional junk to original data
here i n the below code when i try to print "srcbuf" some times it gives additional junk data
can u please find out the error in the code
this is my code taken from dev guide  

ByteBuffer srcBuf;
            ByteBuffer destBuf;

            // Declares an array of byte values
            byte pArray[] = {'A','B','C','D','E','F','G','H','I','J'};

            // Initializes the source array with a capacity of ten elements.
            srcBuf.Construct(10);

            // Copies the ten values from pArray starting at position 0 to the srcBuf
            // Now, srcBuf's position = 10
            srcBuf.SetArray(pArray, 0, 10);

            // Flips the buffer: The limit is set to the current position and
            // then the position is set to zero
            srcBuf.Flip();          // srcBuf's position = 0 and limit = 10
            AppLog("2 is %s ",srcBuf.GetPointer());

            destBuf.Construct(20);

            // Copies from the srcBuf to the destBuf
            // Now, srcBuf's position = 10, the destBuf's position = 10
            destBuf.CopyFrom(srcBuf);
            destBuf.Flip();
            AppLog("destbuff is %s ",destBuf.GetPointer());
            ByteBuffer dbuff;
            dbuff.Construct(16);
            byte a[5];
            destBuf.SetPosition(0);
            destBuf.GetArray(a,0,5);
            dbuff.SetArray(a,0,5);
            dbuff.Flip();
            AppLog("dbuff is %s",dbuff.GetPointer());
            AppLog("dbuff getlimit is %d",dbuff.GetLimit());
            AppLog("destbuff getlimit is %d",destBuf.GetLimit());
            AppLog("sbuff getlimit is %d",srcBuf.GetLimit());

编辑者为: Brock Boland 17 3月, 2014 原因: Paragraph tags added automatically from tizen_format_fix module.

响应

10 回复
Alex Dem

Hi,
Using provided code sample on Tizen 2.2.0 official  (Win 32 sdk) I have got log:
Message: 2 is ABCDEFGHIJ
Message: destbuff is ABCDEFGHIJ
Message: dbuff is ABCDE
Message: dbuff getlimit is 5
Message: destbuff getlimit is 10
Message: sbuff getlimit is 10
After several attempts I did not observe junk extra data. Looks like code is OK but try to use srcBuf.SetPosition(10); before srcBuf.Flip(); just in case.
Alexey.

harish kumar kavali

Hi Alex,

    please check this code & output 
    i'm using tizen 2.2.0 in ubuntu
     
           ByteBuffer buf4,buf5,buf6;
            buf4.Construct(3);
            buf4.Clear();
            buf4.SetArray((byte *)"abc",0,3);
            buf4.Flip();
            buf5.Construct(3);
            buf5.Clear();
            buf5.SetArray((byte *)"def",0,3);
            buf5.Flip();
            buf6.Construct(6);
            buf6.Clear();
            while(buf4.HasRemaining())
            {
                byte b;
                buf4.GetByte(b);
                buf6.SetByte(b);
            }
            while(buf5.HasRemaining())
            {
                byte b;
                buf5.GetByte(b);
                buf6.SetByte(b);
            }
            buf6.Flip();
            AppLog("buf4::%s",buf4.GetPointer());
            AppLog("buf5::%s",buf5.GetPointer());
            AppLog("buf6::%s",buf6.GetPointer());
        break;




11-14 20:04:59.221 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(115) >    buf4::abc
11-14 20:04:59.221 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(116) >     buf5::def
11-14 20:04:59.221 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(117) >     buf6::abcdef

//after running second time

11-14 20:05:04.441 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(115) >    buf4::abc
11-14 20:05:04.441 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(116) >    buf5::def p  (junk)          11-14 20:05:04.441 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(117) >    buf6::abcdef

//after running third time

11-14 20:05:53.641 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(115) >    buf4::abc
11-14 20:05:53.641 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(116) >  buf5::def    �s3� (junk)
11-14 20:05:53.641 : INFO / sampl1 ( 10640 : 10640 ) : virtual void sampl1MainForm::OnActionPerformed(const Tizen::Ui::Control &, int)(117) >    buf6::abcdef

 

Alex Dem

Hi,
Using provided code sample I got junk data on Tizen 2.2.0 device.
Alexey.

Alex Ashirov

Hi,

It seems that one more byte required for null-terminator. Could you please try to reproduce the issue once again with the following changes:

ByteBuffer buf4,buf5,buf6;

                   buf4.Construct(4);

                   buf4.Clear();

                   buf4.SetArray((byte *)"abc",0,4);

                   buf4.Flip();

                   buf5.Construct(4);

                   buf5.Clear();

                   buf5.SetArray((byte *)"def",0,4);

                   buf5.Flip();

                   buf6.Construct(8);

                   buf6.Clear();

                   while(buf4.HasRemaining())

                   {

                       byte b;

                       buf4.GetByte(b);

                       buf6.SetByte(b);

                   }

                   while(buf5.HasRemaining())

                   {

                       byte b;

                       buf5.GetByte(b);

                       buf6.SetByte(b);

                   }

                   buf6.Flip();

                   AppLog("buf4::%s",buf4.GetPointer());

                   AppLog("buf5::%s",buf5.GetPointer());

                   AppLog("buf6::%s",buf6.GetPointer());

 

Of course, in this case buf6 will be displayed like buf4 (without buf5), but this is not a problem.

Alex Ashirov

Hi,

I have just tried this using Tizen sdk 2.2.0 for Windows. I didn’t observe any junk characters. Are you able to try this on Windows?

harish kumar kavali

Hi,
         but i'm working on linux ubuntu 12.10 ,is there any changes or settings to be done to overcome this problem in linux
Regards ,
Harish

Alex Ashirov

Hi Harish,

It seems that the problem has been solved. Please find my message above (BY Alex Ashirov, 14 Ноя 2013, 07:16) for solution. Please let me know if the issue is still present.

harish kumar kavali

Hi ,

but in buf 6 i am not getting concatinated value , it is just giving abc instead of abcdef. 

Regards
K.Harish

harish kumar kavali

hi alex,
thank you so much ,i had solved my problem by skipping '\0' characters
-Harish

Alex Ashirov

Right, you need to remove '\0' from the end of the buf4 during concatenation, but please note that you still need '\0' at the end of the buf6. Otherwise, the junk characters may appear again.