Esempio n. 1
0
 public static void main(String[] args) throws IOException {
   if (args.length != 1) {
     System.err.println("Usage: java BeanWithCursor [input-file]");
     System.exit(1);
   }
   File f = new File(args[0]);
   new BeanWithCursor().process(f);
 }
  /**
   * This method called when the string content is already in a char buffer, and need not be copied
   * for processing.
   */
  private void _writeString(char[] text, int offset, int len)
      throws IOException, JsonGenerationException {
    /* Let's just find longest spans of non-escapable
     * content, and for each see if it makes sense
     * to copy them, or write through
     */
    len += offset; // -> len marks the end from now on
    final int[] escCodes = CharTypes.getOutputEscapes();
    final int escLen = escCodes.length;
    while (offset < len) {
      int start = offset;

      while (true) {
        char c = text[offset];
        if (c < escLen && escCodes[c] != 0) {
          break;
        }
        if (++offset >= len) {
          break;
        }
      }

      // Short span? Better just copy it to buffer first:
      int newAmount = offset - start;
      if (newAmount < SHORT_WRITE) {
        // Note: let's reserve room for escaped char (up to 6 chars)
        if ((_outputTail + newAmount) > _outputEnd) {
          _flushBuffer();
        }
        if (newAmount > 0) {
          System.arraycopy(text, start, _outputBuffer, _outputTail, newAmount);
          _outputTail += newAmount;
        }
      } else { // Nope: better just write through
        _flushBuffer();
        _writer.write(text, start, newAmount);
      }
      // Was this the end?
      if (offset >= len) { // yup
        break;
      }
      // Nope, need to escape the char.
      int escCode = escCodes[text[offset]];
      ++offset;
      int needLen = (escCode < 0) ? 6 : 2;
      if ((_outputTail + needLen) > _outputEnd) {
        _flushBuffer();
      }
      _appendSingleEscape(escCode, _outputBuffer, _outputTail);
      _outputTail += needLen;
    }
  }
 public void writeRaw(char[] text, int offset, int len)
     throws IOException, JsonGenerationException {
   // Only worth buffering if it's a short write?
   if (len < SHORT_WRITE) {
     int room = _outputEnd - _outputTail;
     if (len > room) {
       _flushBuffer();
     }
     System.arraycopy(text, offset, _outputBuffer, _outputTail, len);
     _outputTail += len;
     return;
   }
   // Otherwise, better just pass through:
   _flushBuffer();
   _writer.write(text, offset, len);
 }