Projekt

Obecné

Profil

Stáhnout (4.07 KB) Statistiky
| Větev: | Tag: | Revize:
1
package app;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.ObjectInputStream;
8
import java.io.ObjectOutputStream;
9
import java.util.ArrayList;
10
import jdeserialize.Getopt;
11
import jdeserialize.classdesc;
12
import jdeserialize.content;
13
import jdeserialize.jdeserialize;
14

    
15
public class App {
16

    
17
    /**
18
     * When the Serialized and deserialized classes are same, but placed in diff packages, the
19
     * deserialization does not work. When they are
20
     **/
21
    public static void main(String[] args) throws Exception {
22
        App app = new App();
23

    
24
        // app.standartSerializationDeserialization();
25

    
26
        // https://code.google.com/archive/p/jdeserialize/wikis/Documentation.wiki
27
        //test with calling the name of original jd project
28
        //app.jdCallMain();
29

    
30
        // test directly through code call
31
        app.jdCallLib("serialized/simple.out");
32
    }
33

    
34
    private void jdCallLib(String filename)
35
    {
36
        try
37
        {
38
            //FileInputStream as usual
39
            FileInputStream fis = null;
40
            fis = new FileInputStream(filename);
41
            
42
            //creating an instance of jdeserialize with given filename
43
            jdeserialize jd = new jdeserialize(filename);
44
            //running the main process of deserialization
45
            jd.run(fis, false);
46
            
47
            //gets the "contents" into an array - returnes the deserialization of all
48
            // 'writes' into serialized object via objectOutputStream
49
            ArrayList<content> cntnts = (ArrayList<content>) jd.getContent();
50

    
51
            //gets first object of content type
52
            // - tested only with just one object.
53
            // the content here is just an interface.
54
            // The classes that implements the interface content (all of them through class contentbase)
55
            // are: arrayobj, blockdata, classdesc, classobj, enumobj, exceptionstate, instance, stringobj
56
            content locContent = cntnts.get(0);
57

    
58
            classdesc objectDescription = locContent.
59

    
60

    
61
            int c = 0;
62
        }
63
        catch (IOException e) 
64
        {
65

    
66
        }
67
    }
68

    
69
    private void jdCallMain(String filename)
70
    {
71
        String[] myArgs= new String[1];
72
        myArgs[0] = filename;
73
        jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console.
74
    }
75

    
76
    private void standartSerializationDeserialization() {
77
        try {
78
            System.out.println("Starting...");
79

    
80
            this.serialize(new samples.ok.Simple(), "serialized/simple.out");
81

    
82
            samples.nok.Simple test1 = this.deserialize("serialized/simple.out");
83

    
84
        } catch (Exception e) {
85
            System.out.println(e.toString());
86
            System.out.println(e.getMessage());
87
        }
88
    }
89

    
90
    /**
91
     * Standard way of deserializing objects with java.
92
     * 
93
     * Taken from example method, doc
94
     **/
95
    private <T> T deserialize(String inputFilePath) {
96
        try {
97
            FileInputStream fis = new FileInputStream(inputFilePath);
98
            ObjectInputStream ois = new ObjectInputStream(fis);
99
            T deserialized = (T) ois.readObject();
100
            System.out.println(deserialized.toString());
101
            ois.close();
102

    
103
            return deserialized;
104
        } catch (IOException e) {
105
            e.printStackTrace();
106
        } catch (ClassNotFoundException e) {
107
            e.printStackTrace();
108
        }
109
        return null;
110
    }
111

    
112
    /**
113
     * Standard way of serializing objects with java.
114
     * 
115
     * Taken from example method, doc
116
     **/
117
    private void serialize(Object obj, String outputPath) {
118
        try {
119
            File toWrite = new File(outputPath);
120
            toWrite.createNewFile();
121

    
122
            FileOutputStream fos = new FileOutputStream(toWrite, false);
123
            ObjectOutputStream oos = new ObjectOutputStream(fos);
124

    
125
            oos.writeObject(obj);
126
            oos.flush();
127
            oos.close();
128
            System.out.println("serialized.");
129
        } catch (Exception e) {
130
            e.printStackTrace();
131
        }
132
    }
133
}
    (1-1/1)