File: SerialGraph.java

package info (click to toggle)
libjgraph-java 5.12.4.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 7,884 kB
  • ctags: 7,321
  • sloc: java: 20,619; xml: 135; sh: 51; makefile: 10
file content (81 lines) | stat: -rw-r--r-- 2,225 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * @(#)EditorGraph.java 3.3 23-APR-04
 *  
 * Copyright (c) 2001-2005, Gaudenz Alder
 * 
 * See LICENSE file in distribution for licensing details of this source file
 */
package com.jgraph.example;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JFrame;

import org.jgraph.JGraph;

/**
 * An example to demonstrate the use of serialization in JGraph. As of version
 * 5.7.5, the JGraph object is fully serializable. In a real-world situation,
 * the graph must be prepared for serialization as shown in GraphEdX. Writing
 * the graph to a file using the XMLEncoder is also demonstrated there.
 * 
 * @version 1.0 06-DEC-05
 * @author Gaudenz Alder
 */
public class SerialGraph {

	public static String FILENAME = "test.tmp";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Switch off D3D because of Sun XOR painting bug
		// See http://www.jgraph.com/forum/viewtopic.php?t=4066
		System.setProperty("sun.java2d.d3d", "false");
		JGraph graph = new JGraph();
		writeObject(graph, FILENAME);
		graph = (JGraph) readObject(FILENAME);
		JFrame frame = new JFrame("SerialGraph");
		frame.getContentPane().add(graph);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public static void writeObject(Object object, String filename) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(filename)));
			out.writeObject(object);
			out.flush();
			out.close();
			File file = new File(filename);
			System.out.println("File size is " + file.length() + " byte(s)");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Object readObject(String filename) {
		try {
			ObjectInputStream in = new ObjectInputStream(
					new BufferedInputStream(new FileInputStream(filename)));
			Object object = in.readObject();
			in.close();
			File file = new File(filename);
			file.delete();
			return object;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}