Java 17 Recipes


-2. Serializing Java Objects More Efficiently


Download 3.2 Mb.
Pdf ko'rish
bet153/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   149   150   151   152   153   154   155   156   ...   245
Bog'liq
Java 17 Recipes

8-2. Serializing Java Objects More Efficiently
 Problem
You want to serialize a class but make the output more efficient or smaller in size than 
the product generated via the built-in serialization method.
ChApTer 8 InpuT And OuTpuT


301
 Solution
By making the object implement the Externalizable interface, you instruct the Java 
Virtual Machine to use a custom serialization/deserialization mechanism, as provided 
by the readExternal/writeExternal methods in the following example.
public class ExternalizableProgramSettings implements Externalizable {
private Point locationOnScreen;
private Dimension frameSize;
private Color defaultFontColor;
private String title;
// Empty constructor, required for Externalizable implementors
public ExternalizableProgramSettings() {
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(locationOnScreen.x);
out.writeInt(locationOnScreen.y);
out.writeInt(frameSize.width);
out.writeInt(frameSize.height);
out.writeInt(defaultFontColor.getRGB());
out.writeUTF(title);
}
@Override
public void readExternal(ObjectInput in) throws IOException
ClassNotFoundException {
locationOnScreen = new Point(in.readInt(), in.readInt());
frameSize = new Dimension(in.readInt(), in.readInt());
defaultFontColor = new Color(in.readInt());
title = in.readUTF();
}
// getters and setters omitted for brevity
}
ChApTer 8 InpuT And OuTpuT


302
The main class is:
package org.java17recipes.chapter08.recipe08_02;
import java.awt.*;
import java.io.*;
public class Ch_8_2_ExternalizableExample {
public static void main(String[] args) {
Ch_8_2_ExternalizableExample example = new Ch_8_2_
ExternalizableExample();
example.start();
}
private void start() {
ExternalizableProgramSettings settings = new ExternalizableProgram
Settings(new Point(10,10),new Dimension(300,200), Color.blue, "The 
title of the application" );
saveSettings(settings,"settingsExternalizable.bin");
ExternalizableProgramSettings loadedSettings = loadSettings("setting
sExternalizable.bin");
System.out.println("Are settings are equal? :"+loadedSettings.
equals(settings));
}
private void saveSettings(ExternalizableProgramSettings settings, String 
filename) {
try {
FileOutputStream fos = new FileOutputStream(filename);
try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(settings);
}
} catch (IOException e) {
e.printStackTrace();
}
}
ChApTer 8 InpuT And OuTpuT


303
private ExternalizableProgramSettings loadSettings(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fis);
return (ExternalizableProgramSettings) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   149   150   151   152   153   154   155   156   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling