I know how to read a file by bytes but cannot find a example how to read it in chunks of bytes. I have a byte array, and i want to read the file by 512bytes and send them over a socket.
I have tried by reading total bytes of file and then subtracting 512 bytes until i got a chunk that was less than 512 bytes and signaled EOF and end of transfer.
I am trying to implement a TFTP, where data is sent in 512 byte chunks.
Anyhow would be thankful for a example.
Using
InputStream
you can read in an array of given size and limit the reading to this size.Read here: http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
You ... read 512 bytes at a time.
You can use the appropriate
read()
method from the input stream, for exampleFileInputStream
supports aread(byte[])
to read a chunk of bytes.something like: You may want to wrap the input stream in a
BufferedInputStream
if you wanted to guarantee 512 byte blocks (the constructor takes a block size argument).