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:com.android.gallery3d.filtershow.ui.ExportDialog.java

public void updateCompressionFactor() {
    Bitmap bitmap = MasterImage.getImage().getFilteredImage();
    if (bitmap == null) {
        return;/* w w w .ja v a 2s .  c  o m*/
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, mQuality, out);
    mCompressedSize = out.size();
    mCompressedBounds = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
}

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

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

    try {/*from  w  ww  .  ja va2s .  c  o 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:org.olat.core.gui.media.ServletUtil.java

/**
 * @param response// w ww. j  av  a 2 s .  c o m
 * @param result
 */
public static void serveStringResource(HttpServletRequest httpReq, HttpServletResponse response,
        String result) {
    setStringResourceHeaders(response);

    // log the response headers prior to sending the output
    boolean isDebug = log.isDebug();

    if (isDebug) {
        log.debug(
                "\nResponse headers (some)\ncontent type:" + response.getContentType() + "\ncharacterencoding:"
                        + response.getCharacterEncoding() + "\nlocale:" + response.getLocale());
    }

    try {
        long rstart = 0;
        if (isDebug) {
            rstart = System.currentTimeMillis();
        }
        // make a ByteArrayOutputStream to be able to determine the length.
        // buffer size: assume average length of a char in bytes is max 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream(result.length() * 2);

        // we ignore the accept-charset from the request and always write in
        // utf-8:
        // we have lots of different languages (content) in one application to
        // support, and more importantly,
        // a blend of olat translations and content by authors which can be in
        // different languages.
        OutputStreamWriter osw = new OutputStreamWriter(baos, "utf-8");
        osw.write(result);
        osw.close();
        // the data is now utf-8 encoded in the bytearray -> push it into the outputstream
        int encLen = baos.size();
        response.setContentLength(encLen);

        OutputStream os;
        if (Settings.isDebuging()) {
            SlowBandWidthSimulator sbs = Windows
                    .getWindows(CoreSpringFactory.getImpl(UserSessionManager.class).getUserSession(httpReq))
                    .getSlowBandWidthSimulator();
            os = sbs.wrapOutputStream(response.getOutputStream());
        } else {
            os = response.getOutputStream();
        }
        byte[] bout = baos.toByteArray();
        os.write(bout);
        os.close();

        if (isDebug) {
            long rstop = System.currentTimeMillis();
            log.debug("time to serve inline-resource " + result.length() + " chars / " + encLen + " bytes: "
                    + (rstop - rstart));
        }
    } catch (IOException e) {
        if (isDebug) {
            log.warn("client browser abort when serving inline", e);
        }
    }
}

From source file:org.apache.myriad.state.utils.ByteBufferSupportTest.java

@Test
public void testEmptyAddByteBuffer() throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ByteBuffer bb = getByteBuffer(ArrayUtils.EMPTY_BYTE_ARRAY);
    ByteBufferSupport.addByteBuffer(bb, stream);
    assertEquals(0, stream.size());
}

From source file:org.apache.myriad.state.utils.ByteBufferSupportTest.java

@Test
public void testNonEmptyAddByteBufferList() throws Exception {
    ByteBuffer bb = getByteBuffer(BYTE_ARRAY);
    ByteBuffer bbTwo = getByteBuffer(BYTE_ARRAY);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    List<ByteBuffer> bList = Lists.newArrayList(bb, bbTwo);

    ByteBufferSupport.addByteBuffers(bList, stream);
    assertEquals(44, stream.size());
}

From source file:com.ewcms.publication.deploy.provider.DeployOperatorBaseTest.java

@Test
public void testCopyFileBytes() throws Exception {

    String rootPath = System.getProperty("java.io.tmpdir", "/tmp");
    DeployOperatorable operator = new DeployOperatorBaseImpl.Builder().setPath(rootPath).build();
    DeployOperatorBaseImpl operatorImpl = (DeployOperatorBaseImpl) operator;

    String content = "test-byte";
    operatorImpl.copy(content.getBytes(), "/docuemnt/2/write.jpg");
    FileObject fileObject = operatorImpl.getFileObject();

    ByteArrayOutputStream stream = (ByteArrayOutputStream) fileObject.getContent().getOutputStream();
    Assert.assertEquals(content.getBytes().length, stream.size());
}

From source file:org.ow2.chameleon.fuchsia.importer.jsonrpc.it.JSONRPCImporterTest.java

@Override
protected <T> ImportDeclaration createImportDeclaration(String endpointId, Class<T> klass, T object) {
    //A JsonServlet must be registered
    final JsonRpcServer jsonRpcServer = new JsonRpcServer(object, klass);
    httpServer.createContext(SERVLET_NAME + "/" + endpointId, new HttpHandler() {
        public void handle(HttpExchange httpExchange) throws IOException {
            // Get InputStream for reading the request body.
            // After reading the request body, the stream is close.
            InputStream is = httpExchange.getRequestBody();
            // Get OutputStream to send the response body.
            // When the response body has been written, the stream must be closed to terminate the exchange.
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

            jsonRpcServer.handle(is, byteArrayOutputStream);
            byteArrayOutputStream.close();

            int size = byteArrayOutputStream.size();
            // send response header
            httpExchange.sendResponseHeaders(HttpStatus.SC_OK, size);

            // write response to real outputStream
            OutputStream realOs = httpExchange.getResponseBody();
            realOs.write(byteArrayOutputStream.toByteArray(), 0, size);
            realOs.close();/*w  w w.j ava  2  s  .  c  o m*/
        }
    });

    // Build associated ImportDeclaration
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(ID, endpointId);
    props.put(URL, "http://localhost:" + HTTP_PORT + SERVLET_NAME + "/" + endpointId);
    props.put(SERVICE_CLASS, klass.getName());
    props.put(CONFIGS, "jsonrpc");

    return ImportDeclarationBuilder.fromMetadata(props).build();
}

From source file:at.medevit.elexis.ehc.core.internal.EhcCoreServiceTest.java

@Test
public void testGetVaccinationsDocument() throws Exception {
    EhcCoreServiceImpl service = new EhcCoreServiceImpl();
    CdaChVacd cda = service.getVaccinationsDocument(patient, mandant);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    CDAUtil.save(cda.getDocRoot().getClinicalDocument(), output);
    assertTrue(output.size() > 0);
}

From source file:at.medevit.elexis.ehc.core.internal.EhcCoreServiceTest.java

@Test
public void testWritePatientDocument() throws Exception {
    EhcCoreServiceImpl service = new EhcCoreServiceImpl();
    CdaCh cda = service.getCdaChDocument(patient, mandant);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    CDAUtil.save(cda.getDocRoot().getClinicalDocument(), output);
    assertTrue(output.size() > 0);
    String xml = output.toString("UTF-8");
    assertTrue(xml.contains("name"));
    assertTrue(xml.contains("firstname"));
}

From source file:org.taverna.server.master.localworker.RunDatabase.java

private void logLength(String message, Object obj) {
    if (!log.isDebugEnabled())
        return;/*from  ww w. java  2 s.c o  m*/
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.close();
        log.debug(message + ": " + baos.size());
    } catch (Exception e) {
        log.warn("oops", e);
    }
}