Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bael 1611 #3799

Merged
merged 3 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions core-java/src/main/java/com/baeldung/externalizable/Community.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.baeldung.externalizable;

import java.io.*;

public class Community implements Serializable {

private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "Community{" +
"id=" + id +
'}';
}
}
62 changes: 62 additions & 0 deletions core-java/src/main/java/com/baeldung/externalizable/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.externalizable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Country implements Externalizable {

private static final long serialVersionUID = 1L;

private String name;
private String capital;
private int code;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCapital() {
return capital;
}

public void setCapital(String capital) {
this.capital = capital;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
out.writeUTF(capital);
out.writeInt(code);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
this.name = in.readUTF();
this.capital = in.readUTF();
this.code = in.readInt();
}

@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
", capital='" + capital + '\'' +
", code=" + code +
'}';
}
}
57 changes: 57 additions & 0 deletions core-java/src/main/java/com/baeldung/externalizable/Region.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.externalizable;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Region extends Country implements Externalizable {

private static final long serialVersionUID = 1L;

private String climate;
private Double population;
private Community community;

public String getClimate() {
return climate;
}

public void setClimate(String climate) {
this.climate = climate;
}

public Double getPopulation() {
return population;
}

public void setPopulation(Double population) {
this.population = population;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeUTF(climate);
community = new Community();
community.setId(5);
out.writeObject(community);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
this.climate = in.readUTF();
community = (Community) in.readObject();
}

@Override
public String toString() {
return "Region = {" +
"country='" + super.toString() + '\'' +
"community='" + community.toString() + '\'' +
"climate='" + climate + '\'' +
", population=" + population +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.baeldung.externalizable;

import org.junit.Test;

import java.io.*;

import static org.junit.Assert.assertTrue;

public class ExternalizableUnitTest {

private final static String OUTPUT_FILE = "externalizable.txt";

@Test
public void whenSerializing_thenUseExternalizable() throws IOException, ClassNotFoundException {

Country c = new Country();
c.setCapital("Yerevan");
c.setCode(374);
c.setName("Armenia");

FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
c.writeExternal(objectOutputStream);

objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();

FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

Country c2 = new Country();
c2.readExternal(objectInputStream);

objectInputStream.close();
fileInputStream.close();

assertTrue(c2.getCode() == c.getCode());
assertTrue(c2.getName().equals(c.getName()));
}

@Test
public void whenInheritanceSerialization_then_UseExternalizable() throws IOException, ClassNotFoundException {

Region r = new Region();
r.setCapital("Yerevan");
r.setCode(374);
r.setName("Armenia");
r.setClimate("Mediterranean");
r.setPopulation(120.000);

FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
r.writeExternal(objectOutputStream);

objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();

FileInputStream fileInputStream = new FileInputStream(OUTPUT_FILE);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

Region r2 = new Region();
r2.readExternal(objectInputStream);

objectInputStream.close();
fileInputStream.close();

assertTrue(r2.getPopulation() == null);
}
}