Android-Droid
Android-Droid

Reputation: 14565

Android read large file line by line

I'm working on an application which is connection to the web server and receiving an response. I'm saving this response in txt file in internal memory of the device. Now I need to read the whole file line by line, because if I try to read the whole file it's throwing an OutofMemoryException. So for now I'm writing and reading the file with this code :

    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("http://www.rpc.probnata.com");


    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("debug_data","1"));


    httppost.setEntity(new UrlEncodedFormEntity(postParameters));

    HttpResponse response = httpclient.execute(httppost);
    Log.w("Response ","Status line : "+ response.getStatusLine().toString());
    buffer = EntityUtils.toByteArray(response.getEntity());
    FileOutputStream out = this.openFileOutput("response",this.MODE_PRIVATE);
    out.write(buffer);
    out.close();

Reading the file :

public void parseResponse(){
    try {
        File myDir = new File(getFilesDir().getAbsolutePath());

        BufferedReader br = new BufferedReader(new FileReader(myDir + "/response"));
        String line;
        while ((line = br.readLine()) != null) {
            Log.v("","line : "+line);
            handleDataFromSync(line);
        }
        br.close();



    } catch (Exception e){
        e.printStackTrace();
    }
}

This method parse the response.

public void handleDataFromSync(final String responseBody) {

        for(int index=0;index<responseBody.length();index++){
                Log.w("Response ","Response size : "+ responseBody.length());
                Log.w("","****************Index is : "+index);

                int objectIdentificator = 0;
                objectIdentificator = Integer.parseInt(responseBody.substring(index,index+packetFieldSizes[0]));  
                Log.w("Response ","Object Identificator (LONGINT) : "+ objectIdentificator);
                index = index+packetFieldSizes[0]; 
                Log.w("","****************Index is (must be 32) : "+index);

                String type = null;
                type = responseBody.substring(index,index + packetFieldSizes[1]);
                Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ type);
                short pType = Short.parseShort(type);
                Log.w("Response ","TYPE (UNSIGNED BYTE) : "+ pType);

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

                String operation=null;
                operation = responseBody.substring(index,index + packetFieldSizes[2]);
                short operationType = Short.parseShort(operation);
                Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operation);
                Log.w("Response ","OPERATION (UNSIGNED BYTE) : "+ operationType);
                index = index + packetFieldSizes[2];
                Log.w("","****************Index is (must be 38) : "+index);

                String objectId=null;
                objectId = responseBody.substring(index, index + packetFieldSizes[3]);
                Log.w("Response ","UID (CHAR, length 32) : "+ objectId);
                index = index + packetFieldSizes[3];
                Log.w("","****************Index is (must be 70) : "+index);

                int id=0;
                id = Integer.parseInt(responseBody.substring(index,index + packetFieldSizes[4]));
                Log.w("Response ","ID (LONGINT) : "+ responseBody.substring(index, index + packetFieldSizes[4]));
                Log.w("Response ","ID (LONGINT) : "+ id);
                index = index + packetFieldSizes[4];
                Log.w("","****************Index is (must be 102) : "+index);

                String size=null;
                size = responseBody.substring(index,index + packetFieldSizes[5]); 
                int dataSize = Integer.parseInt(size);
                Log.w("Response ","Data Size (LONGINT) : "+ dataSize);
                index = index + packetFieldSizes[5];
                Log.w("","****************Index is (must be 134) : "+index);

                String hash=null;
                hash = responseBody.substring(index,index + packetFieldSizes[6]);
                Log.w("Response ","Data Hash (CHAR, length 32 : "+ hash);
                index = index + packetFieldSizes[6];
                Log.w("","****************Index is (must be 166) : "+index);

                String  dType=null;
                dType = responseBody.substring(index,index + packetFieldSizes[7]);
                Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dType);
                short dataType = Short.parseShort(dType);
                Log.w("Response ","Data Type (UNSIGNED BYTE) : "+ dataType);
                index = index + packetFieldSizes[7];
                Log.w("","****************Index is (must be 169) : "+index);

                String data=null;
                data = responseBody.substring(index, index + dataSize);
                Log.w("Response ","Data (CHAR, any length, in BASE64) : "+ data);

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

So any idea how to read the next line, because the thing which this code is doing now is to read the first line and after that it's tryint to read the first line again.

Upvotes: 4

Views: 1731

Answers (1)

Carth
Carth

Reputation: 2343

Your bufferedReader implementation looks fine so my suspicion would be that there's something going on in your handleDataFromSync method. From the code I would infer that each responseBody is a string of indeterminate length but with repeating patterns of values at known positions. Potential culprits could be your packetFieldSizes[] which we can't inspect and the value of dataSize.

Validate that the index logged values are what you expect them to be and that you are actually making it out of your handleDataFromSync. If that's the case then there's something wrong with your bufferedReader that I didn't see and you'll need to add some additional logging to determine what's causing the failure at that step.

Upvotes: 2

Related Questions