From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../hjp5/examples/Listing4111.java | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java') diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java new file mode 100644 index 0000000..c847a7f --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java @@ -0,0 +1,93 @@ +/* Listing4111.java */ + +import java.io.*; +import java.util.*; + +public class Listing4111 +{ + public static Object seriaClone(Object o) + throws IOException, ClassNotFoundException + { + //Serialisieren des Objekts + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ObjectOutputStream os = new ObjectOutputStream(out); + os.writeObject(o); + os.flush(); + //Deserialisieren des Objekts + ByteArrayInputStream in = new ByteArrayInputStream( + out.toByteArray() + ); + ObjectInputStream is = new ObjectInputStream(in); + Object ret = is.readObject(); + is.close(); + os.close(); + return ret; + } + + public static void main(String[] args) + { + try { + //Erzeugen des Buchobjekts + Book book = new Book(); + book.author = "Peitgen, Heinz-Otto"; + String[] s = {"Jürgens, Hartmut", "Saupe, Dietmar"}; + book.coAuthors = s; + book.title = "Bausteine des Chaos"; + book.publisher = "rororo science"; + book.pubyear = 1998; + book.pages = 514; + book.isbn = "3-499-60250-4"; + book.reflist = new Vector(); + book.reflist.addElement("The World of MC Escher"); + book.reflist.addElement( + "Die fraktale Geometrie der Natur" + ); + book.reflist.addElement("Gödel, Escher, Bach"); + System.out.println(book.toString()); + //Erzeugen und Verändern der Kopie + Book copy = (Book)seriaClone(book); + copy.title += " - Fraktale"; + copy.reflist.addElement("Fractal Creations"); + //Ausgeben von Original und Kopie + System.out.print(book.toString()); + System.out.println("---"); + System.out.print(copy.toString()); + } catch (IOException e) { + System.err.println(e.toString()); + } catch (ClassNotFoundException e) { + System.err.println(e.toString()); + } + } +} + +class Book +implements Serializable +{ + public String author; + public String[] coAuthors; + public String title; + public String publisher; + public int pubyear; + public int pages; + public String isbn; + public Vector reflist; + + public String toString() + { + String NL = System.getProperty("line.separator"); + StringBuffer ret = new StringBuffer(200); + ret.append(author + NL); + for (int i = 0; i < coAuthors.length; ++i) { + ret.append(coAuthors[i] + NL); + } + ret.append("\"" + title + "\"" + NL); + ret.append(publisher + " " + pubyear + NL); + ret.append(pages + " pages" + NL); + ret.append(isbn + NL); + Enumeration e = reflist.elements(); + while (e.hasMoreElements()) { + ret.append(" " + (String)e.nextElement() + NL); + } + return ret.toString(); + } +} \ No newline at end of file -- cgit v1.2.3