/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  05.09.2007
   Letzte Änderung: 12.11.2010
*/


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

import java.util.*;

// Serialize1 mit einer Klasse
//  BnRead_Clicked
//  BnWrite_Clicked
// Ausgabe in einem Editor

public class Serialize6 extends JFrame {

  JTextArea _editor;
  ArrayList<Person> liste = new ArrayList<Person>();

  //Frame konstruieren
  public Serialize6() {
    setSize(600, 500);
    this.setTitle("Test mit Serialize5 udn DataOutputStream");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  void setGUI() {

     _editor = new JTextArea("");
    this.getContentPane().add(new JScrollPane(_editor), BorderLayout.CENTER);

    _editor.setFont( new Font("Arial", Font.BOLD,18)  );

     JButton BnRead = new JButton("Read");
     JButton BnWrite = new JButton("Write");


    JPanel panel = new JPanel();
    panel.setLayout ( new FlowLayout() );
    panel.add(BnRead);
    panel.add(BnWrite);
    this.getContentPane().add(panel, BorderLayout.SOUTH);
    BnRead.setFont( new Font("Arial", Font.BOLD,18)  );
    BnWrite.setFont( new Font("Arial", Font.BOLD,18)  );

    BnRead.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnRead_Clicked();
      }
    });
    BnWrite.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnWrite_Clicked();
      }
    });

  } // setGUI

  private void BnWrite_Clicked() {
     liste.clear(); 
     for (int i=0; i< 100000; i++) {
       liste.add( new Person("Fred"+i, "Müller"+i, "secret") );
     }
     writeSerialize();
     writeDataOutputStream();
  }

  private void writeSerialize() {  
      long t1, t2;    
      FileOutputStream fout;
      ObjectOutputStream oos = null;
      BufferedOutputStream bout;
      Person p;
      try {
          fout = new FileOutputStream("Personen.ser");
          bout = new BufferedOutputStream(fout);
          oos = new ObjectOutputStream(bout);
          t1 =  System.currentTimeMillis();
          oos.writeObject( new Integer(liste.size()) );
          for (int i=0; i<liste.size(); i++) {
            p = liste.get(i);
            oos.writeObject(p);
          }
          t2 =  System.currentTimeMillis();
          _editor.append("Zeit Serialize:: " + (t2-t1)+"\n\n" );
          oos.flush(); // Speicher leeren, auf Festplatte schreiben
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (oos != null) {
              try {
                oos.flush();
              }
              catch (IOException ioe) {}
              try {
                oos.close();
              } catch (IOException ioe) {}
          }
      }
  }  // writeSerialize

  private void writeDataOutputStream() {  
      long t1, t2;    
      FileOutputStream fout;
      DataOutputStream dout = null;
      BufferedOutputStream bout;
      try {
          fout = new FileOutputStream("Personen.bin");
          bout = new BufferedOutputStream(fout);
          dout = new DataOutputStream(bout);
          dout.writeInt(liste.size());
          t1 =  System.currentTimeMillis();
          for (Person p: liste) {
            p.save2File(dout);
          }
          t2 =  System.currentTimeMillis();
          _editor.append("Zeit DataOutputStream: " + (t2-t1)+"\n\n" );
          dout.flush(); // Speicher leeren, auf Festplatte schreiben
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (dout != null) {
              try {
                dout.flush();
              }
              catch (IOException ioe) {}
              try {
                dout.close();
              } catch (IOException ioe) {}
          }
      }
  }  // writeSerialize


  private void BnRead_Clicked() {
     liste.clear(); 
     readSerialize();
     readDataOutputStream();
  }

  private void readSerialize() {
    long t1, t2;    
    FileInputStream fin;
    ObjectInputStream ois = null;
    BufferedInputStream bin;
    try {
        fin = new FileInputStream("Personen.ser");
        bin = new BufferedInputStream(fin);
        ois = new ObjectInputStream( bin );
        Integer I = (Integer) ois.readObject();
        int n=I.intValue();
        t1 =  System.currentTimeMillis();
        for (int i=0; i<n; i++) {	
          Person p = (Person) ois.readObject();
          liste.add(p);
        }
        t2 =  System.currentTimeMillis();
        _editor.append("Zeit readSerialize: " + (t2-t1)+"\n\n" );
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (ois != null) {
            try {
              ois.close();
            } catch (IOException ioe) {}
        }
    }
  } // BnRead_Clicked



  private void readDataOutputStream() {
    long t1, t2;    
    FileInputStream fin;
    DataInputStream din = null;
    BufferedInputStream bin;
    Person p;
    try {
        fin = new FileInputStream("Personen.bin");
        bin = new BufferedInputStream(fin);
        din = new DataInputStream( bin );
        int n = din.readInt();
        t1 =  System.currentTimeMillis();
        for (int i=0; i<n; i++) {	
          p = new Person();
          p.readFromFile(din);
          liste.add(p);
        }
        t2 =  System.currentTimeMillis();
        _editor.append("Zeit readSerialize: " + (t2-t1)+"\n\n" );
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (din != null) {
            try {
              din.close();
            } catch (IOException ioe) {}
        }
    }
  } // BnRead_Clicked



  public static void main(String[] args) {
    Serialize6 frame = new Serialize6();
    frame.setVisible(true);
  }


}


class Person implements Serializable {

    private static final long serialVersionUID = 20080330L;

    private String firstName;
    private String lastName;
    private String password;
    private
    //transient 
    double betrag=2;
    /*
      Mit transient gekennzeichnete Variablen werden
      bei Serialisierungen zur persistenten Abspeicherung
      eines Objekts nicht mit gespeichert.
    */

    public Person() {
        this("","","");
    }

    public Person(String firstName, String lastName, String password) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        betrag=123;
    }

    public void save2File(DataOutputStream dout) {
      try {
        dout.writeUTF(firstName);
        dout.writeUTF(lastName);
        dout.writeUTF(password);
        dout.writeDouble(betrag);
      }
      catch ( IOException e) {}
    }

    public void readFromFile(DataInputStream din) {
      try {
        firstName = din.readUTF();
        lastName = din.readUTF();
        password = din.readUTF();
        betrag = din.readDouble();
      }
      catch ( IOException e) {}
    }

    public String toString() {
        return new String(" "+lastName + ", " + firstName+ "\n Passwort: " + password+" betrag:" +betrag);
    }

    public void setFirstname(String firstName) {
      this.firstName = firstName;
    }
    public void setLastname(String lastName) {
      this.lastName = lastName;
    }
    public void setPassword(String password) {
      this.password = password;
    }


    public String getFirstname() {
      return  firstName;
    }
    public String getLastname() {
      return  lastName;
    }
    public String getPassword() {
      return  password;
    }


}


