Example usage for java.io OutputStream toString

List of usage examples for java.io OutputStream toString

Introduction

In this page you can find the example usage for java.io OutputStream toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:Main.java

/***
 * Serialize an object into xml string./*from w  w  w . j a v a2 s. co m*/
 * 
 * @param obj
 *            object to serialize
 * @return Xml string represents the object
 */
public static String serializeObject(final Object obj) {
    final OutputStream bufferStream = new ByteArrayOutputStream();

    // Create XML encoder.
    final XMLEncoder xenc = new XMLEncoder(bufferStream);

    xenc.writeObject(obj);
    // Need to close the XMLEncoder to flush.
    xenc.close();

    final String serializedString = bufferStream.toString();

    // JavaDoc indicates that we will not need to close
    // a ByteArrayOutputStream. From JavaDoc:
    // Closing a ByteArrayOutputStream has no effect. The methods
    // in this class can be called after the stream has been closed
    // without generating an IOException.
    //
    // bufferStream.close();

    return serializedString;
}

From source file:org.matonto.ontology.core.utils.MatOntoStringUtils.java

/**
 * Removes OWLAPI signature at the end of ontology document.
 *///from   w  w w  .j  a va2s .co  m
public static OutputStream removeOWLGeneratorSignature(@Nonnull OutputStream outputStream) {
    OutputStream result = new ByteArrayOutputStream();
    String signature = "<\\!--.*?OWL API.*?-->|#\\##.*?OWL API.*";
    String content = outputStream.toString();
    content = content.replaceAll(signature, "");

    try {
        result.write(content.getBytes(Charset.forName("UTF-8")));

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(outputStream);
    }

    return result;
}

From source file:org.ebayopensource.turmeric.eclipse.utils.io.PropertiesFileUtil.java

/**
 * Write to file./* ww  w . jav  a2 s. c o  m*/
 *
 * @param properties the properties
 * @param file the file
 * @param comments the comments
 * @throws IOException Signals that an I/O exception has occurred.
 * @throws CoreException the core exception
 */
public static void writeToFile(Properties properties, IFile file, String comments)
        throws IOException, CoreException {
    OutputStream output = null;
    try {
        output = new ByteArrayOutputStream();
        properties.store(output, comments);
        WorkspaceUtil.writeToFile(output.toString(), file, null);
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:com.chnoumis.commons.zip.utils.ZipUtils.java

public static List<ZipInfo> unZiptoZipInfo(InputStream is) throws ArchiveException, IOException {
    List<ZipInfo> results = new ArrayList<ZipInfo>();
    ArchiveInputStream in = null;/* w w  w  .j a va  2 s. c  o m*/
    try {
        in = new ArchiveStreamFactory().createArchiveInputStream("zip", is);
        ZipArchiveEntry entry = null;
        while ((entry = (ZipArchiveEntry) in.getNextEntry()) != null) {
            ZipInfo zipInfo = new ZipInfo();
            OutputStream o = new ByteArrayOutputStream();
            try {
                IOUtils.copy(in, o);
            } finally {
                o.close();
            }
            zipInfo.setFileName(entry.getName());
            zipInfo.setFileContent(o.toString().getBytes());
            results.add(zipInfo);
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    is.close();

    return results;
}

From source file:org.xmlactions.common.xml.Transform.java

/**
 * Transform an xml using an xml style sheet transformation (xslt);
 * //from  www. j  av  a 2  s . c om
 * @param readerXSL
 *            the stylesheet
 * @param readerXML
 *            the xml
 * @return the transformed xml
 * @throws TransformerException
 */
public static String transform(Reader readerXSL, Reader readerXML) throws TransformerException {

    TransformerFactory tFactory = TransformerFactory.newInstance();

    Transformer transformer = tFactory.newTransformer(new StreamSource(readerXSL));

    OutputStream outputStream = new ByteArrayOutputStream();

    OutputStreamWriter osw = new OutputStreamWriter(outputStream);

    transformer.transform(new StreamSource(readerXML), new StreamResult(osw));

    return outputStream.toString();
}

From source file:com.dianping.avatar.cache.util.CacheMonitorUtil.java

private static String getErrorString(Throwable throwable) {
    OutputStream out = null;
    PrintWriter writer = null;//from   www  .  ja  v  a 2  s  .c o  m
    try {
        out = new ByteArrayOutputStream(3000);
        writer = new PrintWriter(out);
        throwable.printStackTrace(writer);
        writer.flush();
        return out.toString();
    } catch (Exception e2) {
        return throwable.getMessage();
    } finally {
        if (writer != null) {
            writer.close();
        }
        out = null;
        writer = null;
    }
}

From source file:org.xmlactions.common.xml.Transform.java

/**
 * Transform an xml using an xml style sheet transformation (xslt);
 * //from  ww w .j  a  v  a  2 s  .co  m
 * @param readerXSL
 *            the stylesheet
 * @param readerXML
 *            the xml
 * @param transformerFactory
 *            - xalan = org.apache.xalan.processor.TransformerFactoryImpl,
 *            can be empty for no transformer
 * @param map
 *            - map of parameters to pass to the transformer, can be null
 *            for no params
 * @return the transformed xml
 * @throws TransformerException
 */
public static String transform(Reader readerXSL, Reader readerXML, String transformerFactory,
        Map<String, Object> map) throws TransformerException {

    // should we set a transformerFactory?
    if (StringUtils.isNotEmpty(transformerFactory)) {
        Transform.setTransformationFactory(transformerFactory);
    } else {
        Transform.setDefaultTransformationFactory();
    }

    TransformerFactory tFactory = TransformerFactory.newInstance();

    // reset to default factory.
    Transform.setDefaultTransformationFactory();

    Transformer transformer = tFactory.newTransformer(new StreamSource(readerXSL));

    // Add any parameters if needed.
    if (map != null) {
        for (String key : map.keySet()) {
            Object obj = map.get(key);
            transformer.setParameter(key, obj);
        }
    }

    OutputStream outputStream = new ByteArrayOutputStream();

    OutputStreamWriter osw = new OutputStreamWriter(outputStream);

    transformer.transform(new StreamSource(readerXML), new StreamResult(osw));

    return outputStream.toString();
}

From source file:org.operamasks.faces.debug.Debug.java

public static String getServerLog(FacesContext context) {
    OutputStream out = (OutputStream) context.getExternalContext().getRequestMap()
            .get(KEY_DEBUGLOGHANDLER_OUTPUTSTREAM);

    if (out != null)
        return out.toString();

    return null;/*w w  w  .  j  a va 2 s .com*/
}

From source file:org.matonto.ontology.core.utils.MatOntoStringUtils.java

/**
 * .//  ww w  . ja  v a  2  s.c o m
 */
public static OutputStream replaceLanguageTag(@Nonnull OutputStream outputStream,
        @Nonnull String languageSuffix) {
    OutputStream result = new ByteArrayOutputStream();
    String toReplace = "rdf:datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"";
    String replaceWith = "xml:lang=\"" + languageSuffix + "\"";
    String content = outputStream.toString();
    content = content.replaceAll(toReplace, replaceWith);
    try {
        result.write(content.getBytes(Charset.forName("UTF-8")));

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(outputStream);
    }

    return result;
}

From source file:org.xmlactions.common.xml.Transform.java

/**
 * Transform an xml using an xml style sheet transformation (xslt);
 * //from   w  w w  .j a  v  a2s  . co  m
 * @param inputXSL
 *            the stylesheet
 * @param inputXML
 *            the xml
 * @return the transformed xml
 * @throws TransformerException
 */
public static String transform(InputStream inputXSL, InputStream inputXML) throws TransformerException {

    TransformerFactory tFactory = TransformerFactory.newInstance();

    Transformer transformer = tFactory.newTransformer(new StreamSource(inputXSL));

    OutputStream outputStream = new ByteArrayOutputStream();

    OutputStreamWriter osw = new OutputStreamWriter(outputStream);

    StreamSource ss = new StreamSource(new BufferedReader(new InputStreamReader(inputXML)));
    transformer.transform(new StreamSource(inputXML), new StreamResult(osw));

    return outputStream.toString();
}