I got working server and client applications, they work perfect while sending small files, but when I try to send for example movie file that is 700mb over socket it gives me
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
I searched the internet and found some tutorials on sending large files, but couldn't quite understand them, but I think my porblem is in writing file.
This is the code that server uses to write my file:
output = new FileOutputStream(directory + "/" + fileName);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
And here is the code that my client uses to send a file:
byte[] fileLength = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(fileLength, 0, fileLength.length);
OutputStream os = socket.getOutputStream();
//Sending size of file.
DataOutputStream dos = new DataOutputStream(os);
dos.writeLong(fileLength.length);
dos.write(fileLength, 0, fileLength.length);
dos.flush();
socket.close();