Example usage for java.io ByteArrayOutputStream writeTo

List of usage examples for java.io ByteArrayOutputStream writeTo

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream writeTo.

Prototype

public synchronized void writeTo(OutputStream out) throws IOException 

Source Link

Document

Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count) .

Usage

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

/**
 * Tests sending in small bits of gzip encoded content.
 * /*  w w  w  . 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);
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * //w  ww  .ja v a  2 s . c om
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook/mirror").accept(MediaType.TEXT_PLAIN)
            .header("Content-Encoding", "gzip").contentType("text/plain; charset=utf-8").post(content);

    assertEquals(200, response.getStatusCode());
    InputStream responseStream = response.getEntity(InputStream.class);
    for (int c = 0; c < 5000000; ++c) {
        assertEquals(c % 256, responseStream.read());
    }
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * //from w w  w.  j a  v a  2 s .c o m
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    ClientResponse response = client.resource(BASE_URI + "/bigbook/mirror").header("Accept-Encoding", "gzip")
            .accept(MediaType.TEXT_PLAIN).header("Content-Encoding", "gzip")
            .contentType("text/plain; charset=utf-8").post(content);

    assertEquals(200, response.getStatusCode());
    InputStream responseStream = new GZIPInputStream(response.getEntity(InputStream.class));
    for (int c = 0; c < 5000000; ++c) {
        assertEquals(c % 256, responseStream.read());
    }
}

From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java

private <T> void serialize(final T value, final Serializer<T> serializer, final DataOutputStream dos)
        throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(value, baos);//from  w  w w.j  a  v  a2s  .  c  o  m
    dos.writeInt(baos.size());
    baos.writeTo(dos);
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * /*ww w  .  j ava  2  s .c om*/
 * @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.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * /*from w  ww .  j  a v  a2s .  c o  m*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.addRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    // postMethod.setRequestHeader("Content-)
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = postMethod.getResponseBodyAsStream();
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

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

/**
 * Tests sending in small bits of gzip encoded content.
 * //from  w  ww.j  av  a 2s .com
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.setRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Accept-Encoding", "gzip");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = new GZIPInputStream(postMethod.getResponseBodyAsStream());
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.kuali.mobility.people.controllers.PeopleController.java

@RequestMapping(value = "/image/{hash}", method = RequestMethod.GET)
public void getImage(@PathVariable("hash") String imageKeyHash, Model uiModel, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    byte[] byteArray = (byte[]) request.getSession().getAttribute("People.Image.Email." + imageKeyHash);
    if (byteArray != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
        baos.write(byteArray);/*from  w w  w  .  jav a  2s .com*/
        if (baos != null) {
            ServletOutputStream sos = null;
            try {
                response.setContentLength(baos.size());
                sos = response.getOutputStream();
                baos.writeTo(sos);
                sos.flush();
            } catch (Exception e) {
                LOG.error("error creating image file", e);
            } finally {
                try {
                    baos.close();
                    sos.close();
                } catch (Exception e1) {
                    LOG.error("error closing output stream", e1);
                }
            }
        }
    }
}

From source file:biz.webgate.dominoext.poi.component.kernel.CSVProcessor.java

public void generateNewFile(UICSV csvDef, HttpServletResponse httpResponse, FacesContext context) {

    try {/*from   ww  w.j  av a 2 s  .co m*/
        // First getting the File

        ByteArrayOutputStream csvBAOS = generateCSV(csvDef, context);

        httpResponse.setContentType("text/csv");
        httpResponse.setHeader("Cache-Control", "no-cache");
        httpResponse.setDateHeader("Expires", -1);
        httpResponse.setContentLength(csvBAOS.size());
        httpResponse.addHeader("Content-disposition",
                "inline; filename=\"" + csvDef.getDownloadFileName() + "\"");
        OutputStream os = httpResponse.getOutputStream();
        csvBAOS.writeTo(os);
        os.close();
    } catch (Exception e) {
        ErrorPageBuilder.getInstance().processError(httpResponse, "Error during CSV-Generation", e);
    }

}

From source file:com.gargoylesoftware.htmlunit.HttpWebConnection.java

/**
 * Reads the content of the stream and saves it in memory or on the file system.
 * @param is the stream to read/*from   w  w  w  .ja v a 2 s .c o  m*/
 * @param maxInMemory the maximumBytes to store in memory, after which save to a local file
 * @return a wrapper around the downloaded content
 * @throws IOException in case of read issues
 */
public static DownloadedContent downloadContent(final InputStream is, final int maxInMemory)
        throws IOException {
    if (is == null) {
        return new DownloadedContent.InMemory(new byte[] {});
    }
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();

    final byte[] buffer = new byte[1024];
    int nbRead;
    try {
        while ((nbRead = is.read(buffer)) != -1) {
            bos.write(buffer, 0, nbRead);
            if (bos.size() > maxInMemory) {
                // we have exceeded the max for memory, let's write everything to a temporary file
                final File file = File.createTempFile("htmlunit", ".tmp");
                file.deleteOnExit();
                try (final FileOutputStream fos = new FileOutputStream(file)) {
                    bos.writeTo(fos); // what we have already read
                    IOUtils.copyLarge(is, fos); // what remains from the server response
                }
                return new DownloadedContent.OnFile(file, true);
            }
        }
    } catch (final ConnectionClosedException e) {
        LOG.warn("Connection was closed while reading from stream.", e);
        return new DownloadedContent.InMemory(bos.toByteArray());
    } catch (final IOException e) {
        // this might happen with broken gzip content
        LOG.warn("Exception while reading from stream.", e);
        return new DownloadedContent.InMemory(bos.toByteArray());
    } finally {
        IOUtils.closeQuietly(is);
    }

    return new DownloadedContent.InMemory(bos.toByteArray());
}