Example usage for java.io ObjectOutputStream flush

List of usage examples for java.io ObjectOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

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);//  w  w w  .  j a  va  2s.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:SerialDemo.java

static public void main(String[] args) {
    try {//from  w w  w.  j  a v  a2 s.c  om
        { // Save a SerialDemo object with a value of 5.
            FileOutputStream f = new FileOutputStream("/tmp/testing");
            ObjectOutputStream s = new ObjectOutputStream(f);
            SerialDemo d = new SerialDemo(5);

            s.writeObject(d);
            s.flush();
        }
        { // Now restore it and look at the value.
            FileInputStream f = new FileInputStream("/tmp/testing");
            ObjectInputStream s = new ObjectInputStream(f);
            SerialDemo d = (SerialDemo) s.readObject();

            System.out.println("SerialDemo.getVal() is: " + d.getVal());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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);/* w ww  . j av  a  2s.co m*/
    oos.writeObject(foo);
    oos.flush();
    oos.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String WRITE_OBJECT_SQL = "BEGIN " + "  INSERT INTO java_objects(object_id, object_name, object_value) "
            + "  VALUES (?, ?, empty_blob()) " + "  RETURN object_value INTO ?; " + "END;";
    String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?";

    Connection conn = getOracleConnection();
    conn.setAutoCommit(false);//from w ww . j av  a2 s. co m
    List<Object> list = new ArrayList<Object>();
    list.add("This is a short string.");
    list.add(new Integer(1234));
    list.add(new java.util.Date());

    // write object to Oracle
    long id = 0001;
    String className = list.getClass().getName();
    CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL);

    cstmt.setLong(1, id);
    cstmt.setString(2, className);

    cstmt.registerOutParameter(3, java.sql.Types.BLOB);

    cstmt.executeUpdate();
    BLOB blob = (BLOB) cstmt.getBlob(3);
    OutputStream os = blob.getBinaryOutputStream();
    ObjectOutputStream oop = new ObjectOutputStream(os);
    oop.writeObject(list);
    oop.flush();
    oop.close();
    os.close();

    // Read object from oracle
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    InputStream is = rs.getBlob(1).getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object object = oip.readObject();
    className = object.getClass().getName();
    oip.close();
    is.close();
    rs.close();
    pstmt.close();
    conn.commit();

    // de-serialize list a java object from a given objectID
    List listFromDatabase = (List) object;
    System.out.println("[After De-Serialization] list=" + listFromDatabase);
    conn.close();
}

From source file:TestCipher.java

public static void main(String args[]) throws Exception {
    Set set = new HashSet();
    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        Point point = new Point(random.nextInt(1000), random.nextInt(2000));
        set.add(point);// w w  w  .j  a va  2s. c o m
    }
    int last = random.nextInt(5000);

    // Create Key
    byte key[] = password.getBytes();
    DESKeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

    // Create Cipher
    Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    // Create stream
    FileOutputStream fos = new FileOutputStream("out.des");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    ObjectOutputStream oos = new ObjectOutputStream(cos);

    // Write objects
    oos.writeObject(set);
    oos.writeInt(last);
    oos.flush();
    oos.close();

    // Change cipher mode
    desCipher.init(Cipher.DECRYPT_MODE, secretKey);

    // Create stream
    FileInputStream fis = new FileInputStream("out.des");
    BufferedInputStream bis = new BufferedInputStream(fis);
    CipherInputStream cis = new CipherInputStream(bis, desCipher);
    ObjectInputStream ois = new ObjectInputStream(cis);

    // Read objects
    Set set2 = (Set) ois.readObject();
    int last2 = ois.readInt();
    ois.close();

    // Compare original with what was read back
    int count = 0;
    if (set.equals(set2)) {
        System.out.println("Set1: " + set);
        System.out.println("Set2: " + set2);
        System.out.println("Sets are okay.");
        count++;
    }
    if (last == last2) {
        System.out.println("int1: " + last);
        System.out.println("int2: " + last2);
        System.out.println("ints are okay.");
        count++;
    }
    if (count != 2) {
        System.out.println("Problem during encryption/decryption");
    }
}

From source file:com.textocat.textokit.morph.opencorpora.resource.XmlDictionaryPSP.java

public static void main(String[] args) throws Exception {
    XmlDictionaryPSP cfg = new XmlDictionaryPSP();
    new JCommander(cfg, args);

    MorphDictionaryImpl dict = new MorphDictionaryImpl();
    DictionaryExtension ext = cfg.dictExtensionClass.newInstance();
    FileInputStream fis = FileUtils.openInputStream(cfg.dictXmlFile);
    try {//from  ww  w.j  a  v  a 2  s  .  c o  m
        new XmlDictionaryParser(dict, ext, fis).run();
    } finally {
        IOUtils.closeQuietly(fis);
    }

    System.out.println("Preparing to serialization...");
    long timeBefore = currentTimeMillis();
    OutputStream fout = new BufferedOutputStream(FileUtils.openOutputStream(cfg.outputJarFile), 8192 * 8);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(Attributes.Name.MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VERSION,
            dict.getVersion());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_REVISION,
            dict.getRevision());
    manifest.getMainAttributes().putValue(OpencorporaMorphDictionaryAPI.ME_OPENCORPORA_DICTIONARY_VARIANT,
            cfg.variant);
    String dictEntryName = String.format(
            OpencorporaMorphDictionaryAPI.FILENAME_PATTERN_OPENCORPORA_SERIALIZED_DICT, dict.getVersion(),
            dict.getRevision(), cfg.variant);
    JarOutputStream jarOut = new JarOutputStream(fout, manifest);
    jarOut.putNextEntry(new ZipEntry(dictEntryName));
    ObjectOutputStream serOut = new ObjectOutputStream(jarOut);
    try {
        serOut.writeObject(dict.getGramModel());
        serOut.writeObject(dict);
    } finally {
        serOut.flush();
        jarOut.closeEntry();
        serOut.close();
    }
    System.out.println(String.format("Serialization finished in %s ms.\nOutput size: %s bytes",
            currentTimeMillis() - timeBefore, cfg.outputJarFile.length()));
}

From source file:CardWriter.java

public static void main(String[] args) {
    Card3 card = new Card3(12, Card3.SPADES);
    System.out.println("Card to write is: " + card);

    try {/* www  .  j av a2  s  .c o  m*/
        FileOutputStream out = new FileOutputStream("card.out");
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(card);
        oos.flush();
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }
}

From source file:CardReader.java

public static void main(String[] args) {
    Card3 card = new Card3(12, Card3.SPADES);
    System.out.println("Card to write is: " + card);

    try {// w  w w .  j  av  a2  s.co  m
        FileOutputStream out = new FileOutputStream("card.out");
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(card);
        oos.flush();
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }

    Card3 acard = null;

    try {
        FileInputStream in = new FileInputStream("card.out");
        ObjectInputStream ois = new ObjectInputStream(in);
        acard = (Card3) (ois.readObject());
    } catch (Exception e) {
        System.out.println("Problem serializing: " + e);
    }

    System.out.println("Card read is: " + acard);

}

From source file:tcp.TCP.java

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

    JSONParser parser = new JSONParser();

    JSONArray a = (JSONArray) parser.parse(new FileReader("saida.json"));

    for (Object o : a) {
        JSONObject object = (JSONObject) o;

        String name = (String) object.get("name");

        String city = (String) object.get("city");

        String job = (String) object.get("job");

        JSONArray cars = (JSONArray) object.get("cars");

        try ( // Cria um ServerSocket que vai se conectar com sockets

                ServerSocket servidor = new ServerSocket(1236)) {
            System.out.println("Aguardando conexo");
            // Cria um Socket que  definido como a abertura de conexo com o Server
            Socket cliente = servidor.accept();
            System.out.println("Conectou");
            //Escreve a casca da saida,  aqui que voc define onde o que sera enviado vai ficar
            ObjectOutputStream sai = new ObjectOutputStream(cliente.getOutputStream());
            // String que vai ser colocada na casca
            String info = ("name: " + name + "  city: " + city + "  job: " + job + "  cars: " + cars);
            // Envio da string encapsulada
            sai.writeObject(info);/*w  w w.j a v a2  s. c o m*/
            // Limpa o object
            sai.flush();
        }
    }
}

From source file:Person.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("yourFile.dat"));

    Person person = new Person();
    person.setFirstName("A");
    person.setLastName("B");
    person.setAge(38);/*from  www .  jav a  2 s  . com*/
    outputStream.writeObject(person);

    person = new Person();
    person.setFirstName("C");
    person.setLastName("D");
    person.setAge(22);
    outputStream.writeObject(person);

    outputStream.flush();

    outputStream.close();
}