Tôi đang đọc sách Effective Java có ví dụ sau. Trong bản ví dụ tác giả bên dưới tham chiếu của các đối tượng có mặt trong ObjectOutputStream bởi dòng sauCách tham chiếu công việc trong Java
byte[] ref = {0x71, 0, 0x7e, 0, 5}; // Ref #5
Tại sao điểm tham chiếu này để các đối tượng có mặt ngày trong ObjectOutputStream? những gì được lưu trữ trong một tài liệu tham khảo?
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
final class Period {
private final Date start;
private final Date end;
/**
* @param start the beginning of the period
* @param end the end of the period; must not precede start * @throws IllegalArgumentException
* if start is after end
* @throws NullPointerException if start or end is null
*/
public Period(Date start, Date end) {
this.start = new Date(start.getTime());
this.end = new Date(end.getTime());
if (this.start.compareTo(this.end) > 0)
throw new IllegalArgumentException(start + " after " + end);
}
public Date start() {
return new Date(start.getTime());
}
public Date end() {
return new Date(end.getTime());
}
public String toString() {
return start + " - " + end;
}
// Remainder omitted
}
public class MutablePeriod {
// A period instance
public final Period period;
// period's start field, to which we shouldn't have access
public final Date start;
// period's end field, to which we shouldn't have access
public final Date end;
public MutablePeriod() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
// Serialize a valid Period instance
out.writeObject(new Period(new Date(), new Date()));
/*
* Append rogue "previous object refs" for internal * Date fields in Period. For
* details, see "Java Object Serialization Specification," Section 6.4.
*/
byte[] ref = {0x71, 0, 0x7e, 0, 5}; // Ref #5
bos.write(ref); // The start field
ref[4] = 4; // Ref#4
bos.write(ref); // The end field
// Deserialize Period and "stolen" Date references
ObjectInputStream in =
new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
period = (Period) in.readObject();
start = (Date) in.readObject();
end = (Date) in.readObject();
} catch (Exception e) {
throw new AssertionError(e);
}
}
}
Nhìn thoáng qua, có vẻ như họ đang viết các byte thô cho đối tượng rồi đọc nó? Vì vậy, có lẽ một cái gì đó họ đánh giá trước (ví dụ đã viết đối tượng ra, sao chép byte hơn vào mảng bằng tay) – Rogue
Bạn có thể muốn kiểm tra tài liệu tham chiếu trong nhận xét về mã của bạn: [Java Serialization Specification 6.4.2] (https://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html#10152) –
Tangent… Lớp 'Date' được thay thế bởi các lớp java.time như [' java.time.Instant '] (https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) và [' Period'] (https://docs.oracle.com/javase/8 /docs/api/java/time/Period.html) & ['Duration'] (https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html). Xem [Hướng dẫn] (http://docs.oracle.com/javase/tutorial/datetime/iso/period.html). Và [mã nguồn cho 'Thời lượng'] (http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/f7be58eb30bc/src/share/classes/java/time/Duration.java) triển khai' Serializable' . –