Каков на самом деле правильный способ чтения файлов из сокета? Потому что цикл чтения файла не заканчивается, хотя на стороне клиента запись файлов завершена. Я даже пытался распечатать позицию и длину буфера, если мне все еще нужно прочитать данные.
Вот мой код для чтения файла.
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();
}
}