Ignore:
Timestamp:
2015-12-27T16:02:24+01:00 (5 years ago)
Author:
Don-vip
Message:

see #12231 - add support for setFixedLengthStreamingMode (used in DirectUpload plugin)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HttpClient.java

    r9177 r9178  
    99import java.io.InputStream;
    1010import java.io.OutputStream;
     11import java.net.HttpRetryException;
    1112import java.net.HttpURLConnection;
    1213import java.net.URL;
     
    3637    private byte[] requestBody;
    3738    private long ifModifiedSince;
     39    private long contentLength;
    3840    private final Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    3941    private int maxRedirects = Main.pref.getInteger("socket.maxredirects", 5);
     
    6163        if (ifModifiedSince > 0) {
    6264            connection.setIfModifiedSince(ifModifiedSince);
     65        }
     66        if (contentLength > 0) {
     67            connection.setFixedLengthStreamingMode(contentLength);
    6368        }
    6469        connection.setUseCaches(useCache);
     
    158163         * {@code Content-Disposition}
    159164         * @return {@code this}
     165         * @since 9172
    160166         */
    161167        public Response uncompressAccordingToContentDisposition(boolean uncompressAccordingToContentDisposition) {
     
    168174         * @return the URL
    169175         * @see HttpURLConnection#getURL()
     176         * @since 9172
    170177         */
    171178        public URL getURL() {
     
    177184         * @return the HTTP request method
    178185         * @see HttpURLConnection#getRequestMethod()
     186         * @since 9172
    179187         */
    180188        public String getRequestMethod() {
     
    260268         *
    261269         * @see HttpURLConnection#getResponseMessage()
     270         * @since 9172
    262271         */
    263272        public String getResponseMessage() {
     
    294303         * @return the value of the named header field, or {@code null} if there is no such field in the header
    295304         * @see HttpURLConnection#getHeaderField(String)
     305         * @since 9172
    296306         */
    297307        public String getHeaderField(String name) {
     
    304314         * @return unmodifiable List of Strings that represents the corresponding field values
    305315         * @see HttpURLConnection#getHeaderFields()
     316         * @since 9172
    306317         */
    307318        public List<String> getHeaderFields(String name) {
     
    353364     * @see #create(URL)
    354365     * @see #create(URL, String)
     366     * @since 9172
    355367     */
    356368    public URL getURL() {
     
    362374     * @return the HTTP request method
    363375     * @see #create(URL, String)
     376     * @since 9172
    364377     */
    365378    public String getRequestMethod() {
     
    371384     * @param header HTTP header name
    372385     * @return HTTP header value
     386     * @since 9172
    373387     */
    374388    public String getRequestHeader(String header) {
     
    420434     * @param readTimeout an {@code int} that specifies the read timeout value in milliseconds
    421435     * @return {@code this}
    422      * @see HttpURLConnection#setReadTimeout(int) (int)
     436     * @see HttpURLConnection#setReadTimeout(int)
    423437     */
    424438    public HttpClient setReadTimeout(int readTimeout) {
    425439        this.readTimeout = readTimeout;
     440        return this;
     441    }
     442
     443    /**
     444     * This method is used to enable streaming of a HTTP request body without internal buffering,
     445     * when the content length is known in advance.
     446     * <p>
     447     * An exception will be thrown if the application attempts to write more data than the indicated content-length,
     448     * or if the application closes the OutputStream before writing the indicated amount.
     449     * <p>
     450     * When output streaming is enabled, authentication and redirection cannot be handled automatically.
     451     * A {@linkplain HttpRetryException} will be thrown when reading the response if authentication or redirection
     452     * are required. This exception can be queried for the details of the error.
     453     *
     454     * @param contentLength The number of bytes which will be written to the OutputStream
     455     * @return {@code this}
     456     * @see HttpURLConnection#setFixedLengthStreamingMode(long)
     457     * @since 9178
     458     */
     459    public HttpClient setFixedLengthStreamingMode(long contentLength) {
     460        this.contentLength = contentLength;
    426461        return this;
    427462    }
     
    500535     * @param reasonForRequest Reason to show
    501536     * @return {@code this}
     537     * @since 9172
    502538     */
    503539    public HttpClient setReasonForRequest(String reasonForRequest) {
Note: See TracChangeset for help on using the changeset viewer.