diff options
Diffstat (limited to 'src/main/java/de/measite/minidns/Question.java')
-rw-r--r-- | src/main/java/de/measite/minidns/Question.java | 73 |
1 files changed, 46 insertions, 27 deletions
diff --git a/src/main/java/de/measite/minidns/Question.java b/src/main/java/de/measite/minidns/Question.java index 9d1e3f56..8efe19f1 100644 --- a/src/main/java/de/measite/minidns/Question.java +++ b/src/main/java/de/measite/minidns/Question.java @@ -11,52 +11,71 @@ import de.measite.minidns.util.NameUtil; public class Question { - private String name; + private final String name; - private TYPE type; + private final TYPE type; - private CLASS clazz = CLASS.IN; + private final CLASS clazz; - public TYPE getType() { - return type; - } + private byte[] byteArray; - public void setType(TYPE type) { + public Question(String name, TYPE type, CLASS clazz) { + this.name = name; this.type = type; + this.clazz = clazz; } - public CLASS getClazz() { - return clazz; + public TYPE getType() { + return type; } - public void setClazz(CLASS clazz) { - this.clazz = clazz; + public CLASS getClazz() { + return clazz; } public String getName() { return name; } - public void setName(String name) { - this.name = name; + public static Question parse(DataInputStream dis, byte[] data) throws IOException { + String name = NameUtil.parse(dis, data); + TYPE type = TYPE.getType(dis.readUnsignedShort()); + CLASS clazz = CLASS.getClass(dis.readUnsignedShort()); + return new Question (name, type, clazz); } - public void parse(DataInputStream dis, byte[] data) throws IOException { - this.name = NameUtil.parse(dis, data); - this.type = TYPE.getType(dis.readUnsignedShort()); - this.clazz = CLASS.getClass(dis.readUnsignedShort()); + public byte[] toByteArray() { + if (byteArray == null) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(512); + DataOutputStream dos = new DataOutputStream(baos); + + try { + dos.write(NameUtil.toByteArray(this.name)); + dos.writeShort(type.getValue()); + dos.writeShort(clazz.getValue()); + dos.flush(); + } catch (IOException e) { + // Should never happen + throw new IllegalStateException(e); + } + byteArray = baos.toByteArray(); + } + return byteArray; } - public byte[] toByteArray() throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(512); - DataOutputStream dos = new DataOutputStream(baos); - - dos.write(NameUtil.toByteArray(this.name)); - dos.writeShort(type.getValue()); - dos.writeShort(clazz.getValue()); - - dos.flush(); - return baos.toByteArray(); + @Override + public int hashCode() { + return toByteArray().hashCode(); } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (!(other instanceof Question)) { + return false; + } + return this.hashCode() == other.hashCode(); + } } |