title: Server returning illegal capacity -1
body: <p>here im gonna post a perfect example of sending and receiving images,musics, videos or anything through sockets so who ever needs can use it. its almost perfect. it does the job, no matter how many files you have in the target it counts them and sends them to the java server, but atlast im receiving an exception on the server side :</p>
  
  <pre><code> Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
  at java.util.ArrayList.&lt;init&gt;(Unknown Source)
  </code></pre>
  
  <p>i really dont know what is that,but i appreciate if you help so i compelete this for myself and any one else who finds himself in need of one. thanks</p>
  
  <p>Android Cliend : </p>
  
  <pre><code> package com.example.imagesender;
  
  //File imagefile = new File(filepath);
  //  FileInputStream fis = null;
  //       try {
  //           fis = new FileInputStream(imagefile);
  //      } catch (FileNotFoundException e) {
   //          System.out.println("file not found");
   //        e.printStackTrace();
   //    }
  
   // Bitmap bm = BitmapFactory.decodeStream(fis);
  //  imgbyte = new byte [(int)filepath.length()];
  //   imv.setImageBitmap(bm);
  //  Log.d("ClientActivity","length:"+imgbyte);
  
  
  
  
  public class AccountCreator extends Activity {  
      private Button send;  
  
      private Socket socket;  
      private File f,fdst;  
      private FileInputStream fin,fises;  
      private static Context context;  
  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.mes_registerpage);  
          send=(Button)findViewById(R.id.sendpic);  
        //  sendWan=(Button)findViewById(R.id.sendWan);  
          AccountCreator.context = getApplicationContext();  
  
          send.setOnClickListener(new OnClickListener(){  
  
              public void onClick(View view){  
  
                  this.doit();
              }
                  class myAsync extends AsyncTask&lt;String, Boolean, Boolean&gt;{
  
  
                                   @Override
                      protected Boolean doInBackground(String...urls){    
                  try {  
                      socket = new Socket("10.0.2.2", 1500);  
                      System.out.println(socket);  
                      System.out.println("Connecting...");  
  
  
                      File fil=new 
   File(Environment.getExternalStorageDirectory()+"/Pictures/");  
                        System.out.println(fil);
                      File[] Files=fil.listFiles();  
                      System.out.println(Files);
                      for (int count=0;count &lt; Files.length;count ++){  
                          System.out.println(Files[count].getName());  
  
                      }  
  
                      OutputStream os = socket.getOutputStream();    
                      DataOutputStream dos = new DataOutputStream(os);   
  
                      dos.writeInt(Files.length);  
  
                      for (int count=0;count&lt;Files.length;count ++){  
                            dos.writeUTF(Files[count].getName());  
  
                      }  
                      for (int count=0;count&lt;Files.length;count ++){  
  
                            int filesize = (int) Files[count].length();  
                            dos.writeInt(filesize);  
                      }  
  
                      for (int count=0;count&lt;Files.length;count ++){  
  
                      int filesize = (int) Files[count].length();  
                      byte [] buffer = new byte [filesize];  
  
                      FileInputStream fis = new  
   FileInputStream(Files[count].toString());    
                      BufferedInputStream bis = new BufferedInputStream(fis);    
  
                      //Sending file name and file size to the server    
                      bis.read(buffer, 0, buffer.length); //This line is important  
                      dos.write(buffer, 0, buffer.length);     
                      dos.flush();   
  
                      //close socket connection  
                      //socket.close();  
  
                  }  
  
                             //  Toast.makeText(getApplicationContext(),"Transfer file 
  is completed!!", Toast.LENGTH_LONG).show();   
                       socket.close();  
                  }  
                  catch(Exception e){  
                      System.out.println("Error::"+e);  
                      //System.out.println(e.getMessage());  
                      //e.printStackTrace();  
                      //Log.i("******* :( ", "UnknownHostException");  
                  }
                  return null;
  
              }  
                  }
  
  
  
                  protected void onPostExecute(String result){
                  }
  
  
  public void doit(){
  myAsync sync = new myAsync();
  sync.execute();
  }
          });}
  
      }  
  </code></pre>
  
  <p>and the server side:</p>
  
  <pre><code> public class Server {  
  
      /** 
       * @param args 
       */  
      public static void main(String[] args) throws IOException,EOFException {  
          // TODO Auto-generated method stub  
       FileOutputStream fos;  
       BufferedOutputStream bos;  
       OutputStream output;  
       DataOutputStream dos;  
       int len;  
       int smblen;   
       InputStream in;  
       DataInputStream clientData;  
       BufferedInputStream clientBuff;  
  
  
      ServerSocket serverSocket = new ServerSocket(1500);  
      Socket clientSocket = null;  
      clientSocket = serverSocket.accept();  
  
  
          in = clientSocket.getInputStream(); //used    
  
         clientData = new DataInputStream(in); //use    
         clientBuff = new BufferedInputStream(in); //use    
        int N=1;
           while(N==1){    
  
              System.out.println("Starting...");    
  
                 int fileSize = clientData.read();        
                          fileSize = (fileSize &gt; 0) ? fileSize:0;      
  
  
                          List&lt;File&gt; files = new ArrayList&lt;&gt;(fileSize);          
                          List&lt;Integer&gt; sizes = new ArrayList&lt;&gt;(fileSize);
  
                  for (int count=0;count &lt; fileSize;count ++){  
  
                          sizes.add(clientData.readInt());  
                  }  
  
                 for (int count =0;count &lt; fileSize ;count ++){     
  
                    len=sizes.get(count);  
  
                  System.out.println("File Size ="+len);  
  
                  //output = new FileOutputStream("C:/share/" + fileName);  
                  output = new FileOutputStream("D://Users/" + files.get(count));  
                  dos=new DataOutputStream(output);  
                  bos=new BufferedOutputStream(output);  
  
                  byte[] buffer = new byte[1024];    
  
                  bos.write(buffer, 0, buffer.length); //This line is important  
  
                  while (len &gt; 0 &amp;&amp; (smblen = clientData.read(buffer)) &gt; 0) {   
                      dos.write(buffer, 0, smblen);   
                        len = len - smblen;  
                        dos.flush();  
                      }    
              N=2;
                }   
         }  
  
        } //end loop   
  
   }  
  </code></pre>
  
  <p>thanks in advance</p>
  
  <p>the stacktrace:</p>
  
  <pre><code> Exception in thread "main" java.lang.IllegalArgumentException: Illegal Capacity: -1
  at java.util.ArrayList.&lt;init&gt;(Unknown Source)
  at Server.main(Server.java:49)
  </code></pre>