Example usage for org.apache.commons.httpclient ChunkedOutputStream finish

List of usage examples for org.apache.commons.httpclient ChunkedOutputStream finish

Introduction

In this page you can find the example usage for org.apache.commons.httpclient ChunkedOutputStream finish.

Prototype

public void finish() throws IOException 

Source Link

Usage

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

@Override
protected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException {
    InputStream inputStream;//  w w  w . j  a v  a  2s  .  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;/*from   ww w. j  a v  a2s  . 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.wink.itest.contentencoding.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * //from w  ww  .  ja  v a  2 s  .co m
 * @throws HttpException
 * @throws IOException
 */
public void testSendSmallGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook");

    postMethod.addRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    postMethod.setRequestHeader("Transfer-Encoding", "chunked");
    // postMethod.setRequestHeader("Content-)
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    originalContent.write("Hello world".getBytes("UTF-8"));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos);
    GZIPOutputStream gzipOut = new GZIPOutputStream(chunkedOut);

    originalContent.writeTo(gzipOut);

    gzipOut.finish();
    chunkedOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        String response = postMethod.getResponseBodyAsString();
        assertEquals("Hello world" + "helloworld", response);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.contentencoding.WinkContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from www.ja  va  2 s .co m*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendSmallGzipContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    originalContent.write("Hello world".getBytes("UTF-8"));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ChunkedOutputStream chunkedOut = new ChunkedOutputStream(baos);
    GZIPOutputStream gzipOut = new GZIPOutputStream(chunkedOut);

    originalContent.writeTo(gzipOut);

    gzipOut.finish();
    chunkedOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook").accept(MediaType.TEXT_PLAIN)
            .header("Transfer-Encoding", "chunked").header("Content-Encoding", "gzip")
            .contentType("text/plain; charset=utf-8").post(content);
    assertEquals(200, response.getStatusCode());
    String responseBody = response.getEntity(String.class);
    assertEquals("Hello world" + "helloworld", responseBody);
}