protected static byte [ ] byteArrayFromString ( String s ) throws TextParseException { byte [ ] array = s . getBytes ( ) ; boolean escaped = false ; boolean hasEscapes = false ; for ( int i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] == ' ' ) { hasEscapes = true ; break ; } } if ( ! hasEscapes ) { if ( array . length > 255 ) { throw new TextParseException ( "text string too long" ) ; } return array ; } ByteArrayOutputStream os = new ByteArrayOutputStream ( ) ; int digits = 0 ; int intval = 0 ; for ( int i = 0 ; i < array . length ; i ++ ) { byte b = array [ i ] ; if ( escaped ) { if ( b >= ' ' && b <= ' ' && digits < 3 ) { digits ++ ; intval *= 10 ; intval += ( b - ' ' ) ; if ( intval > 255 ) throw new TextParseException ( "bad escape" ) ; if ( digits < 3 ) continue ; b = ( byte ) intval ; } else if ( digits > 0 && digits < 3 ) throw new TextParseException ( "bad escape" ) ; os . write ( b ) ; escaped = false ; } else if ( array [ i ] == ' ' ) { escaped = true ; digits = 0 ; intval = 0 ; } else os . write ( array [ i ] ) ; } if ( digits > 0 && digits < 3 ) throw new TextParseException ( "bad escape" ) ; array = os . toByteArray ( ) ; if ( array . length > 255 ) { throw new TextParseException ( "text string too long" ) ; } return os . toByteArray ( ) ; }
Converts a String into a byte array .