Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

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

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:Main.java

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

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(bs);//from  w  w w .j  a v a  2 s .  c o  m

    for (byte b : baos.toByteArray()) {
        baos.write(b);

        System.out.println(b);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path sourceFile = FileSystems.getDefault().getPath("C:/home/docs/users.txt");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Files.copy(sourceFile, outputStream);
    byte arr[] = outputStream.toByteArray();
    System.out.println("The contents of " + sourceFile.getFileName());
    for (byte data : arr) {
        System.out.print((char) data);
    }/* ww  w.ja v a2s  .  co  m*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int size = 200;
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = image.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.BLUE);//from   w  w  w. j a  v  a2  s .c om
    for (int i = 0; i < size; i += 5) {
        g.drawOval(i, i, size - i, size - i);
    }
    g.dispose();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);

    String data = DatatypeConverter.printBase64Binary(baos.toByteArray());
    String imageString = "data:image/png;base64," + data;
    String html = "<html><body><img src='" + imageString + "'></body></html>";
    System.out.println(html);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();//from www  .ja va  2s  .  co  m
    document.add(new Paragraph("Hello World"));
    document.close();
    PdfReader reader = new PdfReader("2.pdf");
    System.out.println("Tampered? " + reader.isTampered());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("2.pdf"));
    HashMap info = reader.getInfo();
    info.put("Subject", "subject");
    info.put("Author", "author");
    info.put("Keywords", "keywords");
    info.put("Title", "title");
    info.put("Creator", "creator");
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
}

From source file:HelloWorldAddMetadata.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
    document.open();// w w w  .j av  a 2 s.  c o  m
    document.add(new Paragraph("Hello World"));
    document.close();

    PdfReader reader = new PdfReader("HelloWorld.pdf");
    System.out.println("Tampered? " + reader.isTampered());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("HelloWorldStampedMetadata.pdf"));
    System.out.println("Tampered? " + reader.isTampered());
    HashMap<String, String> info = reader.getInfo();
    info.put("Subject", "Hello World");
    info.put("Author", "your name");
    info.put("Keywords", "iText pdf");
    info.put("Title", "Hello World stamped");
    info.put("Creator", "your name");
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
}

From source file:Main.java

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

    byte[] buf = { 87, 64, 72, 31, 90 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream dos = new DataOutputStream(baos);

    dos.write(buf, 2, 3);/*from w  ww  .  jav a2s . co m*/

    dos.flush();

    for (byte b : baos.toByteArray()) {
        System.out.println(b);
    }
}

From source file:Main.java

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

    byte[] bs = { 65, 66, 67, 68, 69 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // write byte array to the output stream
    baos.write(bs, 3, 2);// w  ww  .  j a  va2s .c  o  m

    // read all the bytes in the output stream
    for (byte b : baos.toByteArray()) {
        System.out.println(b);
    }

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String sessionCookie = null;//from ww  w.  j av a2 s .  co  m
    URL url = new java.net.URL("http://127.0.0.1/yourServlet");
    URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    System.out.println(count);
}

From source file:Main.java

public static void main(String[] arg) throws Throwable {
    File f = new File(arg[0]);
    InputStream in = new FileInputStream(f);

    byte[] buff = new byte[8000];

    int bytesRead = 0;

    ByteArrayOutputStream bao = new ByteArrayOutputStream();

    while ((bytesRead = in.read(buff)) != -1) {
        bao.write(buff, 0, bytesRead);/*from ww w.j  av a  2  s  .  com*/
    }
    in.close();

    byte[] data = bao.toByteArray();

    ByteArrayInputStream bin = new ByteArrayInputStream(data);
    System.out.println(bin.available());
}

From source file:Main.java

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

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedOutputStream bos = new BufferedOutputStream(baos);

    int b = 87;/*from   w w  w. j  ava2 s.c om*/

    bos.write(b);

    bos.flush();

    byte[] bytes = baos.toByteArray();

    System.out.println(bytes[0]);

}