What is really the correct way of reading files from the socket? Because the loop on reading the file doesn't end even though on the client side writing the files has been finished. I even tried printing the buffer position and length if I still data to be read.
Here is my code for reading the file.
private void readActualData(SocketChannel socketChannel) {
RandomAccessFile aFile = null;
System.out.println("Reading actual Data");
try {
aFile = new RandomAccessFile(path, "rw");
ByteBuffer buffer = ByteBuffer.allocate(50000000);
FileChannel fileChannel = aFile.getChannel();
int length;
while ((length = socketChannel.read(buffer)) >= 0 || buffer.position() > 0) {
buffer.flip();
fileChannel.write(buffer);
buffer.compact();
System.out.println("Length : "+length+" and Buffer position : "+buffer.position());
}
fileChannel.close();
System.out.println("End of file reached..Done Reading");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}