Example usage for java.io ByteArrayOutputStream size

List of usage examples for java.io ByteArrayOutputStream size

Introduction

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

Prototype

public synchronized int size() 

Source Link

Document

Returns the current size of the buffer.

Usage

From source file:no.dusken.common.plugin.control.web.PluginResourceController.java

@RequestMapping(value = "/pluginresources.do", method = RequestMethod.GET)
public void get(String path, HttpServletResponse response) throws PageNotFoundException {
    String name = "/no/dusken/plugin/content/" + path;
    String filetype = path.substring(path.lastIndexOf("."));
    boolean isAllowedFiletype = allowedFileTypes.contains(filetype);

    if (!isAllowedFiletype) {
        throw new PageNotFoundException(filetype + " not allowed");
    }// w  w w .j  a v  a 2s .c o  m

    InputStream inputStream = getClass().getResourceAsStream(name);

    if (inputStream != null) {
        try {
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, output);
            response.setContentLength(output.size());
            response.setContentType(getContentType(filetype));
            response.setHeader("Cache-Control", "public, max-age=2505600");
            IOUtils.copy(new ByteArrayInputStream(output.toByteArray()), response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new PageNotFoundException(path);
    }
}

From source file:corner.encrypt.services.impl.CipherKey.java

public void recoverCipher() {
    FileInputStream fis = null;//from   w  w w.jav  a  2 s .  c o m
    try {
        fis = new FileInputStream(persisFile);
        ByteArrayOutputStream bou = new ByteArrayOutputStream();
        FileCopyUtils.copy(fis, bou);
        bou.close();
        if (bou.size() > 0) {
            byte[] persistBytes = bou.toByteArray();
            cipher = Base64.decodeBase64(persistBytes);
        }
    } catch (Throwable e) {
        System.err.println("Exception during deserialization:" + e);
    } finally {
        InternalUtils.close(fis);
    }
}

From source file:com.liferay.mobile.android.http.file.DownloadFileTest.java

@Test
public void download() throws Exception {
    BasicAuthentication basic = (BasicAuthentication) session.getAuthentication();

    DigestAuthentication digest = new DigestAuthentication(basic.getUsername(), basic.getPassword());

    session.setAuthentication(digest);//from  w w  w  . j  ava 2s.co  m

    String URL = session.getServer() + "/webdav/guest/document_library/"
            + _file.getString(DLAppServiceTest.TITLE);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DownloadUtil.download(session, baos, URL, null);
    assertEquals(5, baos.size());
}

From source file:org.sonar.server.charts.deprecated.BaseChartTest.java

protected void assertChartSizeGreaterThan(BufferedImage img, int size) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ChartUtilities.writeBufferedImageAsPNG(output, img, true, 0);
    assertTrue("PNG size in bits=" + output.size(), output.size() > size);
}

From source file:org.sonar.server.charts.deprecated.BaseChartTest.java

protected void assertChartSizeLesserThan(BufferedImage img, int size) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ChartUtilities.writeBufferedImageAsPNG(output, img, true, 0);
    assertTrue("PNG size in bits=" + output.size(), output.size() < size);
}

From source file:io.milton.common.RangeUtilsTest.java

public void testWrite_BeyondEndOfFile() throws IOException {
    InputStream in = this.getClass().getResourceAsStream("/jquery-ui-1.8.20.custom.min.js");
    if (in == null) {
        throw new RuntimeException("Couldnt find test file");
    }//from   w  ww.ja v  a  2 s  .c  om
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(in, bout);
    System.out.println("input file length=" + bout.size());
    in = new ByteArrayInputStream(bout.toByteArray());

    Range r = Range.parse("30357-71179"); // one past index of last byte
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    RangeUtils.writeRange(in, r, out);
    System.out.println("testWrite_OpenRange wrote: " + out.toByteArray().length + " bytes");
    assertEquals(40822, out.toByteArray().length);
}

From source file:io.milton.common.RangeUtilsTest.java

public void testWrite_ToEndOfFile() throws IOException {
    InputStream in = this.getClass().getResourceAsStream("/jquery-ui-1.8.20.custom.min.js");
    if (in == null) {
        throw new RuntimeException("Couldnt find test file");
    }//from   w  w  w .j  a  va  2s. co  m
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(in, bout);
    System.out.println("input file length=" + bout.size());
    in = new ByteArrayInputStream(bout.toByteArray());

    Range r = Range.parse("30356-71178"); // exactly end of file
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    RangeUtils.writeRange(in, r, out);
    System.out.println("testWrite_OpenRange2 wrote: " + out.toByteArray().length + " bytes");
    assertEquals(40823, out.toByteArray().length);
}

From source file:ar.com.fdvs.dj.output.MemoryReportWriter.java

public void writeTo(final HttpServletResponse _response) throws IOException, JRException {
    LOGGER.info("entering MemoryReportWriter.writeTo()");
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, stream);
    exporter.exportReport();//  ww  w  . j a  v a2 s.  co m
    _response.setContentLength(stream.size());
    copyStreams(new ByteArrayInputStream(stream.toByteArray()), _response.getOutputStream());
}

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 .  j a  v  a2 s.  c om*/
 * @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());
}

From source file:org.dice_research.topicmodeling.io.stream.DocumentSupplierSerializer.java

protected void writeDocument(DataOutputStream dout, Document document) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(document);//from ww w .j  a va  2  s. c o  m
    oout.close();
    dout.writeInt(bout.size());
    bout.writeTo(dout);
}