code: public static void writeCharSequence(Writer target, CharSequence csq, int start, int end) throws IOException {
          final Class<?> csqClass = csq.getClass();
          if (csqClass == String.class) {
              target.write((String)csq, start, end - start);
          }
          else if (csqClass == StringBuffer.class) {
              char[] buf = new char[end - start];
              ((StringBuffer)csq).getChars(start, end, buf, 0);
              target.write(buf);
          }
          else if (csqClass == StringBuilder.class) {
              char[] buf = new char[end - start];
              ((StringBuilder)csq).getChars(start, end, buf, 0);
              target.write(buf);
          }
          else if (csq instanceof CharArrayAccessible) {
              char[] buf = new char[end - start];
              ((CharArrayAccessible)csq).getChars(start, end, buf, 0);
              target.write(buf);
          }
          else {
              String str = csq.subSequence(start, end).toString();
              target.write(str, 0, str.length());
          }
      }
docs: Writes a CharSequence instance in the most optimal way to the target writer
  
  
  @param target writer
  @param csq source CharSequence instance
  @param start start/offset index
  @param end end index + 1
  @throws IOException