Example usage for org.apache.commons.httpclient HttpConnection getRequestOutputStream

List of usage examples for org.apache.commons.httpclient HttpConnection getRequestOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpConnection getRequestOutputStream.

Prototype

public OutputStream getRequestOutputStream() throws IOException, IllegalStateException 

Source Link

Document

Returns an OutputStream suitable for writing the request.

Usage

From source file:com.eucalyptus.blockstorage.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;//w  ww  .j  a va 2  s .c o m
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            callback.update(totalBytesProcessed);
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:edu.ucsb.eucalyptus.cloud.ws.PutMethodWithProgress.java

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;/*  ww w . j  a v  a  2 s . c o  m*/
    if (outFile != null) {
        inputStream = new FileInputStream(outFile);

        ChunkedOutputStream chunkedOut = new ChunkedOutputStream(conn.getRequestOutputStream());
        byte[] buffer = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
        int bytesRead;
        int numberProcessed = 0;
        long totalBytesProcessed = 0;
        while ((bytesRead = inputStream.read(buffer)) > 0) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            GZIPOutputStream zip = new GZIPOutputStream(out);
            zip.write(buffer, 0, bytesRead);
            zip.close();
            chunkedOut.write(out.toByteArray());
            totalBytesProcessed += bytesRead;
            if (++numberProcessed >= callback.getUpdateThreshold()) {
                callback.run();
                numberProcessed = 0;
            }
        }
        if (totalBytesProcessed > 0) {
            callback.finish();
        } else {
            callback.failed();
        }
        chunkedOut.finish();
        inputStream.close();
    } else {
        return false;
    }
    return true;
}

From source file:org.apache.webdav.lib.methods.HttpRequestBodyMethodBase.java

/**
 * Do write the request body./*from  w w  w.j  a va2s.c om*/
 * Override the method of {@link HttpMethodBase}
 * if the method should wait until a <tt>100 Continue</tt> status code
 * is expected (@link readContinueCode)
 *
 * @since 2.0
 */
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException {
    OutputStream out = conn.getRequestOutputStream();
    if (isHttp11() && (null == getRequestHeader("Content-Length"))) {
        out = new ChunkedOutputStream(out);
    }

    InputStream inputStream = null;
    if (file != null && file.exists()) {
        inputStream = new FileInputStream(file);
    } else if (url != null) {
        inputStream = url.openConnection().getInputStream();
    } else if (data != null) {
        inputStream = new ByteArrayInputStream(data);
    } else {
        return true;
    }

    byte[] buffer = new byte[4096];
    int nb = 0;
    while (true) {
        nb = inputStream.read(buffer);
        if (nb == -1) {
            break;
        }
        out.write(buffer, 0, nb);
    }
    out.flush();
    return true;
}