package cn.CodeSnippet.snippets.core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Serializable;
class SerialIntList implements Serializable {
protected int[] intArray = new int[8];
protected transient int length = 0;
public int get(int ind) {
if (ind >= length) {
throw new ArrayIndexOutOfBoundsException(ind);
} else {
return intArray[ind];
}
}
public void add(int i) {
if (intArray.length == length) {
resize(intArray.length * 2);
}
intArray[length++] = i;
}
protected void resize(int size) {
int[] newdata = new int[size];
System.arraycopy(intArray, 0, newdata, 0, length); // Copy array elements.
intArray = newdata; // Replace old array
}
private void writeObject(ObjectOutputStream out) throws IOException {
if (intArray.length > length) {
resize(length); // Compact the array.
}
out.defaultWriteObject(); // Then write it out normally.
}
@Override
public boolean equals(Object o) {
if (!(o instanceof SerialIntList)) {
return false;
}
SerialIntList that = (SerialIntList) o;
if (this.length != that.length) {
return false;
}
for (int i = 0; i < this.length; i++) {
if (this.intArray[i] != that.intArray[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int code = 1;
for (int i = 0; i < length; i++) {
code = code * 997 + intArray[i];
}
return code;
}
}
public class Main {
static void store(Serializable o, File f) throws IOException {
ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream(f));
ostream.writeObject(o);
ostream.close();
}
static Object load(File f) throws IOException, ClassNotFoundException {
ObjectInputStream instream = new ObjectInputStream(new FileInputStream(f));
return instream.readObject();
}
static Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {
final PipedOutputStream pipeout = new PipedOutputStream();
PipedInputStream pipein = new PipedInputStream(pipeout);
Thread writer = new Thread() {
@Override
public void run() {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(pipeout);
out.writeObject(o);
} catch (IOException e) {
} finally {
try {
out.close();
} catch (Exception e) {
}
}
}
};
// Start serializing and writing
writer.start();
ObjectInputStream in = new ObjectInputStream(pipein);
return in.readObject();
}
static class DataStructure implements Serializable {
String message;
int[] data;
DataStructure other;
@Override
public String toString() {
String s = message;
for (int i = 0; i < data.length; i++) {
s += " " + data[i];
}
if (other != null) {
s += "\\n\\t" + other.toString();
}
return s;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
DataStructure structure = new DataStructure();
structure.message = "Java Code Geeks rocks!";
structure.data = new int[]{1, 2, 3, 4, 5};
structure.other = new DataStructure();
structure.other.message = "JavaCodeGeeks is the best!";
structure.other.data = new int[]{9, 8, 7};
System.out.println("Data: " + structure);
File f = new File("C:/Users/nikos7/Desktop/output2.txt");
System.out.println("Save to file");
Main.store(structure, f);
structure = (DataStructure) Main.load(f);
System.out.println("Read : " + structure);
DataStructure ds2 = (DataStructure) Main.deepclone(structure);
structure.other.message = null;
structure.other.data = null;
System.out.println("Deepcloning: " + ds2);
}
}