Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:29:18+00:00 2026-05-26T11:29:18+00:00

I have a little issue with my code which I am using to read

  • 0

I have a little issue with my code which I am using to read the response from web server.I get the whole information as a data packets so I have to parse the whole response and get the whole packets which server is sending to me. I don’t have problem with the JSON data, but when server sends me a binary data my application crash because I can’t get the whole binary data in 30KB (I’m reading the response in pieces of 30KB). So can anyone just help me a little bit which is the best way to get the response in pieces and parse it?
Here is the sample code which I’m using :

    public void handleDataFromSync(String responseBody) {

        RPCPacket packet;
        X:for(int index=0;index<responseBody.length();index++){
            try {
                    Log.w("Response ","Response size : "+ responseBody.length());
                    Log.w("Response ","Response : "+ responseBody);
                    Log.w("","****************Index is : "+index);
                    Log.i("","BufferString FIRST CHECK OF BUFFER : "+bufferString);

                if(bufferString!=null){

                    shitavaRabota = bufferString.concat(responseBody);
                    Log.v("","ShitavaRabota : "+shitavaRabota);
                    //
                    bufferString = shitavaRabota;
                        Log.i("","BufferString = SHITAVA RABOTA : "+bufferString);
                        Log.i("","BufferString SUBSTR : "+bufferString.substring(0));
                        Log.v("","BufferString LNHTG : "+bufferString.length());
                    remainingSize+=responseBody.length();
                        Log.i("","remeinign size : "+remainingSize);
                        Log.i("","neededSize size : "+neededSize);
                    if(neededSize<remainingSize){
                        data=null;
                        data = bufferString.substring(0, getLastPacketRemainingSize+getSize);
                            Log.v("","DataSize : "+dataSize);
                            Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);
                            Log.i("","bufferString current value : "+bufferString.substring(index, index + dataSize));

                        index = (index + dataSize)-1;
                            Log.w("","****************Index is must be  : "+index);
                            Log.e("","Data Size : "+data.length());
                        first = Base64.decode(data);
                        String string = new String(first, "UTF-8");
                            Log.w("Response ","BASE 64 : "+ string);

                        packet = new RPCPacket( objectIdentificator,
                                                        RPCPacketType.getPacketTypeByValue(pType),
                                                        RPCOperationType.getByValue(operationType),
                                                        objectOId,
                                                        id,
                                                        dataSize,
                                                        hashH,
                                                        RPCPacketDataType.getByValue(dataType),
                                                        first);

                        parseRPCPacket(packet);

                        int bLength = bufferString.length()-dataSize;
                        responseBody = bufferString.substring(bLength);
                            Log.e("","ResponseBody = BufferString : "+responseBody);
                        shitavaRabota = null;
                    } else if(neededSize>remainingSize) {
                        bufferString = bufferString.concat(responseBody.substring(index, responseBody.length()-index));
                            Log.i("","BufferString : "+bufferString);

                    }
                }


                Log.v("","index before objectId : "+index);
                Log.v("","index before objectId 2 : "+(index+packetFieldSizes[0]));
                objectIdentificator = 0;
                objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0]));
                    Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator);
                    Log.i("","Response current value : "+responseBody.substring(index,index+packetFieldSizes[0]));

                index = index+packetFieldSizes[0];
                    Log.w("","****************Index is  : "+index);


                pType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[1]));
                    Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType);
                    Log.i("","Response current value : "+responseBody.substring(index,index + packetFieldSizes[1]));

                index = index + packetFieldSizes[1];
                    Log.w("","****************Index is  : "+index);



                operationType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[2]));
                    Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType);
                    Log.i("","Response current value : "+responseBody.substring(index,index + packetFieldSizes[2]));

                index = index + packetFieldSizes[2];
                    Log.w("","****************Index is  : "+index);


                objectOId=null;
                objectOId = responseBody.substring(index, index + packetFieldSizes[3]);
                    Log.w("Response ","UID (CHAR, length 32) : "+ objectOId);
                    Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[3]));

                index = index + packetFieldSizes[3];
                    Log.w("","****************Index is : "+index);


                id=0;
                id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4]));
                    Log.w("Response ","ID (LONGINT) : "+ id);
                    Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[4]));

                index = index + packetFieldSizes[4];
                    Log.w("","****************Index is (must be 102) : "+index);

                dataSize = 0;
                dataSize = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[5]));
                    Log.w("Response ","Data Size (LONGINT) : "+ dataSize);
                    Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[5]));


                index = index + packetFieldSizes[5];
                    Log.w("","****************Index is (must be 134) : "+index);


                hashH=null;
                hashH = responseBody.substring(index,index + packetFieldSizes[6]);
                    Log.w("Response ","Data Hash (CHAR, length 32 : "+ hashH);
                    Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[6]));


                index = index + packetFieldSizes[6];
                    Log.w("","****************Index is (must be 166) : "+index);

                dataType= 0;
                dataType = Short.parseShort(responseBody.substring(index,index + packetFieldSizes[7]));
                    Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType);
                    Log.i("","Response current value : "+responseBody.substring(index, index + packetFieldSizes[7]));

                index = index + packetFieldSizes[7];
                    Log.w("","****************Index is (must be 169) : "+index);


                neededSize = dataSize; // cherna magiq
                remainingSize = (responseBody.length()-index);
                    Log.v("","NeededSize : "+neededSize);
                    Log.v("","Response length "+remainingSize);


               getSize = neededSize - remainingSize;
               Log.v("","getSize : "+getSize);

                if(neededSize<remainingSize){
                    data=null;
                    data = responseBody.substring(index, index + dataSize);
                        Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);
                        Log.i("","Response current value : "+responseBody.substring(index, index + dataSize));

                    index = (index + dataSize)-1;
                        Log.w("","****************Index is must be  : "+index);
                    first = Base64.decode(data);
                    String string = new String(first, "UTF-8");
                        Log.w("Response ","BASE 64 : "+ string);

                    packet = new RPCPacket( objectIdentificator,
                                                    RPCPacketType.getPacketTypeByValue(pType),
                                                    RPCOperationType.getByValue(operationType),
                                                    objectOId,
                                                    id,
                                                    dataSize,
                                                    hashH,
                                                    RPCPacketDataType.getByValue(dataType),
                                                    first);

                    parseRPCPacket(packet);
                } else if(neededSize>remainingSize){
                        Log.i("","Sorry you don't have enough space, take the second part of stream.");
                        getLastPacketRemainingSize = remainingSize;
                        if(bufferString==null){
                            bufferString = responseBody.substring(index, remainingSize);
                            Log.i("","BufferString IF : "+bufferString);
                        } else if(bufferString!=null){
                            bufferString = bufferString.concat(responseBody.substring(index, remainingSize));
                            Log.i("","BufferString ELSE : "+bufferString);
                        }
                            break X;
                }
    }catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }/* catch (StringIndexOutOfBoundsException e){
            e.printStackTrace();
    }*/
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:29:19+00:00Added an answer on May 26, 2026 at 11:29 am

    Your code snippet is rather hard to read. What’s RPCPacket? What’s bufferString? Are Log lines really essential to understand the logic?

    Do you really have to repeat the main loop once per every character of responseBody, which is what you’re doing now?

    Also, the whole notion of having response as String kinda smells unfriendly to binary data.

    EDIT: to get the first 32 bytes from a byte array, use:

    byte []Source;
    byte []First = Arrays.copyOf(Source, 32);
    

    And please don’t treat binary data as String. Corruption will be almost inevitable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little issue concerning an animation-effect which loads a certain div into
Hey guys I have a little issue here. I have a panel where I
i have little problem with boost::asio library. My app receive and process data asynchronously,
I have little question about how web browser retrieve webpage? I know this User
i have little dilemma, i often use data-bound controls such as Gridview in conjunction
I have a PHP file which produces an Xml sitemap based on data which
I have a little sample application I was working on trying to get some
I have a little issue with setting align of a button in my application.
I have a little issue with AlertDialog on my application. I'm showing an AlertDialog
I have a Javascript API, which should be usable with GWT and Flex. Using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.