Example usage for org.apache.commons.io.output ByteArrayOutputStream toByteArray

List of usage examples for org.apache.commons.io.output ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for org.apache.commons.io.output ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Gets the curent contents of this byte stream as a byte array.

Usage

From source file:com.offbynull.peernetic.debug.visualizer.App.java

public static void main(String[] args) throws Throwable {
    Random random = new Random();
    Visualizer<Integer> visualizer = new JGraphXVisualizer<>();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Recorder<Integer> recorder = new XStreamRecorder(baos);

    visualizer.visualize(recorder, null);

    visualizer.step("Adding nodes 1 and 2", new AddNodeCommand<>(1),
            new ChangeNodeCommand(1, null, new Point(random.nextInt(400), random.nextInt(400)), Color.RED),
            new AddNodeCommand<>(2),
            new ChangeNodeCommand(2, null, new Point(random.nextInt(400), random.nextInt(400)), Color.BLUE));
    Thread.sleep(500);//  w w w.ja v a  2s .c  o  m

    visualizer.step("Adding nodes 3 and 4", new AddNodeCommand<>(3),
            new ChangeNodeCommand(3, null, new Point(random.nextInt(400), random.nextInt(400)), Color.ORANGE),
            new AddNodeCommand<>(4),
            new ChangeNodeCommand(4, null, new Point(random.nextInt(400), random.nextInt(400)), Color.PINK));
    Thread.sleep(500);

    visualizer.step("Connecting 1/2/3 to 4", new AddEdgeCommand<>(1, 4), new AddEdgeCommand<>(2, 4),
            new AddEdgeCommand<>(3, 4));
    Thread.sleep(500);

    visualizer.step("Adding trigger to 4 when no more edges",
            new TriggerOnLingeringNodeCommand(4, new RemoveNodeCommand<>(4)));
    Thread.sleep(500);

    visualizer.step("Removing connections from 1 and 2", new RemoveEdgeCommand<>(1, 4),
            new RemoveEdgeCommand<>(2, 4));
    Thread.sleep(500);

    visualizer.step("Removing connections from 3", new RemoveEdgeCommand<>(3, 4));
    Thread.sleep(500);

    recorder.close();

    Thread.sleep(2000);

    JGraphXVisualizer<Integer> visualizer2 = new JGraphXVisualizer<>();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    Player<Integer> player = new XStreamPlayer<>(bais);

    visualizer2.visualize();

    player.play(visualizer2);

}

From source file:com.qcadoo.model.internal.utils.JdomUtils.java

public static byte[] documentToByteArray(final Document document) {
    try {/*from   w w w . jav  a  2  s  . c  om*/
        XMLOutputter outputter = new XMLOutputter();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        outputter.output(document, out);
        return out.toByteArray();
    } catch (IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:de.micromata.genome.gwiki.utils.PropUtils.java

public static String fromProperties(Properties props) {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    try {/* w  w  w .  j a v  a  2s.c o  m*/
        props.store(bout, "");
        return new String(bout.toByteArray(), PROPS_ENCODING);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:lucee.runtime.functions.other.ObjectSave.java

public synchronized static Object call(PageContext pc, Object input, String filepath) throws PageException {
    if (!(input instanceof Serializable))
        throw new ApplicationException("can only serialize object from type Serializable");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from   w  w w  . j  ava2  s.  c  o  m
        JavaConverter.serialize((Serializable) input, baos);

        byte[] barr = baos.toByteArray();

        // store to file
        if (!StringUtil.isEmpty(filepath, true)) {
            Resource res = ResourceUtil.toResourceNotExisting(pc, filepath);
            pc.getConfig().getSecurityManager().checkFileLocation(res);
            IOUtil.copy(new ByteArrayInputStream(barr), res, true);
        }
        return barr;

    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
}

From source file:com.aestasit.markdown.Markdown.java

public static String printAst(final RootNode node) {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    new AstPrinter(new PrintStream(data)).visit(node);
    return new String(data.toByteArray());
}

From source file:com.aestasit.markdown.Markdown.java

public static String extractText(final RootNode node) {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    new TextExtractor(new PrintStream(data)).visit(node);
    return new String(data.toByteArray());
}

From source file:com.eucalyptus.auth.euare.ldap.LicParserTest.java

private static String readInputAsString(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[512];
    int nRead = 0;
    while ((nRead = in.read(buf)) >= 0) {
        baos.write(buf, 0, nRead);/*from   w w  w.j  a  va 2  s .  co  m*/
    }

    return new String(baos.toByteArray(), "UTF-8");
}

From source file:com.aestasit.markdown.BaseTest.java

protected static InputStream allTestData() throws IOException {
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    for (String fileName : allTestFiles()) {
        IOUtils.write(IOUtils.toString(testData(fileName)), data);
    }//from  www .j av a  2 s. c  o  m
    IOUtils.closeQuietly(data);
    return new ByteArrayInputStream(data.toByteArray());
}

From source file:com.eucalyptus.auth.policy.PolicyParserTest.java

private static String readInputAsString(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] buf = new byte[512];
    int nRead = 0;
    while ((nRead = in.read(buf)) >= 0) {
        baos.write(buf, 0, nRead);//www .  j  a  va2s .co m
    }

    String string = new String(baos.toByteArray(), "UTF-8");
    baos.close();
    return string;
}

From source file:net.padaf.xmpbox.SaveMetadataHelper.java

/**
 * Serialize a schema into a Byte Array// w  ww  . jav  a 2  s  .c  o m
 * 
 * @param schema
 *            Schema concerned by the serialization processing
 * @return a ByteArray which contains serialized schema
 * @throws TransformException
 *             When couldn't parse data to XML/RDF
 */
public static byte[] serialize(XMPSchema schema) throws TransformException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    serialize(schema, bos);
    IOUtils.closeQuietly(bos);
    return bos.toByteArray();
}