1// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org) 2 3package org.xbill.DNS; 4 5import java.io.*; 6import 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 15abstract class TXTBase extends Record { 16 17private static final long serialVersionUID = -4319510507246305931L; 18 19protected List strings; 20 21protected 22TXTBase() {} 23 24protected 25TXTBase(Name name, int type, int dclass, long ttl) { 26 super(name, type, dclass, ttl); 27} 28 29protected 30TXTBase(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 47protected 48TXTBase(Name name, int type, int dclass, long ttl, String string) { 49 this(name, type, dclass, ttl, Collections.singletonList(string)); 50} 51 52void 53rrFromWire(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 61void 62rdataFromString(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 */ 80String 81rrToString() { 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 */ 97public List 98getStrings() { 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 */ 109public List 110getStringsAsByteArrays() { 111 return strings; 112} 113 114void 115rrToWire(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