I would like to transfer a video file read from disk to a receiver using UDP DatagramPacket in Java.
The key points are as follows: though the file to be transmitted is read from disk, I should assume that I'm not aware of the file size. The total file need to be read incrementally and the datagram packets of the file should be of varying sizes.
In short, I should assume that I'm streaming a live video to a receiver without knowing how much data I would totally need to send and at what rate the data will get generated.
Right now, I have a code which reads the file on the disk at once and convert into datagram packets to transmit using UDP socket. But I have no clue on how could I packetize the file without knowing the original file size and read incrementally that would mock the live streaming of a video.
Any inputs on to get this idea implemented using Java would be very useful. I need to implement a real time file transfer protocol once I get this basic thing working.
Sender:
File file = new File("/crazy.mp4");
FileInputStream fis = new FileInputStream(file);
DatagramPacket pack;
int size = 0;
byte[] buffer = new byte[((int) file.length())];
ByteBuffer bb = ByteBuffer.allocate(4);
bb.order(ByteOrder.BIG_ENDIAN);
while (true) {
size = fis.read(buffer);
pack = new DatagramPacket(buffer, buffer.length, address,
packet.getPort());
socket.send(pack);
}
Receiver:
File file = new File("/crazyRecv.mp4");
FileOutputStream fos = new FileOutputStream(file);
DatagramPacket rpacket = new DatagramPacket(buffer, buffer.length);
while (true) {
socket.receive(rpacket);
fos.write(rpacket.getData(), 0, rpacket.getLength());
}
Thanks.