public static void writeEscaped(OutputStream out, byte[] bytes, int start,
      int len, boolean escaped, byte escapeChar, boolean[] needsEscape)
      throws IOException {
    if (escaped) {
      int end = start + len;
      for (int i = start; i <= end; i++) {
        if (i == end || needsEscape[bytes[i] & 0xFF]) {  // Converts negative byte to positive index
          if (i > start) {
            out.write(bytes, start, i - start);
          }

          if (i == end) break;

          out.write(escapeChar);
          if (bytes[i] == '\r') {
            out.write('r');
            start = i + 1;
          } else if (bytes[i] == '\n') {
            out.write('n');
            start = i + 1;
          } else {
            // the current char will be written out later.
            start = i;
          }
        }
      }
    } else {
      out.write(bytes, start, len);
    }
  }
Write the bytes with special characters escaped.

@param escaped
         Whether the data should be written out in an escaped way.
@param escapeChar
         If escaped, the char for prefixing special characters.
@param needsEscape
         If escaped, whether a specific character needs escaping. This
         array should have size of 256.
serde/src/java/org/apache/hadoop/hive/serde2/lazy/LazyUtils.java