1// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org) 2 3package org.xbill.DNS; 4 5import java.io.*; 6import org.xbill.DNS.utils.*; 7 8/** 9 * NSAP Address Record. 10 * 11 * @author Brian Wellington 12 */ 13 14public class NSAPRecord extends Record { 15 16private static final long serialVersionUID = -1037209403185658593L; 17 18private byte [] address; 19 20NSAPRecord() {} 21 22Record 23getObject() { 24 return new NSAPRecord(); 25} 26 27private static final byte [] 28checkAndConvertAddress(String address) { 29 if (!address.substring(0, 2).equalsIgnoreCase("0x")) { 30 return null; 31 } 32 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 33 boolean partial = false; 34 int current = 0; 35 for (int i = 2; i < address.length(); i++) { 36 char c = address.charAt(i); 37 if (c == '.') { 38 continue; 39 } 40 int value = Character.digit(c, 16); 41 if (value == -1) { 42 return null; 43 } 44 if (partial) { 45 current += value; 46 bytes.write(current); 47 partial = false; 48 } else { 49 current = value << 4; 50 partial = true; 51 } 52 53 } 54 if (partial) { 55 return null; 56 } 57 return bytes.toByteArray(); 58} 59 60/** 61 * Creates an NSAP Record from the given data 62 * @param address The NSAP address. 63 * @throws IllegalArgumentException The address is not a valid NSAP address. 64 */ 65public 66NSAPRecord(Name name, int dclass, long ttl, String address) { 67 super(name, Type.NSAP, dclass, ttl); 68 this.address = checkAndConvertAddress(address); 69 if (this.address == null) { 70 throw new IllegalArgumentException("invalid NSAP address " + 71 address); 72 } 73} 74 75void 76rrFromWire(DNSInput in) throws IOException { 77 address = in.readByteArray(); 78} 79 80void 81rdataFromString(Tokenizer st, Name origin) throws IOException { 82 String addr = st.getString(); 83 this.address = checkAndConvertAddress(addr); 84 if (this.address == null) 85 throw st.exception("invalid NSAP address " + addr); 86} 87 88/** 89 * Returns the NSAP address. 90 */ 91public String 92getAddress() { 93 return byteArrayToString(address, false); 94} 95 96void 97rrToWire(DNSOutput out, Compression c, boolean canonical) { 98 out.writeByteArray(address); 99} 100 101String 102rrToString() { 103 return "0x" + base16.toString(address); 104} 105 106} 107