/**
   * Builds a new Record from its textual representation
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @param st A tokenizer containing the textual representation of the rdata.
   * @param origin The default origin to be appended to relative domain names.
   * @return The new record
   * @throws IOException The text format was invalid.
   */
  public static Record fromString(
      Name name, int type, int dclass, long ttl, Tokenizer st, Name origin) throws IOException {
    Record rec;

    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    Tokenizer.Token t = st.get();
    if (t.type == Tokenizer.IDENTIFIER && t.value.equals("\\#")) {
      int length = st.getUInt16();
      byte[] data = st.getHex();
      if (data == null) {
        data = new byte[0];
      }
      if (length != data.length)
        throw st.exception("invalid unknown RR encoding: " + "length mismatch");
      DNSInput in = new DNSInput(data);
      return newRecord(name, type, dclass, ttl, length, in);
    }
    st.unget();
    rec = getEmptyRecord(name, type, dclass, ttl, true);
    rec.rdataFromString(st, origin);
    t = st.get();
    if (t.type != Tokenizer.EOL && t.type != Tokenizer.EOF) {
      throw st.exception("unexpected tokens at end of record");
    }
    return rec;
  }
 /**
  * Determines if two Records are identical. This compares the name, type, class, and rdata (with
  * names canonicalized). The TTLs are not compared.
  *
  * @param arg The record to compare to
  * @return true if the records are equal, false otherwise.
  */
 public boolean equals(Object arg) {
   if (arg == null || !(arg instanceof Record)) return false;
   Record r = (Record) arg;
   if (type != r.type || dclass != r.dclass || !name.equals(r.name)) return false;
   byte[] array1 = rdataToWireCanonical();
   byte[] array2 = r.rdataToWireCanonical();
   return Arrays.equals(array1, array2);
 }
  /**
   * Creates a new empty record, with the given parameters.
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @return An object of a subclass of Record
   */
  public static Record newRecord(Name name, int type, int dclass, long ttl) {
    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    return getEmptyRecord(name, type, dclass, ttl, false);
  }
 Record(Name name, int type, int dclass, long ttl) {
   if (!name.isAbsolute()) throw new RelativeNameException(name);
   Type.check(type);
   DClass.check(dclass);
   TTL.check(ttl);
   this.name = name;
   this.type = type;
   this.dclass = dclass;
   this.ttl = ttl;
 }
 void toWire(DNSOutput out, int section, Compression c) {
   name.toWire(out, c);
   out.writeU16(type);
   out.writeU16(dclass);
   if (section == Section.QUESTION) return;
   out.writeU32(ttl);
   int lengthPosition = out.current();
   out.writeU16(0); /* until we know better */
   rrToWire(out, c, false);
   int rrlength = out.current() - lengthPosition - 2;
   out.save();
   out.jump(lengthPosition);
   out.writeU16(rrlength);
   out.restore();
 }
  /**
   * Creates a new record, with the given parameters.
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @param length The length of the record's data.
   * @param data The rdata of the record, in uncompressed DNS wire format. Only the first length
   *     bytes are used.
   */
  public static Record newRecord(
      Name name, int type, int dclass, long ttl, int length, byte[] data) {
    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    DNSInput in;
    if (data != null) in = new DNSInput(data);
    else in = null;
    try {
      return newRecord(name, type, dclass, ttl, length, in);
    } catch (IOException e) {
      return null;
    }
  }
 private void toWireCanonical(DNSOutput out, boolean noTTL) {
   name.toWireCanonical(out);
   out.writeU16(type);
   out.writeU16(dclass);
   if (noTTL) {
     out.writeU32(0);
   } else {
     out.writeU32(ttl);
   }
   int lengthPosition = out.current();
   out.writeU16(0); /* until we know better */
   rrToWire(out, null, true);
   int rrlength = out.current() - lengthPosition - 2;
   out.save();
   out.jump(lengthPosition);
   out.writeU16(rrlength);
   out.restore();
 }
  /**
   * Compares this Record to another Object.
   *
   * @param o The Object to be compared.
   * @return The value 0 if the argument is a record equivalent to this record; a value less than 0
   *     if the argument is less than this record in the canonical ordering, and a value greater
   *     than 0 if the argument is greater than this record in the canonical ordering. The canonical
   *     ordering is defined to compare by name, class, type, and rdata.
   * @throws ClassCastException if the argument is not a Record.
   */
  public int compareTo(Object o) {
    Record arg = (Record) o;

    if (this == arg) return (0);

    int n = name.compareTo(arg.name);
    if (n != 0) return (n);
    n = dclass - arg.dclass;
    if (n != 0) return (n);
    n = type - arg.type;
    if (n != 0) return (n);
    byte[] rdata1 = rdataToWireCanonical();
    byte[] rdata2 = arg.rdataToWireCanonical();
    for (int i = 0; i < rdata1.length && i < rdata2.length; i++) {
      n = (rdata1[i] & 0xFF) - (rdata2[i] & 0xFF);
      if (n != 0) return (n);
    }
    return (rdata1.length - rdata2.length);
  }
 /* Checks that a name is absolute */
 static Name checkName(String field, Name name) {
   if (!name.isAbsolute()) throw new RelativeNameException(name);
   return name;
 }
 /**
  * Creates a new record identical to the current record, but with a different name. This is most
  * useful for replacing the name of a wildcard record.
  */
 public Record withName(Name name) {
   if (!name.isAbsolute()) throw new RelativeNameException(name);
   Record rec = cloneRecord();
   rec.name = name;
   return rec;
 }
 /**
  * Determines if two Records could be part of the same RRset. This compares the name, type, and
  * class of the Records; the ttl and rdata are not compared.
  */
 public boolean sameRRset(Record rec) {
   return (getRRsetType() == rec.getRRsetType() && dclass == rec.dclass && name.equals(rec.name));
 }