001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import java.util.*;
007:
008: /**
009: * Implements common functionality for the many record types whose format
010: * is a list of strings.
011: *
012: * @author Brian Wellington
013: */
014:
015: abstract class TXTBase extends Record {
016:
017: protected List strings;
018:
019: protected TXTBase() {
020: }
021:
022: protected TXTBase(Name name, int type, int dclass, long ttl) {
023: super (name, type, dclass, ttl);
024: }
025:
026: protected TXTBase(Name name, int type, int dclass, long ttl,
027: List strings) {
028: super (name, type, dclass, ttl);
029: if (strings == null)
030: throw new IllegalArgumentException(
031: "strings must not be null");
032: this .strings = new ArrayList(strings.size());
033: Iterator it = strings.iterator();
034: try {
035: while (it.hasNext()) {
036: String s = (String) it.next();
037: this .strings.add(byteArrayFromString(s));
038: }
039: } catch (TextParseException e) {
040: throw new IllegalArgumentException(e.getMessage());
041: }
042: }
043:
044: protected TXTBase(Name name, int type, int dclass, long ttl,
045: String string) {
046: this (name, type, dclass, ttl, Collections.singletonList(string));
047: }
048:
049: void rrFromWire(DNSInput in) throws IOException {
050: strings = new ArrayList(2);
051: while (in.remaining() > 0) {
052: byte[] b = in.readCountedString();
053: strings.add(b);
054: }
055: }
056:
057: void rdataFromString(Tokenizer st, Name origin) throws IOException {
058: strings = new ArrayList(2);
059: while (true) {
060: Tokenizer.Token t = st.get();
061: if (!t.isString())
062: break;
063: try {
064: strings.add(byteArrayFromString(t.value));
065: } catch (TextParseException e) {
066: throw st.exception(e.getMessage());
067: }
068:
069: }
070: st.unget();
071: }
072:
073: /** converts to a String */
074: String rrToString() {
075: StringBuffer sb = new StringBuffer();
076: Iterator it = strings.iterator();
077: while (it.hasNext()) {
078: byte[] array = (byte[]) it.next();
079: sb.append(byteArrayToString(array, true));
080: if (it.hasNext())
081: sb.append(" ");
082: }
083: return sb.toString();
084: }
085:
086: /**
087: * Returns the text strings
088: * @return A list of Strings corresponding to the text strings.
089: */
090: public List getStrings() {
091: List list = new ArrayList(strings.size());
092: for (int i = 0; i < strings.size(); i++)
093: list.add(byteArrayToString((byte[]) strings.get(i), false));
094: return list;
095: }
096:
097: /**
098: * Returns the text strings
099: * @return A list of byte arrays corresponding to the text strings.
100: */
101: public List getStringsAsByteArrays() {
102: return strings;
103: }
104:
105: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
106: Iterator it = strings.iterator();
107: while (it.hasNext()) {
108: byte[] b = (byte[]) it.next();
109: out.writeCountedString(b);
110: }
111: }
112:
113: }
|