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:caarray.client.test.java.JavaApiFacade.java

public Integer getFileContents(String api, List<CaArrayEntityReference> fileReferences, boolean compressed)
        throws Exception {
    if (fileReferences.isEmpty())
        return 0;
    int total = 0;

    for (int i = 0; i < fileReferences.size(); i++) {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        dataApiUtils.copyFileContentsToOutputStream(fileReferences.get(i), compressed, outStream);
        total += outStream.size();
    }//from  w w w .j a  v a  2  s  .co  m
    return total;
}

From source file:ca.farrelltonsolar.classic.PVOutputUploader.java

private Bundle deserializeBundle(byte[] data) {
    Bundle bundle = null;/*from  w w w  . j  a v a 2s. co  m*/
    final Parcel parcel = Parcel.obtain();
    try {
        final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        final byte[] buffer = new byte[1024];
        final GZIPInputStream zis = new GZIPInputStream(new ByteArrayInputStream(data));
        int len = 0;
        while ((len = zis.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        zis.close();
        parcel.unmarshall(byteBuffer.toByteArray(), 0, byteBuffer.size());
        parcel.setDataPosition(0);
        bundle = parcel.readBundle();
    } catch (IOException ex) {
        Log.w(getClass().getName(), String.format("deserializeBundle failed ex: %s", ex));
        bundle = null;
    } finally {
        parcel.recycle();
    }
    return bundle;
}

From source file:org.openamf.DefaultGateway.java

/**
 * Uses the AMFSerializer to serialize the request
 *
 * @see org.openamf.io.AMFSerializer/*from ww  w. ja v  a2s  .  c  om*/
 */
protected void serializeAMFMessage(HttpServletResponse resp, AMFMessage message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    AMFSerializer serializer = new AMFSerializer(dos);
    serializer.serializeMessage(message);
    resp.setContentType("application/x-amf");
    resp.setContentLength(baos.size());
    ServletOutputStream sos = resp.getOutputStream();
    baos.writeTo(sos);
    sos.flush();
}

From source file:com.highcharts.export.controller.ExportController.java

@RequestMapping(value = "/files/{name}.{ext}", method = RequestMethod.GET)
public HttpEntity<byte[]> getFile(@PathVariable("name") String name, @PathVariable("ext") String extension)
        throws SVGConverterException, IOException {

    Path path = Paths.get(TempDir.getOutputDir().toString(), name + "." + extension);
    String filename = path.toString();
    MimeType mime = getMime(extension);

    ByteArrayOutputStream stream = writeFileToStream(filename);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", mime.getType() + "; charset=utf-8");
    headers.setContentLength(stream.size());

    return new HttpEntity<byte[]>(stream.toByteArray(), headers);
}

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

@Test
public void shouldReportEditionThroughDbInfoApp() throws Exception {
    // given/*w  ww.  j av  a  2s  .co m*/
    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[] { "-path", db.getGraphDatabaseAPI().getStoreDir(), "-c",
            "dbinfo -g Configuration unsupported.dbms.edition" }, ctrlCHandler);

    // then
    assertEquals(0, err.size());
    assertThat(out.toString(), containsString("\"unsupported.dbms.edition\": \"community\""));
}

From source file:org.openamf.io.AMFSerializer.java

/**
 * Writes XML Document/*from   w  w w.  j  a  v a2s. co  m*/
 *
 * @param document
 * @throws IOException
 */
protected void write(Document document) throws IOException {
    outputStream.writeByte(AMFBody.DATA_TYPE_XML);
    Element docElement = document.getDocumentElement();
    String xmlData = XMLUtils.convertDOMToString(docElement);
    if (log.isDebugEnabled())
        log.debug("Writing xmlData: \n" + xmlData);
    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    baOutputStream.write(xmlData.getBytes("UTF-8"));
    outputStream.writeInt(baOutputStream.size());
    baOutputStream.writeTo(outputStream);
}

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

@Action(value = "print-facility-license-summary")
public void doPrintLicensorFacilitySummary() {
    Person person = null;/* w  w w  .j a v  a2  s.  c  o  m*/
    if (specialistId != null) {
        person = personService.getPerson(specialistId);
    }
    if (person == null || person.getId() == null) {
        return;
    }

    try {
        if (facLicenseSummarySortBy == null) {
            facLicenseSummarySortBy = FacilityLicenseSummarySortBy.getDefaultSortBy();
        }
        List<FacilityLicenseView> licenses = facilityService.getFacilityLicenseSummary(specialistId, endDate,
                facLicenseSummarySortBy);
        ByteArrayOutputStream ba = FacilityLicenseSummaryReport.generate(person, endDate,
                facLicenseSummarySortBy, licenses);
        if (ba != null && ba.size() > 0) {
            // This is where the response is set
            String filename = "";
            if (person != null) {
                if (StringUtils.isNotBlank(person.getFirstName())) {
                    filename += person.getFirstName();
                }
                if (StringUtils.isNotBlank(person.getLastName())) {
                    if (filename.length() > 0) {
                        filename += "_";
                    }
                    filename += person.getLastName();
                }
            }
            if (filename.length() > 0) {
                filename += "_";
            }
            filename += "facility_license_summary.pdf";
            sendToResponse(ba, filename);
        }
    } catch (Exception ex) {
        generateErrorPdf();
    }
}

From source file:io.undertow.servlet.test.streams.AbstractServletInputStreamTestCase.java

private void runTestViaJavaImpl(final String message, String url) throws IOException {
    HttpURLConnection urlcon = null;
    try {//from  ww w  .j a v  a2s  . com
        String uri = getBaseUrl() + "/servletContext/" + url;
        urlcon = (HttpURLConnection) new URL(uri).openConnection();
        urlcon.setInstanceFollowRedirects(true);
        urlcon.setRequestProperty("Connection", "close");
        urlcon.setRequestMethod("POST");
        urlcon.setDoInput(true);
        urlcon.setDoOutput(true);
        OutputStream os = urlcon.getOutputStream();
        os.write(message.getBytes());
        os.close();
        Assert.assertEquals(StatusCodes.OK, urlcon.getResponseCode());
        InputStream is = urlcon.getInputStream();

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        byte[] buf = new byte[256];
        int len;
        while ((len = is.read(buf)) > 0) {
            bytes.write(buf, 0, len);
        }
        is.close();
        final String response = new String(bytes.toByteArray(), 0, bytes.size());
        if (!message.equals(response)) {
            System.out.println(String.format("response=%s", Hex.encodeHexString(response.getBytes())));
        }
        Assert.assertEquals(message, response);
    } finally {
        if (urlcon != null) {
            urlcon.disconnect();
        }
    }
}

From source file:GridFDock.DataDistribute.java

public int getFileSize1(String server, String user, String pswd, String path, String filename, int port) {

    URL url = null;//from w  ww  .j  a  v a 2s  .  c  o  m
    URLConnection con = null;
    int c;
    int t = 0;

    BufferedInputStream bis;

    try {

        url = new URL("ftp://" + user + ":" + pswd + "@" + server + ":" + port + path + "/" + filename);

        url.getFile().length();
        con = url.openConnection();
        con.connect();
        InputStream urlfs = con.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while ((c = urlfs.read()) != -1)
            out.write((byte) c);
        urlfs.close();
        t = out.size();
        // return loadmap (new ByteArrayInputStream(out.toByteArray());

    } catch (MalformedURLException e) {
        System.out.println("When get the size of file from site, the url is wrong.");
    } catch (IOException e) {
        System.out.println(
                "When get the size of file from site, it can not open site or the file does not exist.");
    }

    return t;
}

From source file:org.apache.hama.bsp.message.HamaMessageManagerImpl.java

@Override
public final void transfer(InetSocketAddress addr, BSPMessageBundle<M> bundle) throws IOException {
    HamaMessageManager<M> bspPeerConnection = this.getBSPPeerConnection(addr);
    if (bspPeerConnection == null) {
        throw new IllegalArgumentException("Can not find " + addr.toString() + " to transfer messages to!");
    } else {//from   w  w w .  j a v a 2s .  c  o m
        if (conf.getBoolean(Constants.MESSENGER_RUNTIME_COMPRESSION, false)) {
            ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
            DataOutputStream bufferDos = new DataOutputStream(byteBuffer);
            bundle.write(bufferDos);

            byte[] compressed = compressor.compress(byteBuffer.toByteArray());
            peer.incrementCounter(BSPPeerImpl.PeerCounter.TOTAL_COMPRESSED_BYTES_TRANSFERED, compressed.length);
            peer.incrementCounter(BSPPeerImpl.PeerCounter.TOTAL_DECOMPRESSED_BYTES, byteBuffer.size());
            bspPeerConnection.put(compressed);
        } else {
            //peer.incrementCounter(BSPPeerImpl.PeerCounter.TOTAL_MESSAGE_BYTES_TRANSFERED, bundle.getLength());
            bspPeerConnection.put(bundle);
        }
    }
}