TXTRecord.java revision 2
/* -*- Mode: Java; tab-width: 4 -*-
*
* Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
Change History (most recent first):
$Log: TXTRecord.java,v $
Revision 1.6 2006/08/14 23:25:08 cheshire
Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
Revision 1.5 2004/08/25 21:54:36 rpantos
<rdar://problem/3773973> Fix getValue() for values containing '='.
Revision 1.4 2004/08/04 01:04:50 rpantos
<rdar://problems/3731579&3731582> Fix set(); add remove() & toString().
Revision 1.3 2004/07/13 21:24:25 rpantos
Fix for <rdar://problem/3701120>.
Revision 1.2 2004/04/30 21:48:27 rpantos
Change line endings for CVS.
Revision 1.1 2004/04/30 16:29:35 rpantos
First checked in.
ident "%Z%%M% %I% %E% SMI"
To do:
- implement remove()
- fix set() to replace existing values
*/
/**
Object used to construct and parse DNS-SD format TXT records.
For more info see <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-Based Service Discovery</a>, section 6.
*/
public class TXTRecord
{
/*
DNS-SD specifies that a TXT record corresponding to an SRV record consist of
a packed array of bytes, each preceded by a length byte. Each string
is an attribute-value pair.
The TXTRecord object stores the entire TXT data as a single byte array, traversing it
as need be to implement its various methods.
*/
static final protected byte kAttrSep = '=';
protected byte[] fBytes;
/** Constructs a new, empty TXT record. */
public TXTRecord()
{ fBytes = new byte[0]; }
/** Constructs a new TXT record from a byte array in the standard format. */
@param key
The key name. Must be ASCII, with no '=' characters.
<P>
@param value
Value to be encoded into bytes using the default platform character set.
*/
{
}
@param key
The key name. Must be ASCII, with no '=' characters.
<P>
@param value
Binary representation of the value.
*/
{
byte[] keyBytes;
try {
}
throw new IllegalArgumentException();
}
if ( keyBytes[i] == '=')
throw new IllegalArgumentException();
throw new ArrayIndexOutOfBoundsException();
if ( prevLoc == -1)
}
// Insert a key-value pair at index
{
int insertion = 0;
// locate the insertion point
{
}
}
{
int avStart = 0;
{
{
{
System.arraycopy( oldBytes, avStart + avLen + 1, fBytes, avStart, oldBytes.length - avStart - avLen - 1);
return i;
}
}
}
return -1;
}
/** Return the number of keys in the TXT record. */
public int size()
{
int i, avStart;
return i;
}
/** Return true if key is present in the TXT record, false if not. */
{
return true;
return false;
}
/** Return a key in the TXT record by zero-based index. Returns null if index exceeds the total number of keys. */
{
int avStart = 0;
{
int aLen = 0;
break;
}
return null;
}
/**
Look up a key in the TXT record by zero-based index and return its value. <P>
Returns null if index exceeds the total number of keys.
Returns null if the key is present with no value.
*/
{
int avStart = 0;
{
int aLen = 0;
{
{
break;
}
}
}
return value;
}
/** Converts the result of getValue() to a string in the platform default character set. */
{
}
/** Get the value associated with a key. Will be null if the key is not defined.
Array will have length 0 if the key is defined with an = but no value.<P>
@param forKey
The left-hand side of the key-value pair.
<P>
@return The binary representation of the value.
*/
{
int i;
return this.getValue( i);
return null;
}
/** Converts the result of getValue() to a string in the platform default character set.<P>
@param forKey
The left-hand side of the key-value pair.
<P>
@return The value represented in the default platform character set.
*/
{
}
/** Return the contents of the TXT record as raw bytes. */
/** Return a string representation of the object. */
{
{
else
av += "}";
else
}
}
}