/** * Generate a binary paket for this dns question. * @return The dns question. */ 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() | (unicastQuery ? (1 << 15) : 0)); dos.flush(); } catch (IOException e) { // Should never happen throw new IllegalStateException(e); } byteArray = baos.toByteArray(); } return byteArray; }
this.type = TYPE.getType(dis.readUnsignedShort()); int clazzValue = dis.readUnsignedShort(); this.clazz = CLASS.getClass(clazzValue & 0x7fff); this.unicastQuery = (clazzValue & 0x8000) > 0; if (this.clazz == null) {
/** * Parse a byte array and rebuild the dns question from it. * @param dis The input stream. * @param data The plain data (for dns name references). * @return The parsed dns question. * @throws IOException On errors (read outside of packet). */ 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); }