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.exadel.flamingo.flex.messaging.amf.io.AMF0Serializer.java

/**
 * Writes XML Document//from  w w  w .j a  v a  2s  . c om
 *
 * @param document
 * @throws java.io.IOException
 */
protected void write(Document document) throws IOException {
    outputStream.writeByte(AMF0Body.DATA_TYPE_XML);
    Element docElement = document.getDocumentElement();
    String xmlData = 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:org.dataconservancy.dcs.ingest.client.impl.DualManagerDeposit.java

public String execute() {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    modelBuilder.buildSip(dcp, out);//w  w w .  j a  va  2 s.  com

    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put(HttpHeaderUtil.CONTENT_TYPE, "application/xml");
    metadata.put(HttpHeaderUtil.CONTENT_LENGTH, Integer.toString(out.size()));

    try {
        return sipManager.deposit(new ByteArrayInputStream(out.toByteArray()), "application/xml",
                "http://dataconservancy.org/schemas/dcp/1.0", metadata).getDepositID();
    } catch (PackageException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.fusepool.p3.proxy.ProxyHandler.java

private void startTransformation(final String resourceUri, final String ldpcUri, final String transformerUri,
        final byte[] bytes, final Header[] requestHeaders) {
    (new Thread() {

        @Override/*  w  w  w. j  a v a2  s.  c  o  m*/
        public void run() {
            Transformer transformer = new TransformerClientImpl(transformerUri);
            Entity entity = new InputStreamEntity() {

                @Override
                public MimeType getType() {
                    try {
                        for (Header h : requestHeaders) {
                            if (h.getName().equalsIgnoreCase("Content-Type")) {
                                return new MimeType(h.getValue());
                            }
                        }
                        return new MimeType("application/octet-stream");
                    } catch (MimeTypeParseException ex) {
                        throw new RuntimeException(ex);
                    }
                }

                @Override
                public InputStream getData() throws IOException {
                    return new ByteArrayInputStream(bytes);
                }

                @Override
                public URI getContentLocation() {
                    try {
                        return new URI(resourceUri);
                    } catch (URISyntaxException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            };

            Entity transformationResult;
            try {
                transformationResult = transformer.transform(entity, new MimeType("*/*"));
            } catch (MimeTypeParseException ex) {
                throw new RuntimeException(ex);
            }

            //final HttpEntity httpEntity = response.getEntity();
            //final Header contentTypeHeader = httpEntity.getContentType();
            final String contentType = transformationResult.getType().toString();
            try {
                if (isRdf(contentType)) {
                    Graph transformationResultGraph = parser.parse(transformationResult.getData(), contentType);
                    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    serializer.serialize(baos, transformationResultGraph, SupportedFormat.TURTLE);
                    final byte[] bytes = baos.toByteArray();
                    final StringWriter turtleString = new StringWriter(baos.size() + 2000);
                    turtleString.append(new String(bytes, "UTF-8"));
                    turtleString.append('\n');
                    turtleString.append("<> " + ELDP.transformedFrom + " <" + resourceUri + "> .");
                    post(ldpcUri, new ByteArrayEntity(turtleString.toString().getBytes("UTF-8")), "text/turtle",
                            resourceUri, requestHeaders);
                } else {
                    post(ldpcUri, new org.apache.http.entity.InputStreamEntity(transformationResult.getData()),
                            contentType, resourceUri, requestHeaders);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

        }

    }).start();
}

From source file:com.dragoniade.deviantart.deviation.SearchRss.java

public boolean validate() {
    String user = "";
    SEARCH search = SEARCH.getDefault();
    String searchQuery = search.getSearch().replace("%username%", user);

    String queryString = "http://backend.deviantart.com/rss.xml?q=" + searchQuery + "&type=deviation";
    GetMethod method = new GetMethod(queryString);
    try {/*from   www.j  av a 2  s  . c om*/

        method = new GetMethod(queryString);
        int sc = client.executeMethod(method);
        if (sc != 200) {
            return false;
        }
        InputStream is = method.getResponseBodyAsStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[4096];
        int read = -1;
        while ((read = is.read(buffer)) > -1) {
            baos.write(buffer, 0, read);
            if (baos.size() > 2097152) {
                return false;
            }
        }
        String charsetName = method.getResponseCharSet();
        String body = baos.toString(charsetName);

        if (body.length() == -0) {
            return false;
        }

        if (body.indexOf("<channel") < 0) {
            return false;
        }
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        method.releaseConnection();
    }
}

From source file:com.nhn.android.me2day.sample.multipart.Part.java

/**
 * Return the full length of all the data.
 * If you override this method make sure to override 
 * #send(OutputStream) as well/*w w w.j a va2  s. c o  m*/
 * 
 * @return long The length.
 * @throws IOException If an IO problem occurs
 */
public long length() throws IOException {
    Log.d("Part", "enter length()");
    if (lengthOfData() < 0) {
        return -1;
    }
    ByteArrayOutputStream overhead = new ByteArrayOutputStream();
    sendStart(overhead);
    sendDispositionHeader(overhead);
    sendContentTypeHeader(overhead);
    sendTransferEncodingHeader(overhead);
    sendEndOfHeader(overhead);
    sendEnd(overhead);
    return overhead.size() + lengthOfData();
}

From source file:nz.geek.caffe.jmeter.CrLfTcpClient.java

/**
 * Reads data until the defined EOL byte is reached. If there is no EOL byte
 * defined, then reads until the end of the stream is reached.
 *//*from   ww  w  . j  av a 2  s.  c om*/
@Override
public String read(final InputStream is) throws ReadException {

    final ByteArrayOutputStream w = new ByteArrayOutputStream();

    try {

        int count = 0;
        int read = 0;
        int last = -1;

        while (true) {
            read = is.read();

            if (count > 0 && read == '\n' && last == '\r') {
                break;
            }

            w.write(read);

            count++;

            last = read;

        }

        w.flush();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Read: " + w.size() + "\n" + w.toString());
        }

        // cut off last trailing \r
        byte[] assembledData = new byte[count - 1];
        System.arraycopy(w.toByteArray(), 0, assembledData, 0, count - 1);

        return new String(assembledData, this.charset);
    } catch (final IOException e) {
        throw new ReadException("", e, w.toString());
    }
}

From source file:com.android.internal.http.multipart.Part.java

/**
 * Return the full length of all the data.
 * If you override this method make sure to override 
 * #send(OutputStream) as well// ww  w.  j a  v  a 2  s.  c  o  m
 * 
 * @return long The length.
 * @throws IOException If an IO problem occurs
 */
public long length() throws IOException {
    LOG.trace("enter length()");
    if (lengthOfData() < 0) {
        return -1;
    }
    ByteArrayOutputStream overhead = new ByteArrayOutputStream();
    sendStart(overhead);
    sendDispositionHeader(overhead);
    sendContentTypeHeader(overhead);
    sendTransferEncodingHeader(overhead);
    sendEndOfHeader(overhead);
    sendEnd(overhead);
    return overhead.size() + lengthOfData();
}

From source file:com.jobs.lib_v1.net.http.multipart.Part.java

/***
 * Return the full length of all the data. If you override this method make sure to override #send(OutputStream) as well
 * /*w ww  . ja  va 2s.  c  om*/
 * @return long The length.
 * @throws IOException If an IO problem occurs
 */
public long length() throws IOException {
    if (lengthOfData() < 0) {
        return -1;
    }

    ByteArrayOutputStream overhead = new ByteArrayOutputStream();
    sendStart(overhead);
    sendDispositionHeader(overhead);
    sendContentTypeHeader(overhead);
    sendTransferEncodingHeader(overhead);
    sendEndOfHeader(overhead);
    sendEnd(overhead);

    return overhead.size() + lengthOfData();
}

From source file:org.commoncrawl.util.CompressedURLFPListV2.java

public static void validateDuplicateChecking() {
    TreeMultimap<Long, URLFPV2> sourceMap = TreeMultimap.create();
    TreeMultimap<Long, URLFPV2> destMap = TreeMultimap.create();
    ;//  w  ww  .  ja  v  a  2s .  c om

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    Builder firstBuilder = new Builder(FLAG_ARCHIVE_SEGMENT_ID | FLAG_SERIALIZE_URLFP_FLAGS);

    insertURLFPItem(sourceMap, "http://www.google.com/hello", 1, 255);
    insertURLFPItem(sourceMap, "http://google.com/hello", 1, 255);
    insertURLFPItem(sourceMap, "http://google.com/foobar", 1, 255);
    insertURLFPItem(sourceMap, "http://www.google.com/hello", 1, 255);

    addMapToBuilder(firstBuilder, sourceMap);

    try {
        // flush to byte stream ...
        firstBuilder.flush(byteStream);
        // now set up to read the stream
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray(), 0,
                byteStream.size());
        Reader reader = new Reader(inputStream);

        while (reader.hasNext()) {
            URLFPV2 fp = reader.next();
            destMap.put(fp.getRootDomainHash(), fp);
        }
        reader.close();

        for (long rootDomain : sourceMap.keySet()) {
            for (URLFPV2 URLFPV2 : sourceMap.get(rootDomain)) {
                System.out.println("SourceFP Root:" + URLFPV2.getRootDomainHash() + " Domain:"
                        + URLFPV2.getDomainHash() + " URL:" + URLFPV2.getUrlHash());
            }
        }
        for (long rootDomain : destMap.keySet()) {
            for (URLFPV2 URLFPV2 : destMap.get(rootDomain)) {
                System.out.println("DestFP Root:" + URLFPV2.getRootDomainHash() + " Domain:"
                        + URLFPV2.getDomainHash() + " URL:" + URLFPV2.getUrlHash());
            }
        }

        Assert.assertTrue(sourceMap.equals(destMap));

    } catch (IOException e) {
        e.printStackTrace();
    }

}

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

@RequestMapping(value = "/test/{fileName}", method = RequestMethod.GET)
public ResponseEntity<byte[]> staticImagesDownload(@PathVariable("fileName") String fileName)
        throws IOException {

    String imageLoc = servletContext.getRealPath("WEB-INF/benchmark");
    FileInputStream fis = new FileInputStream(imageLoc + "/" + fileName + ".png");

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {/*from w w w  .ja va 2 s  .  c o m*/
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
    } catch (IOException ex) {
        // nothing here
    } finally {
        fis.close();
    }

    HttpHeaders responseHeaders = httpHeaderAttachment("TEST-" + fileName, MimeType.PNG, bos.size());
    return new ResponseEntity<byte[]>(bos.toByteArray(), responseHeaders, HttpStatus.OK);
}