ADDITIONAL SYSTEM INFORMATION :
> javac -version
javac 11.0.22
> sw_vers
ProductName: macOS
ProductVersion: 14.4.1
BuildVersion: 23E224
A DESCRIPTION OF THE PROBLEM :
When reading data from an HTTP server using HttpURLConnection#HttpInputStream, terminating the HTTP server does not result in an IOException being thrown by the read() method. It ends up reading a partial subset of the file without throwing any error. This behavior is unexpected and can lead to data integrity issues, and makes it difficult to identify if the end of file has actually been reached or if we've read a subset of the file.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In the attached source code, kill the HTTP server at the `inputStream.read(buffer)` step, and observe the value of totalBytesRead printed. It would be less than the size of the entire file. Despite this, it doesn't throw any exception, and the program finishes with exit code 0.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When the HTTP server is killed unexpected, the read() method should throw an IOException instead of returning -1, as -1 indicates that the end of file had been reached, which is incorrect.
ACTUAL -
When the HTTP server is killed, read() method returns -1 instead of throwing an IOException.
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpInputStream1
{
public static void main(String[] args) {
readUsingHttpInputStream();
}
public static void readUsingHttpInputStream() {
String urlString = "http://localhost:9333/filename.json";
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = urlConnection.getInputStream();
int bytesRead;
byte[] buffer = new byte[1024];
int totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
inputStream.close();
System.out.println("totalBytesRead = " + totalBytesRead);
} else {
System.out.println("Failed to fetch the file. HTTP response code: " + responseCode);
}
urlConnection.disconnect();
} catch (IOException e) {
System.out.println("e = " + e);
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
FREQUENCY : always
> javac -version
javac 11.0.22
> sw_vers
ProductName: macOS
ProductVersion: 14.4.1
BuildVersion: 23E224
A DESCRIPTION OF THE PROBLEM :
When reading data from an HTTP server using HttpURLConnection#HttpInputStream, terminating the HTTP server does not result in an IOException being thrown by the read() method. It ends up reading a partial subset of the file without throwing any error. This behavior is unexpected and can lead to data integrity issues, and makes it difficult to identify if the end of file has actually been reached or if we've read a subset of the file.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
In the attached source code, kill the HTTP server at the `inputStream.read(buffer)` step, and observe the value of totalBytesRead printed. It would be less than the size of the entire file. Despite this, it doesn't throw any exception, and the program finishes with exit code 0.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
When the HTTP server is killed unexpected, the read() method should throw an IOException instead of returning -1, as -1 indicates that the end of file had been reached, which is incorrect.
ACTUAL -
When the HTTP server is killed, read() method returns -1 instead of throwing an IOException.
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpInputStream1
{
public static void main(String[] args) {
readUsingHttpInputStream();
}
public static void readUsingHttpInputStream() {
String urlString = "http://localhost:9333/filename.json";
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = urlConnection.getInputStream();
int bytesRead;
byte[] buffer = new byte[1024];
int totalBytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
}
inputStream.close();
System.out.println("totalBytesRead = " + totalBytesRead);
} else {
System.out.println("Failed to fetch the file. HTTP response code: " + responseCode);
}
urlConnection.disconnect();
} catch (IOException e) {
System.out.println("e = " + e);
e.printStackTrace();
}
}
}
---------- END SOURCE ----------
FREQUENCY : always