MCPcopy Create free account
hub / github.com/DeNA/PacketProxy / byteArrayFromString

Method byteArrayFromString

src/main/java/core/org/xbill/DNS/Record.java:337–395  ·  view source on GitHub ↗

Converts a String into a byte array.

(String s)

Source from the content-addressed store, hash-verified

335 * Converts a String into a byte array.
336 */
337protected static byte []
338byteArrayFromString(String s) throws TextParseException {
339 byte [] array = s.getBytes();
340 boolean escaped = false;
341 boolean hasEscapes = false;
342
343 for (int i = 0; i < array.length; i++) {
344 if (array[i] == '\\') {
345 hasEscapes = true;
346 break;
347 }
348 }
349 if (!hasEscapes) {
350 if (array.length > 255) {
351 throw new TextParseException("text string too long");
352 }
353 return array;
354 }
355
356 ByteArrayOutputStream os = new ByteArrayOutputStream();
357
358 int digits = 0;
359 int intval = 0;
360 for (int i = 0; i < array.length; i++) {
361 byte b = array[i];
362 if (escaped) {
363 if (b >= '0' && b <= '9' && digits < 3) {
364 digits++;
365 intval *= 10;
366 intval += (b - '0');
367 if (intval > 255)
368 throw new TextParseException
369 ("bad escape");
370 if (digits < 3)
371 continue;
372 b = (byte) intval;
373 }
374 else if (digits > 0 && digits < 3)
375 throw new TextParseException("bad escape");
376 os.write(b);
377 escaped = false;
378 }
379 else if (array[i] == '\\') {
380 escaped = true;
381 digits = 0;
382 intval = 0;
383 }
384 else
385 os.write(array[i]);
386 }
387 if (digits > 0 && digits < 3)
388 throw new TextParseException("bad escape");
389 array = os.toByteArray();
390 if (array.length > 255) {
391 throw new TextParseException("text string too long");
392 }
393
394 return os.toByteArray();

Callers 15

ISDNRecordMethod · 0.80
rdataFromStringMethod · 0.80
URIRecordMethod · 0.80
rdataFromStringMethod · 0.80
HINFORecordMethod · 0.80
rdataFromStringMethod · 0.80
TXTBaseMethod · 0.80
rdataFromStringMethod · 0.80
GPOSRecordMethod · 0.80
rdataFromStringMethod · 0.80
NAPTRRecordMethod · 0.80
rdataFromStringMethod · 0.80

Calls 3

getBytesMethod · 0.65
writeMethod · 0.65
toByteArrayMethod · 0.45

Tested by

no test coverage detected