I am studying Android development (I am new to programming in general) and found out about the HTTP network and saw this code in the lesson:
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
I do not understand what InputStream, InputStreamReader and BufferedReader do. They all have a read () method, as well as readLine () in the case of BufferedReader. Why can't I use InputStream or add only InputStreamReader? Why do I need to add a BufferedReader? I know that this is related to efficiency, but I don’t understand how to do it.
I researched and the documentation for BufferedReader is trying to explain this, but I still don't understand who is doing what:
, , Reader, , . BufferedReader Reader read() , FileReaders InputStreamReaders. ,
BufferedReader in = new BufferedReader(new FileReader("foo.in")); . read() readLine() , , , , .
, , InputStream , InputStreamReader - , BufferedReader - - , . , , , .
, , , , . , , , , : Q1, Q2, Q3, Q4. , . , .
, BufferedReader readLine() readStream InputStreamReader, readStream read()? InputStream , int, , InputStreamReader , int , BufferedReader , ? , , ? , , .
!