55
66import java .io .ByteArrayOutputStream ;
77import java .io .IOException ;
8+ import java .io .InvalidObjectException ;
9+ import java .io .ObjectInputStream ;
10+ import java .io .ObjectStreamException ;
11+ import java .io .Serializable ;
812import java .text .DecimalFormat ;
913import java .util .Arrays ;
1014import java .util .function .Supplier ;
15+ import lombok .extern .slf4j .Slf4j ;
1116import org .xbill .DNS .utils .base16 ;
1217
1318/**
1621 *
1722 * @author Brian Wellington
1823 */
19- public abstract class Record implements Cloneable , Comparable <Record > {
24+ @ Slf4j
25+ public abstract class Record implements Cloneable , Comparable <Record >, Serializable {
2026 protected Name name ;
21- protected int type , dclass ;
27+ protected int type ;
28+ protected int dclass ;
2229 protected long ttl ;
2330
2431 private static final DecimalFormat byteFormat = new DecimalFormat ();
@@ -27,6 +34,23 @@ public abstract class Record implements Cloneable, Comparable<Record> {
2734 byteFormat .setMinimumIntegerDigits (3 );
2835 }
2936
37+ private static class RecordSerializationProxy implements Serializable {
38+ private static final long serialVersionUID = 1434159920070152561L ;
39+ private final byte [] wireData ;
40+
41+ RecordSerializationProxy (Record r ) {
42+ wireData = r .toWire (Section .ANSWER );
43+ }
44+
45+ protected Object readResolve () throws ObjectStreamException {
46+ try {
47+ return Record .fromWire (wireData , Section .ANSWER );
48+ } catch (IOException e ) {
49+ throw new InvalidObjectException (e .getMessage ());
50+ }
51+ }
52+ }
53+
3054 protected Record () {}
3155
3256 /** @since 3.1 */
@@ -43,6 +67,15 @@ protected Record(Name name, int type, int dclass, long ttl) {
4367 this .ttl = ttl ;
4468 }
4569
70+ Object writeReplace () {
71+ log .trace ("Creating proxy object for serialization" );
72+ return new RecordSerializationProxy (this );
73+ }
74+
75+ private void readObject (ObjectInputStream ois ) throws InvalidObjectException {
76+ throw new InvalidObjectException ("Use RecordSerializationProxy" );
77+ }
78+
4679 private static Record getEmptyRecord (Name name , int type , int dclass , long ttl , boolean hasData ) {
4780 Record rec ;
4881 if (hasData ) {
@@ -169,7 +202,8 @@ public static Record newRecord(Name name, int type, int dclass) {
169202 }
170203
171204 static Record fromWire (DNSInput in , int section , boolean isUpdate ) throws IOException {
172- int type , dclass ;
205+ int type ;
206+ int dclass ;
173207 long ttl ;
174208 int length ;
175209 Name name ;
@@ -404,12 +438,7 @@ protected static String byteArrayToString(byte[] array, boolean quote) {
404438
405439 /** Converts a byte array into the unknown RR format. */
406440 protected static String unknownToString (byte [] data ) {
407- StringBuilder sb = new StringBuilder ();
408- sb .append ("\\ # " );
409- sb .append (data .length );
410- sb .append (" " );
411- sb .append (base16 .toString (data ));
412- return sb .toString ();
441+ return "\\ # " + data .length + " " + base16 .toString (data );
413442 }
414443
415444 /**
@@ -453,8 +482,7 @@ public static Record fromString(
453482 rec .rdataFromString (st , origin );
454483 t = st .get ();
455484 if (t .type != Tokenizer .EOL && t .type != Tokenizer .EOF ) {
456- throw st .exception (
457- "unexpected tokens at end of record (wanted EOL/EOF, got " + t .toString () + ")" );
485+ throw st .exception ("unexpected tokens at end of record (wanted EOL/EOF, got " + t + ")" );
458486 }
459487 return rec ;
460488 }
0 commit comments