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:Main.java

public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69, 70, 71, 72 };

    OutputStream os = new ByteArrayOutputStream();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(bs);/*w  w  w. ja  v  a  2s. c  om*/

    baos.writeTo(os);

    System.out.println(os.toString());

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69, 70, 71, 72 };

    OutputStream os = new ByteArrayOutputStream();

    ByteArrayOutputStream baos = new ByteArrayOutputStream(200);

    baos.write(bs);//from  w ww  .  ja  va 2 s . c  o  m

    baos.writeTo(os);

    System.out.println(os.toString());

}

From source file:Main.java

public static void main(String args[]) throws IOException {
    int howMany = 20;
    // To avoid resizing the buffer, calculate the size of the
    // byte array in advance.
    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);/*from  ww  w  .  ja v  a 2 s .  c o  m*/
    }

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {

    int howMany = 20;

    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);//  w  ww . ja  v a 2s.co  m
    }

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}

From source file:MainClass.java

public static void main(String args[]) throws IOException {

    int howMany = 20;

    // To avoid resizing the buffer, calculate the size of the
    // byte array in advance.
    ByteArrayOutputStream bout = new ByteArrayOutputStream(howMany * 4);
    DataOutputStream dout = new DataOutputStream(bout);

    for (int i = 0; i <= 20; i++) {
        dout.writeInt(i);//  ww w. j  a  va2 s  .c om
    }

    FileOutputStream fout = new FileOutputStream("fibonacci.dat");
    try {
        bout.writeTo(fout);
        fout.flush();
    } finally {
        fout.close();
    }
}

From source file:com.github.xmltopdf.JasperPdfGenerator.java

/**.
 * @param args/*  www . j a v a 2s.  c  om*/
 *            the arguments
 * @throws IOException in case IO error
 */
public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        LOG.info(null, USAGE);
        return;
    }
    List<String> templates = new ArrayList<String>();
    List<String> xmls = new ArrayList<String>();
    List<String> types = new ArrayList<String>();
    for (String arg : args) {
        if (arg.endsWith(".jrxml")) {
            templates.add(arg);
        } else if (arg.endsWith(".xml")) {
            xmls.add(arg);
        } else if (arg.startsWith(DOC_TYPE)) {
            types = Arrays
                    .asList(arg.substring(DOC_TYPE.length()).replaceAll("\\s+", "").toUpperCase().split(","));
        }
    }
    if (templates.isEmpty()) {
        LOG.info(null, USAGE);
        return;
    }
    if (types.isEmpty()) {
        types.add("PDF");
    }
    for (String type : types) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        if (DocType.valueOf(type) != null) {
            new JasperPdfGenerator().createDocument(templates, xmls, os, DocType.valueOf(type));
            os.writeTo(
                    new FileOutputStream(templates.get(0).replaceFirst("\\.jrxml$", "." + type.toLowerCase())));
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    ByteArrayOutputStream f = new ByteArrayOutputStream(12);
    System.out.println("Please 10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }//  ww w. jav  a  2  s  .c  o m
    System.out.println("Buffer as a string");
    System.out.println(f.toString());
    System.out.println("Into array");
    byte b[] = f.toByteArray();
    for (int i = 0; i < b.length; i++) {
        System.out.print((char) b[i]);
    }
    System.out.println();
    OutputStream f2 = new FileOutputStream("test.txt");
    f.writeTo(f2);
    f.reset();
    System.out.println("10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }
    System.out.println("Done..");
}

From source file:illarion.compile.Compiler.java

public static void main(final String[] args) {
    ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream();
    PrintStream orgStdOut = System.out;
    System.setOut(new PrintStream(stdOutBuffer));

    SLF4JBridgeHandler.removeHandlersForRootLogger();
    SLF4JBridgeHandler.install();/* w  w  w .  j a v  a 2 s  .  c om*/

    Options options = new Options();

    final Option npcDir = new Option("n", "npc-dir", true,
            "The place where the compiled NPC files are stored.");
    npcDir.setArgs(1);
    npcDir.setArgName("directory");
    npcDir.setRequired(false);
    options.addOption(npcDir);

    final Option questDir = new Option("q", "quest-dir", true,
            "The place where the compiled Quest files are stored.");
    questDir.setArgs(1);
    questDir.setArgName("directory");
    questDir.setRequired(false);
    options.addOption(questDir);

    final Option type = new Option("t", "type", true,
            "This option is used to set what kind of parser is supposed to be used in case"
                    + " the content of standard input is processed.");
    type.setArgs(1);
    type.setArgName("type");
    type.setRequired(false);
    options.addOption(type);

    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        String[] files = cmd.getArgs();
        if (files.length > 0) {
            System.setOut(orgStdOut);
            stdOutBuffer.writeTo(orgStdOut);

            processFileMode(cmd);
        } else {
            System.setOut(orgStdOut);
            processStdIn(cmd);
        }
    } catch (final ParseException e) {
        final HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java -jar compiler.jar [Options] File", options, true);
        System.exit(-1);
    } catch (final IOException e) {
        LOGGER.error(e.getLocalizedMessage());
        System.exit(-1);
    }
}

From source file:Main.java

/** 
 *Copies source stream to target./* w ww  .j a v a 2  s .c o  m*/
 * 
 *@param source The source. 
 *@param name target The target.
 **/
protected static void copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target) throws Exception {
    // See if this is a MemoryStream -- we can use WriteTo.
    ByteArrayOutputStream memContentStream = source;
    if (memContentStream != null) {
        memContentStream.writeTo(target);
        memContentStream.flush();
    } else {
        // Otherwise, copy data through a buffer

        int c;
        ByteArrayInputStream inStream = new ByteArrayInputStream(source.toByteArray());

        while ((c = inStream.read()) != -1) {
            target.write((char) c);

        }
    }
}

From source file:Main.java

public static void printStream(InputStream is) {
    StreamSource source = new StreamSource(is);
    String msgString = null;/*from  w ww  . j  a v  a 2 s  . c  o  m*/
    try {
        Transformer xFormer = TransformerFactory.newInstance().newTransformer();
        xFormer.setOutputProperty("omit-xml-declaration", "yes");
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        Result result = new StreamResult(outStream);
        xFormer.transform(source, result);
        outStream.writeTo(System.out);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}