Java - HttpURLConnection setFixedLengthStreamingMode()
The Java HttpURLConnection setFixedLengthStreamingMode() method enables streaming of a HTTP request body without internal buffering, when the content length is known in advance.
An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount.
Declaration
Following is the declaration for java.net.HttpURLConnection.setFixedLengthStreamingMode() method
public void setFixedLengthStreamingMode(int contentLength)
Parameters
contentLength − The number of bytes which will be written to the OutputStream.
Return Value
NA
Exception
IllegalStateException − if URLConnection is already connected or if a different streaming mode is already enabled.
IllegalArgumentException − if a content length less than zero is specified.
Example 1
The following example shows the usage of Java HttpURLConnection setFixedLengthStreamingMode() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index1.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setDoOutput(true);
urlConnection.connect();
System.out.println("Connected.");
if(urlConnection.getInputStream() != null) {
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String content = "";
String current;
while((current = in.readLine()) != null) {
content += current;
}
System.out.println(content);
}else {
System.out.println("Input Stream is null");
}
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Connected. java.io.IOException: insufficient data written at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)
Example 2
The following example shows the usage of Java HttpURLConnection setFixedLengthStreamingMode() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.tutorialspoint.com/index1.htm?language=en#j2se");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setDoOutput(true);
urlConnection.connect();
System.out.println("Connected.");
if(urlConnection.getInputStream() != null) {
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String content = "";
String current;
while((current = in.readLine()) != null) {
content += current;
}
System.out.println(content);
}else {
System.out.println("Input Stream is null");
}
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Connected. java.io.IOException: insufficient data written at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)
Example 3
The following example shows the usage of Java HttpURLConnection setFixedLengthStreamingMode() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInputStream(), we're getting the content of the website home page and printing the same −
package com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setFixedLengthStreamingMode(1000);
urlConnection.setDoOutput(true);
urlConnection.connect();
System.out.println("Connected.");
if(urlConnection.getInputStream() != null) {
BufferedReader in = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String content = "";
String current;
while((current = in.readLine()) != null) {
content += current;
}
System.out.println(content);
}else {
System.out.println("Input Stream is null");
}
urlConnection.disconnect();
System.out.println("Disconnected.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
Connected. java.io.IOException: insufficient data written at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.close(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at com.tutorialspoint.HttpUrlConnectionDemo.main(HttpUrlConnectionDemo.java:18)