2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: TXTRecord.java,v $
26 Revision 1.5 2004/08/25 21:54:36 rpantos
27 <rdar://problem/3773973> Fix getValue() for values containing '='.
29 Revision 1.4 2004/08/04 01:04:50 rpantos
30 <rdar://problems/3731579&3731582> Fix set(); add remove() & toString().
32 Revision 1.3 2004/07/13 21:24:25 rpantos
33 Fix for <rdar://problem/3701120>.
35 Revision 1.2 2004/04/30 21:48:27 rpantos
36 Change line endings for CVS.
38 Revision 1.1 2004/04/30 16:29:35 rpantos
43 - fix set() to replace existing values
47 package com
.apple
.dnssd
;
51 Object used to construct and parse DNS-SD format TXT records.
52 For more info see <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-Based Service Discovery</a>, section 6.
55 public class TXTRecord
58 DNS-SD specifies that a TXT record corresponding to an SRV record consist of
59 a packed array of bytes, each preceded by a length byte. Each string
60 is an attribute-value pair.
62 The TXTRecord object stores the entire TXT data as a single byte array, traversing it
63 as need be to implement its various methods.
66 static final protected byte kAttrSep
= '=';
68 protected byte[] fBytes
;
70 /** Constructs a new, empty TXT record. */
72 { fBytes
= new byte[0]; }
74 /** Constructs a new TXT record from a byte array in the standard format. */
75 public TXTRecord( byte[] initBytes
)
76 { fBytes
= (byte[]) initBytes
.clone(); }
78 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
80 The key name. Must be ASCII, with no '=' characters.
83 Value to be encoded into bytes using the default platform character set.
85 public void set( String key
, String value
)
87 byte[] valBytes
= (value
!= null) ? value
.getBytes() : null;
88 this.set( key
, valBytes
);
91 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
93 The key name. Must be ASCII, with no '=' characters.
96 Binary representation of the value.
98 public void set( String key
, byte[] value
)
101 int valLen
= (value
!= null) ? value
.length
: 0;
104 keyBytes
= key
.getBytes( "US-ASCII");
106 catch ( java
.io
.UnsupportedEncodingException uee
) {
107 throw new IllegalArgumentException();
110 for ( int i
=0; i
< keyBytes
.length
; i
++)
111 if ( keyBytes
[i
] == '=')
112 throw new IllegalArgumentException();
114 if ( keyBytes
.length
+ valLen
>= 255)
115 throw new ArrayIndexOutOfBoundsException();
117 int prevLoc
= this.remove( key
);
119 prevLoc
= this.size();
121 this.insert( keyBytes
, value
, prevLoc
);
124 protected void insert( byte[] keyBytes
, byte[] value
, int index
)
125 // Insert a key-value pair at index
127 byte[] oldBytes
= fBytes
;
128 int valLen
= (value
!= null) ? value
.length
: 0;
132 // locate the insertion point
133 for ( int i
=0; i
< index
&& insertion
< fBytes
.length
; i
++)
134 insertion
+= fBytes
[ insertion
] + 1;
136 avLen
= (byte) ( keyBytes
.length
+ valLen
+ (value
!= null ?
1 : 0));
137 newLen
= (byte) ( avLen
+ oldBytes
.length
+ 1);
139 fBytes
= new byte[ newLen
];
140 System
.arraycopy( oldBytes
, 0, fBytes
, 0, insertion
);
141 int secondHalfLen
= oldBytes
.length
- insertion
;
142 System
.arraycopy( oldBytes
, insertion
, fBytes
, newLen
- secondHalfLen
, secondHalfLen
);
143 fBytes
[ insertion
] = avLen
;
144 System
.arraycopy( keyBytes
, 0, fBytes
, insertion
+ 1, keyBytes
.length
);
147 fBytes
[ insertion
+ 1 + keyBytes
.length
] = kAttrSep
;
148 System
.arraycopy( value
, 0, fBytes
, insertion
+ keyBytes
.length
+ 2, valLen
);
152 /** Remove a key/value pair from the TXT record. Returns index it was at, or -1 if not found. */
153 public int remove( String key
)
157 for ( int i
=0; avStart
< fBytes
.length
; i
++)
159 int avLen
= fBytes
[ avStart
];
160 if ( key
.length() <= avLen
&&
161 ( key
.length() == avLen
|| fBytes
[ avStart
+ key
.length() + 1] == kAttrSep
))
163 String s
= new String( fBytes
, avStart
+ 1, key
.length());
164 if ( 0 == key
.compareToIgnoreCase( s
))
166 byte[] oldBytes
= fBytes
;
167 fBytes
= new byte[ oldBytes
.length
- avLen
- 1];
168 System
.arraycopy( oldBytes
, 0, fBytes
, 0, avStart
);
169 System
.arraycopy( oldBytes
, avStart
+ avLen
+ 1, fBytes
, avStart
, oldBytes
.length
- avStart
- avLen
- 1);
173 avStart
+= avLen
+ 1;
178 /** Return the number of keys in the TXT record. */
183 for ( i
=0, avStart
=0; avStart
< fBytes
.length
; i
++)
184 avStart
+= fBytes
[ avStart
] + 1;
188 /** Return true if key is present in the TXT record, false if not. */
189 public boolean contains( String key
)
193 for ( int i
=0; null != ( s
= this.getKey( i
)); i
++)
194 if ( 0 == key
.compareToIgnoreCase( s
))
199 /** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
200 public String
getKey( int index
)
204 for ( int i
=0; i
< index
&& avStart
< fBytes
.length
; i
++)
205 avStart
+= fBytes
[ avStart
] + 1;
207 if ( avStart
< fBytes
.length
)
209 int avLen
= fBytes
[ avStart
];
212 for ( aLen
=0; aLen
< avLen
; aLen
++)
213 if ( fBytes
[ avStart
+ aLen
+ 1] == kAttrSep
)
215 return new String( fBytes
, avStart
+ 1, aLen
);
221 Look up a key in the TXT record by zero-based index and return its value. <P>
222 Returns null if index exceeds the total number of keys.
223 Returns null if the key is present with no value.
225 public byte[] getValue( int index
)
230 for ( int i
=0; i
< index
&& avStart
< fBytes
.length
; i
++)
231 avStart
+= fBytes
[ avStart
] + 1;
233 if ( avStart
< fBytes
.length
)
235 int avLen
= fBytes
[ avStart
];
238 for ( aLen
=0; aLen
< avLen
; aLen
++)
240 if ( fBytes
[ avStart
+ aLen
+ 1] == kAttrSep
)
242 value
= new byte[ avLen
- aLen
- 1];
243 System
.arraycopy( fBytes
, avStart
+ aLen
+ 2, value
, 0, avLen
- aLen
- 1);
251 /** Converts the result of getValue() to a string in the platform default character set. */
252 public String
getValueAsString( int index
)
254 byte[] value
= this.getValue( index
);
255 return value
!= null ?
new String( value
) : null;
258 /** Get the value associated with a key. Will be null if the key is not defined.
259 Array will have length 0 if the key is defined with an = but no value.<P>
262 The left-hand side of the key-value pair.
264 @return The binary representation of the value.
266 public byte[] getValue( String forKey
)
271 for ( i
=0; null != ( s
= this.getKey( i
)); i
++)
272 if ( 0 == forKey
.compareToIgnoreCase( s
))
273 return this.getValue( i
);
277 /** Converts the result of getValue() to a string in the platform default character set.<P>
280 The left-hand side of the key-value pair.
282 @return The value represented in the default platform character set.
284 public String
getValueAsString( String forKey
)
286 byte[] val
= this.getValue( forKey
);
287 return val
!= null ?
new String( val
) : null;
290 /** Return the contents of the TXT record as raw bytes. */
291 public byte[] getRawBytes() { return (byte[]) fBytes
.clone(); }
293 /** Return a string representation of the object. */
294 public String
toString()
296 String a
, result
= null;
298 for ( int i
=0; null != ( a
= this.getKey( i
)); i
++)
300 String av
= String
.valueOf( i
) + "={" + a
;
301 String val
= this.getValueAsString( i
);
303 av
+= "=" + val
+ "}";
309 result
= result
+ ", " + av
;
311 return result
!= null ? result
: "";