1

I have written a small client-server program, where server is running on an Android phone and client program is running on my PC. It works absolutely fine when I send small files from server to client (like small text files, doc files, etc). But when I try to send larger files (like mp3 of size 2 to 3 MB), the client program throws error due to array size overflow.

Here is my client side program running on my PC:

 package fileClient;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

public class myFileClient {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        int filesize=1022386;
        int bytesRead;
        int currentTot = 0;
        String servAdd="10.142.100.161";
        Socket socket = null;
        InetAddress serverIP=InetAddress.getByName(servAdd);
        byte [] bytearray  = new byte [filesize];

        socket=new Socket(serverIP, 4444);
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("//home//evinish//MANA.mp3");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(bytearray,0,bytearray.length);
        currentTot = bytesRead;

        do {
            bytesRead =
               is.read(bytearray, currentTot, (bytearray.length-currentTot));
            if(bytesRead >= 0) currentTot += bytesRead;
         } while(bytesRead > -1);

         bos.write(bytearray, 0 , currentTot);
         bos.flush();
         bos.close();
         socket.close();
    }

}

The console output shows following error:

 /usr/lib/jvm/java-6-openjdk-amd64/bin/java: line 3: [: too many arguments
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
    at fileClient.myFileClient.main(myFileClient.java:38)

I tried to increase filesize variable to 100MB or so, but that didn't work either. Can anybody tell me a work around for this?

Vinit Shandilya
  • 1,643
  • 5
  • 24
  • 44

0 Answers0