Skip to content

Commit e919bb5

Browse files
author
yuki
committed
[wip]ch 18.13
1 parent be0c04b commit e919bb5

14 files changed

Lines changed: 629 additions & 1 deletion

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# idea
22
.idea/
3-
*.iml
3+
*.iml
4+
target/**

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
103103
<version>1.0.13</version>
104104
</dependency>
105105

106+
<!-- https://mvnrepository.com/artifact/nu.xom/com.springsource.nu.xom -->
107+
<dependency>
108+
<groupId>nu.xom</groupId>
109+
<artifactId>com.springsource.nu.xom</artifactId>
110+
<version>1.2.5</version>
111+
</dependency>
106112

107113
</dependencies>
108114
</project>

src/java/code/ch18/Alien.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package code.ch18;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class Alien implements Serializable {
10+
11+
12+
}

src/java/code/ch18/Blip3.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package code.ch18;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class Blip3 implements Externalizable {
10+
11+
private int i;
12+
private String s;
13+
14+
public Blip3() {
15+
System.out.println("blip3 constructor");
16+
}
17+
18+
public Blip3(int i, String s) {
19+
System.out.println("blip3(int i, String s) constructor");
20+
this.i = i;
21+
this.s = s;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return s + i;
27+
}
28+
29+
@Override
30+
public void writeExternal(ObjectOutput out) throws IOException {
31+
System.out.println("blip3 writeExternal");
32+
out.writeObject(s);
33+
out.writeInt(i);
34+
}
35+
36+
@Override
37+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
38+
System.out.println("blip3 readExternal");
39+
s = (String) in.readObject();
40+
i = in.readInt();
41+
}
42+
43+
public static void main(String[] args) throws IOException, ClassNotFoundException {
44+
System.out.println("constructing objects:");
45+
Blip3 blip3 = new Blip3(47, "A String");
46+
System.out.println(blip3);
47+
ObjectOutputStream out = new ObjectOutputStream(
48+
new FileOutputStream("Blip3.out")
49+
);
50+
51+
System.out.println("saving b3:");
52+
out.writeObject(blip3);
53+
out.close();
54+
55+
System.out.println("recovering b3: ");
56+
ObjectInputStream in = new ObjectInputStream(
57+
new FileInputStream("Blip3.out")
58+
);
59+
blip3 = (Blip3) in.readObject();
60+
System.out.println(blip3);
61+
}
62+
63+
}

src/java/code/ch18/Blips.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package code.ch18;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class Blips {
10+
11+
public static void main(String[] args) throws IOException, ClassNotFoundException {
12+
System.out.println("Constructing Objects:");
13+
Blip1 b1 = new Blip1();
14+
Blip2 b2 = new Blip2();
15+
ObjectOutputStream o = new ObjectOutputStream(
16+
new FileOutputStream("Blips.out")
17+
);
18+
System.out.println("Saving objects:");
19+
o.writeObject(b1);
20+
o.writeObject(b2);
21+
o.close();
22+
23+
ObjectInputStream in = new ObjectInputStream(
24+
new FileInputStream("Blips.out")
25+
);
26+
System.out.println("recovering b1:");
27+
b1 = (Blip1) in.readObject();
28+
System.out.println("recovering b2:");
29+
b2 = (Blip2) in.readObject();
30+
}
31+
32+
}
33+
34+
class Blip1 implements Externalizable {
35+
36+
public Blip1() {
37+
System.out.println("Blip1 Constructor");
38+
}
39+
40+
@Override
41+
public void writeExternal(ObjectOutput out) throws IOException {
42+
System.out.println("Blip1.writeExternal");
43+
}
44+
45+
@Override
46+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
47+
System.out.println("Blip1.readExternal");
48+
}
49+
}
50+
51+
class Blip2 implements Externalizable {
52+
53+
Blip2() {
54+
System.out.println("Blip2 Constructor");
55+
}
56+
57+
@Override
58+
public void writeExternal(ObjectOutput out) throws IOException {
59+
System.out.println("Blip2.writeExternal");
60+
}
61+
62+
@Override
63+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
64+
System.out.println("Blip2.readExternal");
65+
}
66+
}
67+
68+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package code.ch18;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class FreezeAlien {
10+
11+
public static void main(String[] args) throws IOException {
12+
ObjectOutput out = new ObjectOutputStream(
13+
new FileOutputStream("X.file")
14+
);
15+
16+
Alien quellek = new Alien();
17+
out.writeObject(quellek);
18+
}
19+
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package code.ch18;
2+
3+
import java.io.*;
4+
import java.util.zip.GZIPInputStream;
5+
import java.util.zip.GZIPOutputStream;
6+
7+
/**
8+
* @author yuzhe
9+
* @since 10/16/18
10+
*/
11+
public class GZIPCompress {
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader in = new BufferedReader(
15+
new FileReader("src/resources/ch18/data.txt")
16+
);
17+
BufferedOutputStream out = new BufferedOutputStream(
18+
new GZIPOutputStream(
19+
new FileOutputStream("src/resources/ch18/data.txt.gz")
20+
)
21+
);
22+
System.out.println("writing file");
23+
int c;
24+
while ((c = in.read()) != -1) {
25+
out.write(c);
26+
}
27+
out.flush();
28+
in.close();
29+
out.close();
30+
System.out.println("reading file:");
31+
BufferedReader in2 = new BufferedReader(
32+
new InputStreamReader(new GZIPInputStream(
33+
new FileInputStream("src/resources/ch18/data.txt.gz")
34+
))
35+
);
36+
String s;
37+
while ((s = in2.readLine()) != null) {
38+
System.out.println(s);
39+
}
40+
in2.close();
41+
}
42+
43+
}

src/java/code/ch18/Person.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package code.ch18;
2+
3+
import nu.xom.Element;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class Person {
10+
private String first, last;
11+
12+
public Person(String f, String l) {
13+
this.first = f;
14+
this.last = l;
15+
}
16+
17+
public Element getXML(){
18+
Element person = new Element("person");
19+
Element firstName = new Element("first");
20+
firstName.appendChild(first);
21+
Element lastName = new Element("last");
22+
lastName.appendChild(last);
23+
person.appendChild(firstName);
24+
person.appendChild(lastName);
25+
return person;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package code.ch18;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectInputStream;
6+
import java.util.List;
7+
8+
/**
9+
* @author yuzhe
10+
* @since 10/17/18
11+
*/
12+
public class RecoverCADState {
13+
14+
public static void main(String[] args) throws IOException, ClassNotFoundException {
15+
ObjectInputStream in = new ObjectInputStream(new FileInputStream("CADState.out"));
16+
List<Class<? extends Shape>> shapeTypes = (List<Class<? extends Shape>>) in.readObject();
17+
Line.deserializeStaticState(in);
18+
List<Shape> shapes = (List<Shape>) in.readObject();
19+
System.out.println(shapes);
20+
}
21+
22+
}

src/java/code/ch18/SerialCtl.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package code.ch18;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 10/17/18
8+
*/
9+
public class SerialCtl implements Serializable {
10+
11+
private String a;
12+
private transient String b;
13+
14+
public SerialCtl(String aa, String bb) {
15+
a = "not transient: " + aa;
16+
b = "transient: " + bb;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return a + "\t" + b;
22+
}
23+
24+
private void writeObject(ObjectOutputStream stream) throws IOException {
25+
stream.defaultWriteObject();
26+
stream.writeObject(b);
27+
}
28+
29+
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException{
30+
stream.defaultReadObject();
31+
b = (String) stream.readObject();
32+
}
33+
34+
public static void main(String[] args) throws IOException, ClassNotFoundException {
35+
SerialCtl sc = new SerialCtl("test1", "test2");
36+
System.out.println("before:" + sc);
37+
ByteArrayOutputStream buf = new ByteArrayOutputStream();
38+
ObjectOutputStream out = new ObjectOutputStream(buf);
39+
out.writeObject(sc);
40+
41+
ObjectInputStream in = new ObjectInputStream(
42+
new ByteArrayInputStream(buf.toByteArray())
43+
);
44+
SerialCtl sc2 = (SerialCtl) in.readObject();
45+
System.out.println("After: " + sc2);
46+
}
47+
}

0 commit comments

Comments
 (0)