Home | History | Annotate | Download | only in DNS
      1 // Copyright (c) 1999-2004 Brian Wellington (bwelling (at) xbill.org)
      2 
      3 package org.xbill.DNS;
      4 
      5 import java.io.*;
      6 import java.util.*;
      7 
      8 /**
      9  * Implements common functionality for the many record types whose format
     10  * is a list of strings.
     11  *
     12  * @author Brian Wellington
     13  */
     14 
     15 abstract class TXTBase extends Record {
     16 
     17 private static final long serialVersionUID = -4319510507246305931L;
     18 
     19 protected List strings;
     20 
     21 protected
     22 TXTBase() {}
     23 
     24 protected
     25 TXTBase(Name name, int type, int dclass, long ttl) {
     26 	super(name, type, dclass, ttl);
     27 }
     28 
     29 protected
     30 TXTBase(Name name, int type, int dclass, long ttl, List strings) {
     31 	super(name, type, dclass, ttl);
     32 	if (strings == null)
     33 		throw new IllegalArgumentException("strings must not be null");
     34 	this.strings = new ArrayList(strings.size());
     35 	Iterator it = strings.iterator();
     36 	try {
     37 		while (it.hasNext()) {
     38 			String s = (String) it.next();
     39 			this.strings.add(byteArrayFromString(s));
     40 		}
     41 	}
     42 	catch (TextParseException e) {
     43 		throw new IllegalArgumentException(e.getMessage());
     44 	}
     45 }
     46 
     47 protected
     48 TXTBase(Name name, int type, int dclass, long ttl, String string) {
     49 	this(name, type, dclass, ttl, Collections.singletonList(string));
     50 }
     51 
     52 void
     53 rrFromWire(DNSInput in) throws IOException {
     54 	strings = new ArrayList(2);
     55 	while (in.remaining() > 0) {
     56 		byte [] b = in.readCountedString();
     57 		strings.add(b);
     58 	}
     59 }
     60 
     61 void
     62 rdataFromString(Tokenizer st, Name origin) throws IOException {
     63 	strings = new ArrayList(2);
     64 	while (true) {
     65 		Tokenizer.Token t = st.get();
     66 		if (!t.isString())
     67 			break;
     68 		try {
     69 			strings.add(byteArrayFromString(t.value));
     70 		}
     71 		catch (TextParseException e) {
     72 			throw st.exception(e.getMessage());
     73 		}
     74 
     75 	}
     76 	st.unget();
     77 }
     78 
     79 /** converts to a String */
     80 String
     81 rrToString() {
     82 	StringBuffer sb = new StringBuffer();
     83 	Iterator it = strings.iterator();
     84 	while (it.hasNext()) {
     85 		byte [] array = (byte []) it.next();
     86 		sb.append(byteArrayToString(array, true));
     87 		if (it.hasNext())
     88 			sb.append(" ");
     89 	}
     90 	return sb.toString();
     91 }
     92 
     93 /**
     94  * Returns the text strings
     95  * @return A list of Strings corresponding to the text strings.
     96  */
     97 public List
     98 getStrings() {
     99 	List list = new ArrayList(strings.size());
    100 	for (int i = 0; i < strings.size(); i++)
    101 		list.add(byteArrayToString((byte []) strings.get(i), false));
    102 	return list;
    103 }
    104 
    105 /**
    106  * Returns the text strings
    107  * @return A list of byte arrays corresponding to the text strings.
    108  */
    109 public List
    110 getStringsAsByteArrays() {
    111 	return strings;
    112 }
    113 
    114 void
    115 rrToWire(DNSOutput out, Compression c, boolean canonical) {
    116 	Iterator it = strings.iterator();
    117 	while (it.hasNext()) {
    118 		byte [] b = (byte []) it.next();
    119 		out.writeCountedString(b);
    120 	}
    121 }
    122 
    123 }
    124