From: Brian W. <bwe...@xb...> - 2012-03-14 19:13:13
|
On Mar 14, 2012, at 9:34 AM, Abby Kehat wrote: > I am using dnsjava-2.1.3. When converting a record to a String, the Record.byteArrayToString method escapes some characters (‘\’ and ‘”’) with a ‘\’. Unfortunately, this means that the resulting string doesn’t accurately represent the contents of the message and can cause incorrect results when the string is used as is in other processing. > > For example, suppose I am handling NAPTR records. > > NAPTRRecord naptrRecord = … ; > String regExp = naptrRecord.getRegexp(); > > Suppose the NAPTR record has a regular expression of “!^(.+)$!0\1!”. This quite standard regular expression means that the replacement part (between the second two ‘!’) is to be applied to the string matched by the first part (between the first two ‘!’). In this simple example, we expect the string captured by the first part of the expression,^(.+)$ - which, in our case, captures the entire string - to be prefixed with the digit 0. > > However, the NAPTR getRegexp() method calls the byteArrayToString method, thereby escaping the ‘\’, giving the String “!^(.+)$!0\\1!”. If used in regular expression processing the additional \ will cause the result to be “0\1”, which is incorrect. > > I could manually replace the escaped characters myself, but since the toString method of the dnsjava classes uses the byteArrayToString method, I wouldn’t be able to do this in all uses of the byteArrayToString method. Additionally, there would be some inconsistency in what is displayed in a log file, for example, and what is actually in the wire format of the object. > > One was of solving this is by providing a byteArray method, which simply returns the wire format of a field. The String(byte []) constructor can be used to convert this to a String. > Another option is to simply use the String(byte[]) constructor in the byteArrayToString method. Obviously, this would might problems in displaying unprintable characters, but at theast the String would be an accurate representation of what’s in the message. > > Any assistance would be appreciated. I don't know what the right answer is. One solution could involve adding a parallel method to getRegexp(), which returns the data as a byte array; this is somewhat similar to TXTRecord's getStringsAsByteArrays(). But I don't think regexp's work with byte arrays, and converting the byte arrays into strings using String(byte[]) is going to have problems with unprintable characters and/or decode multibyte characters. In any case, it would be really hard for dnsjava to do "the right thing" here, because the right thing isn't too clear, and probably won't be the same for everyone. If it turns out that getRegexpAsByteArray() (or a better name) is useful, it could be added. Brian |