Skip to content

Instantly share code, notes, and snippets.

@gaul
Last active December 22, 2015 07:39
Show Gist options
  • Save gaul/6439757 to your computer and use it in GitHub Desktop.
Save gaul/6439757 to your computer and use it in GitHub Desktop.
Demonstrate how to call setFixedLengthStreamingMode with an int argument in Java 6 and with a long argument in Java 7.
/**
* Demonstrate how to call setFixedLengthStreamingMode with an int argument in
* Java 6 and with a long argument in Java 7.
*/
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
public class SetFixedLengthStreamingMode {
private static final Method METHOD;
static {
Method method;
try {
method = HttpURLConnection.class.getMethod(
"setFixedLengthStreamingMode", long.class);
} catch (NoSuchMethodException nsme) {
try {
method = HttpURLConnection.class.getMethod(
"setFixedLengthStreamingMode", int.class);
} catch (NoSuchMethodException nsme2) {
throw new RuntimeException(nsme2);
}
}
METHOD = method;
}
public static void main(String[] args) throws Exception {
URL url = new URL("http://google.com/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
METHOD.invoke(conn, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment