Java DataInputStream Length

I am creating a file server application for a school assignment. I currently have a simple Client class that sends an image via TCP and a Server class that receives and writes it to a file.

this is my client code

 import java.io.*; import java.net.*; class Client { public static void main(String args[]) throws Exception { long start = System.currentTimeMillis(); Socket clientSocket = new Socket("127.0.0.1", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); File file = new File("hot.jpg"); FileInputStream fin = new FileInputStream(file); byte sendData[] = new byte[(int)file.length()]; fin.read(sendData); outToServer.write(sendData, 0, sendData.length); clientSocket.close(); long end = System.currentTimeMillis(); System.out.println("Took " + (end - start) + "ms"); } } 

and this is my server code.

 import java.io.*; import java.net.*; class Server { public static void main(String args[]) throws Exception { ServerSocket serverSocket = new ServerSocket(6789); Socket connectionSocket = serverSocket.accept(); DataInputStream dis = new DataInputStream(connectionSocket.getInputStream()); byte[] receivedData = new byte[61500]; // <- THIS NUMBER for(int i = 0; i < receivedData.length; i++) receivedData[i] = dis.readByte(); connectionSocket.close(); serverSocket.close(); FileOutputStream fos = new FileOutputStream("received.jpg"); fos.write(receivedData); fos.close(); } } 

My question is how to get the size of the uploaded file. If you check the Server code, you will see that I currently hardcoded the number ie 61500. How can I get this number dynamically?

Or am I doing it wrong? What will be the alternative solution?

+4
source share
2 answers

Before sending a file, add one length field. (Note that since you are reading a file in memory, the maximum file size may be ~ 2 GB.)


Before sending the file, write the file length:

  outToServer.writeInt(sendData.length); 

And when you first read the length and use it as the length:

  int dataLength = dis.readInt() byte[] receivedData = new byte[dataLength]; 

It would be best not to read the file in memory first, but to transfer it directly from FileInputStream - then you could transfer large files!

+6
source

If you know the length, using readFully () is much more efficient than reading a byte at a time.

In this case, you do not need to know the length, you can write a cycle to read / write as much data as possible.

 InputStream is = connectionSocket.getInputStream(); byte[] bytes = new byte[8192]; int len; while((len = is.read(bytes)) > 0) fos.write(bytes, 0, len); 

You can not read the entire file in memory by copying data while reading.

 FileInputStream fis = new FileInputStream(filename); OutputStream os = socket.getOutputStream(); byte[] bytes = new byte[8192]; int len; while((len = fis.read(bytes)) > 0) os.write(bytes, 0, len); 

You can use Apache IOUtils .copy () to copy from one stream to another if you want.

This approach has the advantage that the file can be of any size (over 2 GB). Array usage is limited to 2 GB (and uses more memory)

+2
source

All Articles