Example usage for java.io ByteArrayOutputStream writeTo

List of usage examples for java.io ByteArrayOutputStream writeTo

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream writeTo.

Prototype

public synchronized void writeTo(OutputStream out) throws IOException 

Source Link

Document

Writes the complete contents of this ByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count) .

Usage

From source file:org.springframework.flex.http.AmfHttpMessageConverter.java

private void writeObject(Object data, HttpOutputMessage outputMessage, AmfTrace trace) throws IOException {
    ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
    Amf3Output serializer = new Amf3Output(new SerializationContext());
    serializer.setOutputStream(outBuffer);
    serializer.setDebugTrace(trace);/*from w  w  w .ja  v  a 2 s  .  com*/
    try {
        serializer.writeObject(data);
        outBuffer.flush();
        outBuffer.close();
        outputMessage.getHeaders().setContentLength(outBuffer.size());
        outBuffer.writeTo(outputMessage.getBody());
    } catch (SerializationException se) {
        throw new HttpMessageNotWritableException("Could not write " + data + " as AMF message.", se);
    }
}

From source file:org.deegree.securityproxy.service.commons.responsefilter.clipping.AbstractClippingResponseFilterManager.java

private DefaultResponseFilterReport processClippingAndAddHeaderInfo(
        StatusCodeResponseBodyWrapper servletResponse, Geometry clippingGeometry, OwsRequest request)
        throws IOException, ClippingException {
    InputStream imageAsStream = servletResponse.getBufferedStream();
    ByteArrayOutputStream destination = new ByteArrayOutputStream();
    ResponseClippingReport clippedImageReport = imageClipper.calculateClippedImage(imageAsStream,
            clippingGeometry, destination, request);

    addHeaderInfoIfNoFailureOccurred(servletResponse, clippedImageReport);
    // required to set the header (must be set BEFORE any data is written to the response)
    destination.writeTo(servletResponse.getRealOutputStream());
    return clippedImageReport;
}

From source file:OutSimplePdf.java

public void makePdf(HttpServletRequest request, HttpServletResponse response, String methodGetPost) {
    try {//from  www. java  2s  .co m
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("some text"));
        document.close();

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/pdf");
        response.setContentLength(baos.size());

        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();

    } catch (Exception e2) {
        System.out.println("Error in " + getClass().getName() + "\n" + e2);
    }
}

From source file:org.springframework.flex.http.AmfView.java

/**
 * {@inheritDoc}/*w ww.ja  v  a 2s  . c  om*/
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Object value = filterModel(model);

    try {

        AmfTrace trace = null;
        if (log.isDebugEnabled()) {
            trace = new AmfTrace();
        }

        ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
        SerializationContext context = new SerializationContext();
        Amf3Output out = new Amf3Output(context);
        if (trace != null) {
            out.setDebugTrace(trace);
        }
        out.setOutputStream(outBuffer);
        out.writeObject(value);
        out.flush();

        outBuffer.flush();

        response.setContentLength(outBuffer.size());
        outBuffer.writeTo(response.getOutputStream());

        if (log.isDebugEnabled()) {
            log.debug("Wrote AMF message:\n" + trace);
        }
    } finally {
        FlexContext.clearThreadLocalObjects();
        SerializationContext.clearThreadLocalObjects();
    }
}

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

/**
 * Writes XML Document//w  w w.j av a 2s . c o  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:de.betterform.connector.serializer.FormDataSerializer.java

/**
 * Serialize instance into multipart/form-data stream as defined in
 * http://www.w3.org/TR/xforms/slice11.html#serialize-form-data
 *
 * @param submission/*from   w  ww.jav  a 2  s . c o  m*/
 * @param instance
 * @param wrapper
 * @param defaultEncoding
 * @throws Exception on error
 */
public void serialize(Submission submission, Node instance, SerializerRequestWrapper wrapper,
        String defaultEncoding) throws Exception {
    // sanity checks
    if (instance == null) {
        return;
    }

    switch (instance.getNodeType()) {

    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
        break;

    case Node.DOCUMENT_NODE:
        instance = ((Document) instance).getDocumentElement();
        break;

    default:
        return;
    }

    String encoding = defaultEncoding;
    if (submission.getEncoding() != null) {
        encoding = submission.getEncoding();
    }

    // generate boundary
    Random rnd = new Random(System.currentTimeMillis());
    String boundary = DigestUtils.md5Hex(getClass().getName() + rnd.nextLong());

    // serialize the instance
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(bos, encoding)));
    if (instance.getNodeType() == Node.ELEMENT_NODE) {
        serializeElement(writer, (Element) instance, boundary, encoding);
    } else {
        writer.print(instance.getTextContent());
    }
    writer.print("\r\n--" + boundary + "--");
    writer.flush();
    bos.writeTo(wrapper.getBodyStream());
    wrapper.addHeader("internal-boundary-mark", boundary);
}

From source file:org.openamf.DefaultGateway.java

/**
 * Uses the AMFSerializer to serialize the request
 *
 * @see org.openamf.io.AMFSerializer/*w ww  .ja v  a  2 s . co  m*/
 */
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.google.cloud.dns.testing.LocalDnsHelper.java

private static void writeBatchResponse(HttpExchange exchange, ByteArrayOutputStream output) {
    exchange.getResponseHeaders().set("Content-type", "multipart/mixed; boundary=" + RESPONSE_BOUNDARY);
    try {//from   w ww  .j a v a2s  .  c  o m
        exchange.getResponseHeaders().add("Connection", "close");
        exchange.sendResponseHeaders(200, output.toByteArray().length);
        OutputStream responseBody = exchange.getResponseBody();
        output.writeTo(responseBody);
        responseBody.close();
    } catch (IOException e) {
        log.log(Level.WARNING, "IOException encountered when sending response.", e);
    }
}

From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF0Serializer.java

/**
 * Writes XML Document/*from  w  ww. j a  v  a  2s  .  c o  m*/
 *
 * @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.apache.camel.converter.stream.CachedOutputStream.java

private void pageToFileStream() throws IOException {
    flush();//from  w w  w  . j a va2  s .c o  m

    ByteArrayOutputStream bout = (ByteArrayOutputStream) currentStream;
    if (outputDir == null) {
        tempFile = FileUtil.createTempFile("cos", ".tmp");
    } else {
        tempFile = FileUtil.createTempFile("cos", ".tmp", outputDir);
    }

    if (LOG.isTraceEnabled()) {
        LOG.trace("Creating temporary stream cache file: " + tempFile);
    }

    try {
        currentStream = new BufferedOutputStream(new FileOutputStream(tempFile));
        bout.writeTo(currentStream);
    } finally {
        // ensure flag is flipped to file based
        inMemory = false;
    }
}