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:org.shimlib.web.servlet.view.filedownload.FileDownloadView.java

@Override
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException {
    response.setContentLength(baos.size());

    // Flush byte array to servlet output stream.
    ServletOutputStream out = response.getOutputStream();
    baos.writeTo(out);//  ww w. j  a  va  2  s.  c  om
    out.flush();
}

From source file:com.ewcms.content.resource.service.operator.FileOperatorTest.java

@Test
public void testRead() throws IOException {
    FileOperator operator = new FileOperator("");
    String uri = FileOperatorTest.class.getResource("write.jpg").getPath();
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    operator.read(output, uri);/*w  w w  .j  ava 2  s  . co  m*/

    Assert.assertTrue(output.size() > 1000);
}

From source file:org.openmrs.module.reporting.common.ExcelBuilderTest.java

@Test
public void shouldBuildAnExcelWorkbook() throws Exception {

    ExcelBuilder excelBuilder = new ExcelBuilder();
    Assert.assertNotNull(excelBuilder.getWorkbook());

    excelBuilder.newSheet("SheetOne");
    Assert.assertEquals("SheetOne", excelBuilder.getCurrentSheet().getSheetName());

    excelBuilder.addCell("Row One Cell One");
    excelBuilder.addCell("Row One Cell Two", "bold");
    Assert.assertEquals("Row One Cell One, Row One Cell Two",
            ExcelUtil.formatRow(excelBuilder.getCurrentRow()));
    excelBuilder.nextRow();/*from w  w  w.  j  a v a2  s.  c  o  m*/
    excelBuilder.addCell("Row Two Cell One");
    excelBuilder.addCell("Row Two Cell Two", "bold");
    Assert.assertEquals("Row Two Cell One, Row Two Cell Two",
            ExcelUtil.formatRow(excelBuilder.getCurrentRow()));

    excelBuilder.newSheet("SheetTwo");
    Assert.assertEquals("SheetTwo", excelBuilder.getCurrentSheet().getSheetName());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    excelBuilder.write(baos);
    Assert.assertTrue(baos.size() > 0);
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

/**
 * //  w ww. j a  va  2 s  .c om
 * @param fis
 * @param expectedLength
 *            length to be expected or -2 if you don't want the check
 * @param cipher
 * @return
 */
public static InputStream createInputStream(InputStream fis, long expectedLength, Cipher cipher) {
    try {
        InputStream in = fis;
        ObjectInputStream ois = new ObjectInputStream(in);
        byte marker = readMarker(ois);
        long l = readLength(ois);
        if (log.isDebugEnabled()) {
            log.debug(
                    "JFSEncryptedStream.createInputStream() length check " + expectedLength + " == " + l + "?");
        } // if
        if (expectedLength != DONT_CHECK_LENGTH) {
            if (l != expectedLength) {
                log.error("JFSEncryptedStream.createInputStream() length check failed");
                return null;
            } // if
        } // if
        if (cipher == null) {
            log.error("JFSEncryptedStream.createInputStream() no cipher for length " + expectedLength);
        } else {
            in = new CipherInputStream(in, cipher);
        } // if
        if (marker == COMPRESSION_DEFLATE) {
            Inflater inflater = new Inflater(true);
            in = new InflaterInputStream(in, inflater, COMPRESSION_BUFFER_SIZE);
        } // if
        if (marker == COMPRESSION_BZIP2) {
            in = new BZip2CompressorInputStream(in);
        } // if
        if (marker == COMPRESSION_LZMA) {
            Decoder decoder = new Decoder();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            byte[] properties = new byte[5];
            int readBytes = in.read(properties, 0, properties.length);
            boolean result = decoder.SetDecoderProperties(properties);
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() readBytes=" + readBytes);
                log.debug("JFSEncryptedStream.createInputStream() result=" + result);
            } // if

            decoder.Code(in, outputStream, l);
            in.close();
            outputStream.close();
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() " + outputStream.size());
            } // if
            in = new ByteArrayInputStream(outputStream.toByteArray());
        } // if
        return in;
    } catch (IOException ioe) {
        log.error("JFSEncryptedStream.createInputStream() I/O Exception " + ioe.getLocalizedMessage());
        return null;
    } // try/catch
}

From source file:org.icgc.dcc.portal.manifest.ManifestArchive.java

public void addManifest(@NonNull String fileName, @NonNull ByteArrayOutputStream fileContents)
        throws IOException {
    val tarEntry = new TarArchiveEntry(fileName);

    tarEntry.setSize(fileContents.size());
    tar.putArchiveEntry(tarEntry);// w  w  w  .  j  a va2 s .  c  o m

    fileContents.writeTo(tar);
    tar.closeArchiveEntry();
}

From source file:werecloud.api.view.JSONView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    ServletOutputStream out = response.getOutputStream();
    if (model.containsKey("model")) {
        ObjectMapper mapper = new ObjectMapper();
        //use ISO-8601 dates instead of timestamp
        mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES, outputNulls);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        mapper.writeValue(bos, model.get("model"));
        response.setContentLength(bos.size());
        response.setContentType("application/json");
        out.write(bos.toByteArray());/*from   w  ww.ja  v a 2  s . c o  m*/
        return;
    }
    throw new Exception("Could not find model.");
}

From source file:org.adho.dhconvalidator.conversion.oxgarage.ZipResult.java

/**
 * @param buffer/* w w w. j  a v a 2  s . c o  m*/
 * @return <code>true</code> if the given buffer contains a ZIP file
 */
private boolean isZipFile(ByteArrayOutputStream buffer) {
    if (buffer.size() > 4) {
        byte[] testArray = buffer.toByteArray();
        return testArray[0] == 0x50 && testArray[1] == 0x4b && testArray[2] == 0x03 && testArray[3] == 0x04;
    }
    return false;
}

From source file:magoffin.matt.sobriquet.sendmail.test.SendmailAliasLDIFGeneratorTests.java

@Test
public void generateNothing() throws IOException {
    SendmailAliasLDIFGenerator gen = new SendmailAliasLDIFGenerator();
    ByteArrayOutputStream byos = new ByteArrayOutputStream();
    gen.generate(Collections.<Alias>emptyList(), byos);
    Assert.assertEquals("Nothing generated", 0, byos.size());
}

From source file:org.sonar.api.charts.AbstractChartTest.java

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

From source file:org.sonar.api.charts.AbstractChartTest.java

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