int bytesAvailable;
FileInputStream fileInputStream = new FileInputStream(new File("somewhere"));
bytesAvailable = fileInputStream.available();
int maxBufferSize = 100;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// // read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
While studying HttpURLConnection
, there is a part like this, but I can't find a clear answer even if I search for it, so I'm asking you a question!
I wonder what byteAvailable
means and the role of buffer
in FileInputStream
here!!
In a stream class, the available method usually refers to the number of data that can be read, and buffer refers to the variable that contains the data.
// Check the number of data that can be read in the stream.
bytesAvailable = fileInputStream.available();
// The number of data that can be read from the stream and
// I'll compare the maximum number of readings I can read
// Choose the smaller number of the two as the buffer size.
bufferSize = Math.min(bytesAvailable, maxBufferSize);
// Set array variables for data
buffer = new byte[bufferSize];
// BytesRead: Get results on how much you read.
// read() : 0th index of buffer array to bufferSize
// Place the data in the buffer array.
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
© 2024 OneMinuteCode. All rights reserved.