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.facebook.stetho.dumpapp.RawDumpappHandler.java

@Override
protected HttpEntity getResponseEntity(HttpRequest request, InputStream bufferedInput, HttpResponse response)
        throws IOException {
    ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();

    try {//www  .  j  ava 2 s  .  c  o  m
        PrintStream stdout = new PrintStream(stdoutBuffer);
        try {
            ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
            PrintStream stderr = new PrintStream(stderrBuffer);

            try {
                int exitCode = getDumper().dump(bufferedInput, stdout, stderr, getArgs(request));
                response.addHeader(RESPONSE_HEADER_EXIT_CODE, String.valueOf(exitCode));
            } finally {
                stderr.close();
                if (stderrBuffer.size() > 0) {
                    System.err.write(stderrBuffer.toByteArray());
                }
            }
        } finally {
            stdout.close();
        }
    } finally {
        bufferedInput.close();
    }

    return createResponseEntity(stdoutBuffer.toByteArray());
}

From source file:com.webcohesion.ofx4j.client.net.OFXV1Connection.java

/**
 * Send the specified buffer to the specified URL.
 *
 * @param url The URL.//from  w w  w.ja  v  a2 s.c o m
 * @param outBuffer The buffer.
 * @return The response.
 */
protected InputStream sendBuffer(URL url, ByteArrayOutputStream outBuffer)
        throws IOException, OFXConnectionException {
    HttpURLConnection connection = openConnection(url);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-ofx");
    connection.setRequestProperty("Content-Length", String.valueOf(outBuffer.size()));
    connection.setRequestProperty("Accept", "*/*, application/x-ofx");
    connection.setDoOutput(true);
    connection.connect();

    OutputStream out = connection.getOutputStream();
    out.write(outBuffer.toByteArray());

    InputStream in;
    int responseCode = connection.getResponseCode();
    if (responseCode >= 200 && responseCode < 300) {
        in = connection.getInputStream();
    } else if (responseCode >= 400 && responseCode < 500) {
        throw new OFXServerException("Error with client request: " + connection.getResponseMessage(),
                responseCode);
    } else {
        throw new OFXServerException(
                "Invalid response code from OFX server: " + connection.getResponseMessage(), responseCode);
    }

    return in;
}

From source file:org.neo4j.shell.StartClientTest.java

@Test
public void shouldPrintVersionAndExit() throws Exception {
    // given//from w  w w. j a va  2  s  .  c  om
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    CtrlCHandler ctrlCHandler = mock(CtrlCHandler.class);
    StartClient client = new StartClient(new PrintStream(out), new PrintStream(err));

    // when
    client.start(new String[] { "-version" }, ctrlCHandler);

    // then
    assertEquals(0, err.size());
    String version = out.toString();
    assertThat(version, startsWith("Neo4j Community, version "));
}

From source file:br.gov.jfrj.siga.libs.webwork.SigaAnonimoActionSupport.java

public String getUrlEncodedParameters() throws UnsupportedEncodingException, IOException {
    if (getPar() != null) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (final String key : getPar().keySet()) {
            final String as[] = getPar().get(key);
            for (final String val : as) {
                if (baos.size() > 0)
                    baos.write('&');
                baos.write(URLEncoder.encode(key, "utf-8").getBytes());
                baos.write('=');
                baos.write(URLEncoder.encode(val, "utf-8").getBytes());
            }//ww  w.ja v a 2s. co  m
        }
        return new String(baos.toByteArray());
    }
    return null;
}

From source file:gov.utah.dts.det.ccl.actions.reports.ReportsPrintAction.java

private void sendToResponse(ByteArrayOutputStream ba, String filename) throws IOException {
    if (ba != null && ba.size() > 0) {
        response.setContentLength(ba.size());
        response.setContentType("application/pdf");
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        if (StringUtils.isNotBlank(filename)) {
            response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
        } else {/*from w  w  w  .j  a v a  2 s .com*/
            response.setHeader("Content-disposition", "attachment");
        }

        ServletOutputStream out = response.getOutputStream();
        ba.writeTo(out);
        out.flush();
    }
}

From source file:org.apache.marmotta.kiwi.test.cluster.SerializerTest.java

/**
 * Run the given object through the marshaller using an in-memory stream.
 * @param origin/*  w  ww  .ja v  a2  s . c o m*/
 * @param <T>
 * @return
 */
private <T> void marshall(T origin, StreamSerializer<T> externalizer)
        throws IOException, ClassNotFoundException, InterruptedException {
    log.info("- testing Java ObjectStream ...");
    ByteArrayOutputStream outBytesOS = new ByteArrayOutputStream();
    ObjectOutputStream outOS = new ObjectOutputStream(outBytesOS);

    outOS.writeObject(origin);

    outOS.close();

    log.info("  object {}: serialized with {} bytes", origin, outBytesOS.size());

    log.info("- testing serializer directly ...");
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    ObjectDataOutputStream out = new ObjectDataOutputStream(outBytes, simpleService);

    externalizer.write(out, origin);
    out.close();

    log.info("  object {}: serialized with {} bytes", origin, outBytes.size());

    ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray());
    ObjectDataInputStream in = new ObjectDataInputStream(inBytes, simpleService);

    T destination1 = externalizer.read(in);

    Assert.assertEquals(origin, destination1);

    log.info("- testing serializer with Hazelcast serialization service ...");

    ByteArrayOutputStream outBytesFull = new ByteArrayOutputStream();
    ObjectDataOutputStream outFull = new ObjectDataOutputStream(outBytesFull, fullService);

    fullService.writeObject(outFull, origin);
    outFull.close();

    log.info("  object {}: serialized with {} bytes", origin, outBytesFull.size());

    ByteArrayInputStream inBytesFull = new ByteArrayInputStream(outBytesFull.toByteArray());
    ObjectDataInputStream inFull = new ObjectDataInputStream(inBytesFull, fullService);

    T destination2 = (T) fullService.readObject(inFull);

    Assert.assertEquals(origin, destination2);

}

From source file:org.apache.marmotta.kiwi.test.externalizer.ExternalizerTest.java

/**
 * Run the given object through the marshaller using an in-memory stream.
 * @param origin/*  w w  w. j  a v a2  s . c  om*/
 * @param <T>
 * @return
 */
private <T> void marshall(T origin, AdvancedExternalizer<T> externalizer)
        throws IOException, ClassNotFoundException, InterruptedException {
    log.info("- testing Java ObjectStream ...");
    ByteArrayOutputStream outBytesOS = new ByteArrayOutputStream();
    ObjectOutputStream outOS = new ObjectOutputStream(outBytesOS);

    outOS.writeObject(origin);

    outOS.close();

    log.info("  object {}: serialized with {} bytes", origin, outBytesOS.size());

    log.info("- testing externalizer directly ...");
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(outBytes);

    externalizer.writeObject(out, origin);
    out.close();

    log.info("  object {}: serialized with {} bytes", origin, outBytes.size());

    ByteArrayInputStream inBytes = new ByteArrayInputStream(outBytes.toByteArray());
    ObjectInputStream in = new ObjectInputStream(inBytes);

    T destination1 = externalizer.readObject(in);

    Assert.assertEquals(origin, destination1);
    assertEqualsId(origin, destination1);

    log.info("- testing externalizer with infinispan cluster marshaller ...");

    byte[] bytes = marshaller.objectToByteBuffer(origin);
    log.info("  object {}: serialized with {} bytes", origin, bytes.length);

    Object destination2 = marshaller.objectFromByteBuffer(bytes);

    Assert.assertEquals(origin, destination2);
    assertEqualsId(origin, destination2);

    log.info("- testing externalizer with infinispan hotrod marshaller ...");

    byte[] bytesH = hotrod.objectToByteBuffer(origin);
    log.info("  object {}: serialized with {} bytes", origin, bytesH.length);

    Object destination3 = hotrod.objectFromByteBuffer(bytesH);

    Assert.assertEquals(origin, destination3);
    assertEqualsId(origin, destination3);

}

From source file:net.networksaremadeofstring.rhybudd.ZenossWidgetGraph.java

@SuppressWarnings("unused")
private Bitmap RenderLineGraph() {
    Bitmap emptyBmap = Bitmap.createBitmap(290, 150, Config.ARGB_8888);

    int width = emptyBmap.getWidth();
    int height = emptyBmap.getHeight();
    Bitmap charty = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(charty);
    final int color = 0xff0B0B61;
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);//from w w w . j av  a2s  .  c  o  m

    //if(warningEvents > )
    canvas.drawText("100", 0, 10, paint);

    //y
    canvas.drawLine(25, 0, 25, 289, paint);
    //x
    canvas.drawLine(25, 149, 289, 149, paint);

    int CritArray[] = { 5, 4, 6, 10, 10, 6, 4, 4 };
    int curX = 25;

    int divisor = 148 / 10;
    paint.setColor(Color.RED);
    int curY = 148 - (CritArray[0] * divisor);

    for (int a : CritArray) {
        canvas.drawLine(curX, curY, curX + 32, (148 - (a * divisor)), paint);
        curX += 32;
        curY = 148 - (a * divisor);
    }

    int ErrArray[] = { 1, 2, 2, 2, 4, 2, 1, 0 };
    curX = 25;

    paint.setColor(Color.rgb(255, 102, 0));
    curY = 148 - (ErrArray[0] * divisor);

    for (int a : ErrArray) {
        canvas.drawLine(curX, curY, curX + 32, (148 - (a * divisor)), paint);
        curX += 32;
        curY = 148 - (a * divisor);
    }

    int WarnArray[] = { 0, 2, 4, 8, 10, 4, 2, 2 };
    curX = 25;

    paint.setColor(Color.YELLOW);
    curY = 148 - (WarnArray[0] * divisor);

    Path myPath = new Path();

    for (int a : WarnArray) {
        canvas.drawLine(curX, curY, curX + 32, (148 - (a * divisor)), paint);
        curX += 32;
        curY = 148 - (a * divisor);
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    charty.compress(CompressFormat.PNG, 50, out);

    return BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
}

From source file:net.networksaremadeofstring.rhybudd.ZenossWidgetGraph.java

private Bitmap RenderBarGraph(int CritCount, int ErrCount, int WarnCount) {

    //Log.i("Counts", Integer.toString(CritCount) + " / " + Integer.toString(ErrCount) + " / " + Integer.toString(WarnCount));
    Bitmap emptyBmap = Bitmap.createBitmap(290, 150, Config.ARGB_8888);

    int width = emptyBmap.getWidth();
    int height = emptyBmap.getHeight();
    Bitmap charty = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(charty);
    //final int color = 0xff0B0B61; 
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);/*from  w ww.j  a va 2  s. co m*/

    //y
    canvas.drawLine(25, 0, 25, 289, paint);
    //x
    canvas.drawLine(25, 149, 289, 149, paint);

    paint.setAntiAlias(true);
    int Max = 0;

    if (CritCount > ErrCount && CritCount > WarnCount)
        Max = CritCount;
    else if (ErrCount > CritCount && ErrCount > WarnCount)
        Max = ErrCount;
    else if (WarnCount > CritCount && WarnCount > ErrCount)
        Max = WarnCount;
    else
        Max = CritCount;

    if (Max > 0)
        canvas.drawText(Integer.toString(Max), 0, 10, paint);

    if (Max > 1)
        canvas.drawText(Integer.toString(Max / 2), 0, 75, paint);

    canvas.drawText("0", 0, 148, paint);

    double divisor = 148 / (double) Max;

    paint.setAlpha(128);

    Rect rect = new Rect(32, (int) (148 - (divisor * CritCount)), 64, 148);
    paint.setColor(Color.argb(200, 208, 0, 0)); //red

    if (CritCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    rect = new Rect(128, (int) (148 - (divisor * ErrCount)), 160, 148);
    paint.setColor(Color.argb(200, 255, 102, 0));//orange

    if (ErrCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    rect = new Rect(224, (int) (148 - (divisor * WarnCount)), 256, 148);
    paint.setColor(Color.argb(200, 255, 224, 57)); //yellow
    if (WarnCount > 0)
        canvas.drawRect(new RectF(rect), paint);

    //Return
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    charty.compress(CompressFormat.PNG, 50, out);

    return BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
}

From source file:node.Mailbox.java

public void printMessageSize(MessageWrapper<AbstractMessage> message) {
    try {//  w ww .jav a 2s .co m
        ByteArrayOutputStream baostemp = new ByteArrayOutputStream();
        ObjectOutputStream oostemp = new ObjectOutputStream(baostemp);
        oostemp.writeObject(message.msg);
        oostemp.flush();
        println("Message is " + baostemp.size() + " bytes");
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }
}