0

I am using RetrieveFilestream method with BufferedInputStream in a for loop. I am closing all streams after processing each file and also adding ftp complete pending command.

Every thing works as expected in my test environment with few files. But in realtime data where there are 200-300 files, it hangs somewhere.

It is not throwing any exception making it difficult to debug. Cannot debug one by one. Any help? Here is my code Block.

     public String LoopThroughFiles(FTPClient myftp, String DirectoryName)
                 {
                     boolean flag=false;
                     String output="";

                      InputStream inStream=null;


                      BufferedInputStream bInf= null;

                      StringBuilder mystring = new StringBuilder();
                      progressBar = (ProgressBar) findViewById(R.id.progressBar);    


                     try {
                          flag= myftp.changeWorkingDirectory(DirectoryName);

                        if(flag==true)
                        {
                         FTPFile[] files = myftp.listFiles();

                         progressBar.setMax(files.length);


                         String fname="";

                         myftp.enterLocalPassiveMode();

                        if(files.length > 0) 
                        {

                             int n=0;

                            for (FTPFile file : files)  
                            {


                                 n=n+1;
                                int r= progressBar.getProgress();
                                progressBar.setProgress(r+n);


                               fname=file.getName();
                              // String path= myftp.printWorkingDirectory();

                              if(fname.indexOf("txt") != -1)
                              { 

                                inStream =  myftp.retrieveFileStream(fname);

                                int reply = myftp.getReplyCode();

                                if (inStream == null   || (!FTPReply.isPositivePreliminary(reply) && !FTPReply.isPositiveCompletion(reply)))  {Log.e("error retrieving file",myftp.getReplyString()); }


                             bInf=new BufferedInputStream (inStream);
                              int bytesRead;
                             byte[] buffer=new byte[1024]; 
                             String fileContent=null; 


                           while((bytesRead=bInf.read(buffer))!=-1)
                           {
                               fileContent=new String(buffer,0,bytesRead);
                               mystring.append(fileContent);

                           }

                            mystring.append(",");

                             bInf.close();

                             inStream.close();

                              boolean isSucess= myftp.completePendingCommand();  
                              if(isSucess == false)
                                  Log.e("error retrieving file","Failed to retrieve the stream for " + fname);

                              }

                            }


                          flag= myftp.changeToParentDirectory(); 

                          }
                        }

                    }
                     catch (java.net.UnknownHostException e) {  
                          e.printStackTrace();  
                          Log.e("readfile,UnknownHost",e.getMessage());

                        }  
                        catch (java.io.IOException e) {  
                          e.printStackTrace();  
                          Log.e("readfile,IO",e.getMessage());
                        }  
                       catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                         Log.e("readfile,General",e.getMessage());
                       } 
                     finally
                     {
                         try {

                          output = mystring.toString();

                          if(bInf != null)
                              bInf.close();

                          if(inStream != null)
                            inStream.close();


                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                                 Log.e("readfile,finallyblock",e.getMessage());
                            }
                     }

                     return output;
                 }
Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47

0 Answers0