Copy cat > /tmp/AdvLab09.java << 'JAVAEOF'
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.nio.file.*;
public class AdvLab09 {
static class Product implements Serializable {
@Serial private static final long serialVersionUID = 1L;
private final int id;
private final String name;
private final double price;
private transient String cacheKey; // excluded from serialization
private static int instanceCount = 0; // class-level, not serialized
Product(int id, String name, double price) {
this.id=id; this.name=name; this.price=price;
this.cacheKey = "p:" + id;
instanceCount++;
}
@Override public String toString() {
return "Product{id=" + id + ",name=" + name + ",price=" + price + ",cache=" + cacheKey + "}"; }
// Custom serialization: restore transient after deserialization
@Serial private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject(); // serialize non-transient fields
}
@Serial private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
cacheKey = "p:" + id + ":restored"; // rebuild transient
}
}
// Manual binary protocol
static byte[] encode(List<int[]> products) throws IOException {
var baos = new ByteArrayOutputStream();
try (var dos = new DataOutputStream(baos)) {
dos.writeInt(products.size());
for (var p : products) {
dos.writeInt(p[0]);
dos.writeDouble(p[1] / 100.0);
dos.writeInt(p[2]);
}
}
return baos.toByteArray();
}
static List<int[]> decode(byte[] data) throws IOException {
var result = new ArrayList<int[]>();
try (var dis = new DataInputStream(new ByteArrayInputStream(data))) {
int count = dis.readInt();
for (int i = 0; i < count; i++) {
result.add(new int[]{dis.readInt(), (int)(dis.readDouble()*100), dis.readInt()});
}
}
return result;
}
public static void main(String[] args) throws Exception {
System.out.println("=== Java Object Serialization ===");
var product = new Product(1, "Surface Pro", 864.0);
System.out.println("Before: " + product);
var baos = new ByteArrayOutputStream();
try (var oos = new ObjectOutputStream(baos)) { oos.writeObject(product); }
byte[] bytes = baos.toByteArray();
System.out.println("Serialized: " + bytes.length + " bytes");
try (var ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
var restored = (Product) ois.readObject();
System.out.println("Restored: " + restored);
}
// List serialization
var products = List.of(new Product(1,"Surface Pro",864.0), new Product(2,"Surface Pen",49.99));
baos.reset();
try (var oos = new ObjectOutputStream(baos)) { oos.writeObject(products); }
System.out.println("List serialized: " + baos.toByteArray().length + " bytes");
// Manual binary protocol
System.out.println("\n=== Manual Binary Protocol ===");
var data = List.of(new int[]{1,86400,15}, new int[]{2,4999,80}, new int[]{3,9999,999});
byte[] encoded = encode(data);
System.out.println("Encoded: " + encoded.length + " bytes (Java serial: " + baos.size() + " bytes)");
var decoded = decode(encoded);
decoded.forEach(p -> System.out.printf(" id=%d $%.2f stock=%d%n", p[0], p[1]/100.0, p[2]));
// Compression
System.out.println("\n=== GZIP Compression ===");
byte[] serialized = baos.toByteArray();
var compressed = new ByteArrayOutputStream();
try (var gzip = new GZIPOutputStream(compressed)) { gzip.write(serialized); }
byte[] compressedBytes = compressed.toByteArray();
System.out.println("Original: " + serialized.length + " bytes");
System.out.printf("GZIP: %d bytes (%.0f%%)%n",
compressedBytes.length, compressedBytes.length * 100.0 / serialized.length);
var decompressed = new ByteArrayOutputStream();
try (var gzip = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
gzip.transferTo(decompressed);
}
System.out.println("Decompressed: " + decompressed.toByteArray().length + " bytes (matches: " +
(decompressed.toByteArray().length == serialized.length) + ")");
// File persistence
System.out.println("\n=== File Persistence ===");
Path tmp = Files.createTempFile("innoz_", ".ser");
try (var oos = new ObjectOutputStream(new FileOutputStream(tmp.toFile()))) {
oos.writeObject(products.get(0));
}
System.out.println("Written: " + tmp.getFileName() + " (" + Files.size(tmp) + " bytes)");
try (var ois = new ObjectInputStream(new FileInputStream(tmp.toFile()))) {
var loaded = (Product) ois.readObject();
System.out.println("Loaded: " + loaded);
}
Files.delete(tmp);
// serialVersionUID
System.out.println("\n=== serialVersionUID ===");
System.out.println("Product UID: " + ObjectStreamClass.lookup(Product.class).getSerialVersionUID());
System.out.println(" (Must match across serialization/deserialization for class evolution)");
}
}
JAVAEOF
docker run --rm -v /tmp/AdvLab09.java:/tmp/AdvLab09.java zchencow/innozverse-java:latest sh -c "javac /tmp/AdvLab09.java -d /tmp && java -cp /tmp AdvLab09"