Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:Main.java

/**
 * To output a Node as a String.//from  w w  w.  j a  v  a  2 s.  c o m
 */
public static String nodeToString(Node document) throws Exception {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    Source xmlSource = new DOMSource(document);
    Result outputTarget = new StreamResult(outputStream);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(xmlSource, outputTarget);

    return outputStream.toString();
}

From source file:Main.java

/**
 * Creates from the given Object an xml string.
 * //from  www.  j a  v  a 2s  .  c o m
 * @param <T>
 *            the generic type of the return type
 * @param obj
 *            the obj to transform to an xml string.
 * @return the xml string
 */
public static <T> String toXmlWithXMLEncoder(final T obj) {
    XMLEncoder enc = null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        enc = new XMLEncoder(baos);
        enc.writeObject(obj);
        enc.close();
        enc = null;
    } finally {
        if (enc != null) {
            enc.close();
        }
    }
    return baos.toString();
}

From source file:com.vmware.identity.rest.core.client.RequestExecutor.java

private static String consumeContent(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(in.available());

    final byte[] data = new byte[8196];
    int len = 0;/*from w  w  w. j  a v a 2s  . c o m*/

    while ((len = in.read(data)) > 0) {
        out.write(data, 0, len);
    }

    return out.toString();
}

From source file:edu.rice.cs.bioinfo.programs.phylonet.PhyloNetAAT.java

private static void checkTest(String nexus, String expectedStdOut, String expectedStdError, String testFile)
        throws IOException {
    String faultMessage = testFile + " failed.";
    ByteArrayOutputStream display = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    Program.run(new ByteArrayInputStream(nexus.getBytes()), new PrintStream(error), new PrintStream(display),
            _rand, BigDecimal.ZERO);
    Assert.assertEquals(faultMessage, expectedStdError, error.toString().replace("\r", ""));
    Assert.assertEquals(faultMessage, expectedStdOut, display.toString().replace("\r", ""));
}

From source file:com.smash.revolance.ui.materials.TemplateHelper.java

public static String processTemplate(String templateName, Map<String, Object> variables)
        throws TemplateException, IOException {
    // load template
    Template template = null;//from w  ww  . ja  v  a  2 s .c  om
    InputStreamReader stream = null;
    try {
        InputStream content = TemplateHelper.class.getClassLoader().getResourceAsStream(templateName);
        if (content == null) {
            System.err.println("Unable to find template: " + templateName);
            return "Unable to find template: " + templateName;
        } else {
            stream = new InputStreamReader(content);
            template = new Template("template", stream, null);
        }
    } catch (IOException e) {
        System.err.println("Unable to find template: " + templateName);
        return "Unable to find template: " + templateName;
    } finally {
        IOUtils.closeQuietly(stream);
    }
    // process template
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (template != null) {
        // Fill the template with parameter values
        Writer out = new OutputStreamWriter(baos);
        template.process(variables, out);
        out.flush();
    }
    return baos.toString();
}

From source file:com.ery.estorm.util.ReflectionUtils.java

/**
 * Log the current thread stacks at INFO level.
 * //from w ww  .j a  v  a2  s  .  c om
 * @param log
 *            the logger that logs the stack trace
 * @param title
 *            a descriptive title for the call stacks
 * @param minInterval
 *            the minimum time from the last
 */
public static void logThreadInfo(Log log, String title, long minInterval) {
    boolean dumpStack = false;
    if (log.isInfoEnabled()) {
        synchronized (ReflectionUtils.class) {
            long now = System.currentTimeMillis();
            if (now - previousLogTime >= minInterval * 1000) {
                previousLogTime = now;
                dumpStack = true;
            }
        }
        if (dumpStack) {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            printThreadInfo(new PrintWriter(buffer), title);
            log.info(buffer.toString());
        }
    }
}

From source file:io.milton.http.LockInfoSaxHandler.java

public static LockInfo parseLockInfo(Request request) throws IOException, FileNotFoundException, SAXException {
    InputStream in = request.getInputStream();

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

    LockInfoSaxHandler handler = new LockInfoSaxHandler();
    reader.setContentHandler(handler);/*from   www  .  j  a va2  s .co  m*/
    if (log.isDebugEnabled()) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        byte[] bytes = bout.toByteArray();
        in = new ByteArrayInputStream(bytes);
        log.debug("LockInfo: " + bout.toString());
    }
    reader.parse(new InputSource(in));
    LockInfo info = handler.getInfo();
    info.depth = LockDepth.INFINITY; // todo
    if (info.lockedByUser == null) {
        if (request.getAuthorization() != null) {
            if (request.getAuthorization().getUser() != null) {
                info.lockedByUser = request.getAuthorization().getUser();
            } else {
                Object user = request.getAuthorization().getTag();
                if (user instanceof DiscretePrincipal) {
                    DiscretePrincipal dp = (DiscretePrincipal) user;
                    info.lockedByUser = dp.getPrincipalURL();
                }
            }

        }
    }
    if (info.lockedByUser == null) {
        log.warn("resource is being locked with a null user. This won't really be locked at all...");
    }
    return info;
}

From source file:com.sap.prd.mobile.ios.mios.FileUtilsTest.java

private static boolean checkForSymbolicLink(final File f) throws IOException {

    ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
    PrintStream stream = new PrintStream(byteOs);

    try {//w  w  w  . j  a va 2  s  . co m
        Forker.forkProcess(stream, null, "ls", "-l", f.getAbsolutePath());
        stream.flush();
        return byteOs.toString().startsWith("l");
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static void assertFileContentEquals(String path, String expectedContent) throws IOException {
    FileObject file = VFS.getManager().resolveFile(path);
    InputStream in = file.getContent().getInputStream();
    int length = 0;
    byte[] buffer = new byte[256];
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);//from w w w  .  j  a  v  a 2 s.  co  m
    }

    assertEquals("Unexpected file content", expectedContent, out.toString());
}

From source file:Main.java

/**
 * Serialize any object into String/*w  ww .j ava2  s .c o  m*/
 * @param object Object
 * @return String
 * @throws IOException If unable to access Stream
 */
public static String serialize(Object object) throws java.io.IOException {
    if (object == null)
        return null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
            new Base64OutputStream(byteArrayOutputStream, 0));
    objectOutputStream.writeObject(object);
    objectOutputStream.flush();
    objectOutputStream.close();
    return byteArrayOutputStream.toString();
}