2

I read a tutorial 'Read file in String with BufferedInputStream', and I got a code from here:

http://examples.javacodegeeks.com/core-java/io/bufferedinputstream/read-file-in-string-with-bufferedinputstream/

The question is this line: // byte array to store input byte[] contents = new byte[1024];

So, how can I ensure it is 1024 byte? If I have a data with 1025 byte, and my code will break. So, how can I make it more generic? Thanks.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195

2 Answers2

1

In the code that you are referring to the array is filled by calling

byte[] contents = new byte[1024];
int bytesRead=0;
bytesRead = bin.read(contents));

bin.read will see the size of contents and read at most 1024 bytes from the stream.

bennihepp
  • 262
  • 1
  • 11
0

No,It won't break.

Magic is here

while ((bytesRead = bin.read(contents)) != -1) {

bin.read(contents)) It will read the next set of bytes from the file.

Question asked by me : How the buffer byte array is continuously filling while streaming?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307