Example #1
0
  // {{{ write() method
  protected void write(Buffer buffer, OutputStream out) throws IOException {
    String encodingName = buffer.getStringProperty(JEditBuffer.ENCODING);
    Encoding encoding = EncodingServer.getEncoding(encodingName);
    Writer writer = encoding.getTextWriter(new BufferedOutputStream(out, getByteIOBufferSize()));

    Segment lineSegment = new Segment();
    String newline = buffer.getStringProperty(JEditBuffer.LINESEP);
    if (newline == null) newline = System.getProperty("line.separator");

    final int bufferLineCount = buffer.getLineCount();
    setMaximum(bufferLineCount / PROGRESS_INTERVAL);
    setValue(0);

    int i = 0;
    while (i < bufferLineCount) {
      buffer.getLineText(i, lineSegment);
      try {
        writer.write(lineSegment.array, lineSegment.offset, lineSegment.count);
        if (i < bufferLineCount - 1
            || (jEdit.getBooleanProperty("stripTrailingEOL")
                && buffer.getBooleanProperty(Buffer.TRAILING_EOL))) {
          writer.write(newline);
        }
      } catch (CharacterCodingException e) {
        String message =
            getWriteEncodingErrorMessage(
                encodingName, encoding,
                lineSegment, i);
        IOException wrapping = new CharConversionException(message);
        wrapping.initCause(e);
        throw wrapping;
      }

      if (++i % PROGRESS_INTERVAL == 0) setValue(i / PROGRESS_INTERVAL);
    }
    writer.flush();
  } // }}}
Example #2
0
  // {{{ getFirstGuiltyCharacterIndex() method
  // Look for the first character which causes encoding error.
  private static int getFirstGuiltyCharacterIndex(Encoding encoding, Segment line)
      throws IOException {
    if (line.count < 1) {
      return -1;
    } else if (line.count == 1) {
      return 0;
    }

    Writer tester =
        encoding.getTextWriter(
            new OutputStream() {
              public void write(int b) {}
            });
    for (int i = 0; i < line.count; ++i) {
      try {
        tester.write(line.array[line.offset + i]);
      } catch (CharacterCodingException e) {
        return i;
      }
    }
    return -1;
  } // }}}