private byte [ ] doEscapeZeroAndNewline ( byte [ ] decodedArray , int decodedArrayOffset , int decodedArrayPosition , int decodedArrayLimit , byte [ ] encodedArray , int [ ] encodedArrayInsertionCount , ByteBuffer prefix ) { int prefixLength = prefix . remaining ( ) ; for ( ; decodedArrayPosition < decodedArrayLimit ; decodedArrayPosition ++ ) { byte decodedValue = decodedArray [ decodedArrayPosition ] ; switch ( decodedValue ) { case 0x00 : // null byte, requires multi-byte escaped representation encodedArray = supplyEncodedArray ( decodedArray , decodedArrayOffset , decodedArrayPosition , decodedArrayLimit , encodedArray , encodedArrayInsertionCount , prefix ) ; // 0x00 -> 0x7f 0x30 ('0') encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x7f ; encodedArrayInsertionCount [ 0 ] ++ ; encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x30 ; // advance to next byte continue ; case 0x0a : // newline byte, requires multi-byte escaped representation encodedArray = supplyEncodedArray ( decodedArray , decodedArrayOffset , decodedArrayPosition , decodedArrayLimit , encodedArray , encodedArrayInsertionCount , prefix ) ; // 0x0a -> 0x7f 0x6e ('n') encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x7f ; encodedArrayInsertionCount [ 0 ] ++ ; encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x6e ; // advance to next byte continue ; case 0x0d : // carriage return byte, requires multi-byte escaped // representation encodedArray = supplyEncodedArray ( decodedArray , decodedArrayOffset , decodedArrayPosition , decodedArrayLimit , encodedArray , encodedArrayInsertionCount , prefix ) ; // 0x0d -> 0x7f 0x72 ('r') encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x7f ; encodedArrayInsertionCount [ 0 ] ++ ; encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x72 ; // advance to next byte continue ; case 0x7f : // escape prefix byte, requires multi-byte escaped // representation encodedArray = supplyEncodedArray ( decodedArray , decodedArrayOffset , decodedArrayPosition , decodedArrayLimit , encodedArray , encodedArrayInsertionCount , prefix ) ; // 0x7f -> 0x7f 0x7f encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x7f ; encodedArrayInsertionCount [ 0 ] ++ ; encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = 0x7f ; // advance to next byte continue ; default : if ( encodedArray != null ) { // not escaping character, copy this byte to encodedValue encodedArray [ decodedArrayPosition + encodedArrayInsertionCount [ 0 ] + prefixLength ] = decodedValue ; } continue ; } } return encodedArray ; }
insert escape characters into encodedArray return number of inserted bytes