1 /* -*- Mode: Java; tab-width: 4 -*-
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 Change History (most recent first):
19 $Log: TXTRecord.java,v $
20 Revision 1.8 2007/03/16 23:39:40 vazquez
21 <rdar://problem/4612778> Java: Coding error in java wrappers, limited TXTRecord length
23 Revision 1.7 2006/12/13 07:13:23 mkrochma
24 <rdar://problem/4612778> Java: Coding error in java wrappers, limited TXTRecord length
26 Revision 1.6 2006/08/14 23:25:08 cheshire
27 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
29 Revision 1.5 2004/08/25 21:54:36 rpantos
30 <rdar://problem/3773973> Fix getValue() for values containing '='.
32 Revision 1.4 2004/08/04 01:04:50 rpantos
33 <rdar://problems/3731579&3731582> Fix set(); add remove() & toString().
35 Revision 1.3 2004/07/13 21:24:25 rpantos
36 Fix for <rdar://problem/3701120>.
38 Revision 1.2 2004/04/30 21:48:27 rpantos
39 Change line endings for CVS.
41 Revision 1.1 2004/04/30 16:29:35 rpantos
46 - fix set() to replace existing values
50 package com
.apple
.dnssd
;
54 Object used to construct and parse DNS-SD format TXT records.
55 For more info see <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-Based Service Discovery</a>, section 6.
58 public class TXTRecord
61 DNS-SD specifies that a TXT record corresponding to an SRV record consist of
62 a packed array of bytes, each preceded by a length byte. Each string
63 is an attribute-value pair.
65 The TXTRecord object stores the entire TXT data as a single byte array, traversing it
66 as need be to implement its various methods.
69 static final protected byte kAttrSep
= '=';
71 protected byte[] fBytes
;
73 /** Constructs a new, empty TXT record. */
75 { fBytes
= new byte[0]; }
77 /** Constructs a new TXT record from a byte array in the standard format. */
78 public TXTRecord( byte[] initBytes
)
79 { fBytes
= (byte[]) initBytes
.clone(); }
81 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
83 The key name. Must be ASCII, with no '=' characters.
86 Value to be encoded into bytes using the default platform character set.
88 public void set( String key
, String value
)
90 byte[] valBytes
= (value
!= null) ? value
.getBytes() : null;
91 this.set( key
, valBytes
);
94 /** Set a key/value pair in the TXT record. Setting an existing key will replace its value.<P>
96 The key name. Must be ASCII, with no '=' characters.
99 Binary representation of the value.
101 public void set( String key
, byte[] value
)
104 int valLen
= (value
!= null) ? value
.length
: 0;
107 keyBytes
= key
.getBytes( "US-ASCII");
109 catch ( java
.io
.UnsupportedEncodingException uee
) {
110 throw new IllegalArgumentException();
113 for ( int i
=0; i
< keyBytes
.length
; i
++)
114 if ( keyBytes
[i
] == '=')
115 throw new IllegalArgumentException();
117 if ( keyBytes
.length
+ valLen
>= 255)
118 throw new ArrayIndexOutOfBoundsException();
120 int prevLoc
= this.remove( key
);
122 prevLoc
= this.size();
124 this.insert( keyBytes
, value
, prevLoc
);
127 protected void insert( byte[] keyBytes
, byte[] value
, int index
)
128 // Insert a key-value pair at index
130 byte[] oldBytes
= fBytes
;
131 int valLen
= (value
!= null) ? value
.length
: 0;
135 // locate the insertion point
136 for ( int i
=0; i
< index
&& insertion
< fBytes
.length
; i
++)
137 insertion
+= (0xFF & (fBytes
[ insertion
] + 1));
139 avLen
= keyBytes
.length
+ valLen
+ (value
!= null ?
1 : 0);
140 newLen
= avLen
+ oldBytes
.length
+ 1;
142 fBytes
= new byte[ newLen
];
143 System
.arraycopy( oldBytes
, 0, fBytes
, 0, insertion
);
144 int secondHalfLen
= oldBytes
.length
- insertion
;
145 System
.arraycopy( oldBytes
, insertion
, fBytes
, newLen
- secondHalfLen
, secondHalfLen
);
146 fBytes
[ insertion
] = ( byte) avLen
;
147 System
.arraycopy( keyBytes
, 0, fBytes
, insertion
+ 1, keyBytes
.length
);
150 fBytes
[ insertion
+ 1 + keyBytes
.length
] = kAttrSep
;
151 System
.arraycopy( value
, 0, fBytes
, insertion
+ keyBytes
.length
+ 2, valLen
);
155 /** Remove a key/value pair from the TXT record. Returns index it was at, or -1 if not found. */
156 public int remove( String key
)
160 for ( int i
=0; avStart
< fBytes
.length
; i
++)
162 int avLen
= fBytes
[ avStart
];
163 if ( key
.length() <= avLen
&&
164 ( key
.length() == avLen
|| fBytes
[ avStart
+ key
.length() + 1] == kAttrSep
))
166 String s
= new String( fBytes
, avStart
+ 1, key
.length());
167 if ( 0 == key
.compareToIgnoreCase( s
))
169 byte[] oldBytes
= fBytes
;
170 fBytes
= new byte[ oldBytes
.length
- avLen
- 1];
171 System
.arraycopy( oldBytes
, 0, fBytes
, 0, avStart
);
172 System
.arraycopy( oldBytes
, avStart
+ avLen
+ 1, fBytes
, avStart
, oldBytes
.length
- avStart
- avLen
- 1);
176 avStart
+= (0xFF & (avLen
+ 1));
181 /** Return the number of keys in the TXT record. */
186 for ( i
=0, avStart
=0; avStart
< fBytes
.length
; i
++)
187 avStart
+= (0xFF & (fBytes
[ avStart
] + 1));
191 /** Return true if key is present in the TXT record, false if not. */
192 public boolean contains( String key
)
196 for ( int i
=0; null != ( s
= this.getKey( i
)); i
++)
197 if ( 0 == key
.compareToIgnoreCase( s
))
202 /** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
203 public String
getKey( int index
)
207 for ( int i
=0; i
< index
&& avStart
< fBytes
.length
; i
++)
208 avStart
+= fBytes
[ avStart
] + 1;
210 if ( avStart
< fBytes
.length
)
212 int avLen
= fBytes
[ avStart
];
215 for ( aLen
=0; aLen
< avLen
; aLen
++)
216 if ( fBytes
[ avStart
+ aLen
+ 1] == kAttrSep
)
218 return new String( fBytes
, avStart
+ 1, aLen
);
224 Look up a key in the TXT record by zero-based index and return its value. <P>
225 Returns null if index exceeds the total number of keys.
226 Returns null if the key is present with no value.
228 public byte[] getValue( int index
)
233 for ( int i
=0; i
< index
&& avStart
< fBytes
.length
; i
++)
234 avStart
+= fBytes
[ avStart
] + 1;
236 if ( avStart
< fBytes
.length
)
238 int avLen
= fBytes
[ avStart
];
241 for ( aLen
=0; aLen
< avLen
; aLen
++)
243 if ( fBytes
[ avStart
+ aLen
+ 1] == kAttrSep
)
245 value
= new byte[ avLen
- aLen
- 1];
246 System
.arraycopy( fBytes
, avStart
+ aLen
+ 2, value
, 0, avLen
- aLen
- 1);
254 /** Converts the result of getValue() to a string in the platform default character set. */
255 public String
getValueAsString( int index
)
257 byte[] value
= this.getValue( index
);
258 return value
!= null ?
new String( value
) : null;
261 /** Get the value associated with a key. Will be null if the key is not defined.
262 Array will have length 0 if the key is defined with an = but no value.<P>
265 The left-hand side of the key-value pair.
267 @return The binary representation of the value.
269 public byte[] getValue( String forKey
)
274 for ( i
=0; null != ( s
= this.getKey( i
)); i
++)
275 if ( 0 == forKey
.compareToIgnoreCase( s
))
276 return this.getValue( i
);
280 /** Converts the result of getValue() to a string in the platform default character set.<P>
283 The left-hand side of the key-value pair.
285 @return The value represented in the default platform character set.
287 public String
getValueAsString( String forKey
)
289 byte[] val
= this.getValue( forKey
);
290 return val
!= null ?
new String( val
) : null;
293 /** Return the contents of the TXT record as raw bytes. */
294 public byte[] getRawBytes() { return (byte[]) fBytes
.clone(); }
296 /** Return a string representation of the object. */
297 public String
toString()
299 String a
, result
= null;
301 for ( int i
=0; null != ( a
= this.getKey( i
)); i
++)
303 String av
= String
.valueOf( i
) + "={" + a
;
304 String val
= this.getValueAsString( i
);
306 av
+= "=" + val
+ "}";
312 result
= result
+ ", " + av
;
314 return result
!= null ? result
: "";