summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java')
-rw-r--r--Master/Reference Architectures and Patterns/hjp5/examples/Listing4111.java93
1 files changed, 93 insertions, 0 deletions
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