From 7e58d497ef4ff49f9cc11930ae3d9fb3fc191346 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Tue, 9 Apr 2013 17:40:19 -0700 Subject: [PATCH] Improve the performance of the IndentingWriter --- .../jf/util/CommentingIndentingWriter.java | 48 ----------- .../java/org/jf/util/IndentingWriter.java | 79 +++++++++++++------ 2 files changed, 54 insertions(+), 73 deletions(-) delete mode 100644 util/src/main/java/org/jf/util/CommentingIndentingWriter.java diff --git a/util/src/main/java/org/jf/util/CommentingIndentingWriter.java b/util/src/main/java/org/jf/util/CommentingIndentingWriter.java deleted file mode 100644 index 9b1de4f3..00000000 --- a/util/src/main/java/org/jf/util/CommentingIndentingWriter.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2012, Google Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.jf.util; - -import java.io.IOException; -import java.io.Writer; - -public class CommentingIndentingWriter extends IndentingWriter { - private final String commentStr; - - public CommentingIndentingWriter(Writer writer, String commentStr) { - super(writer); - this.commentStr = commentStr; - } - - protected void writeLineStart() throws IOException { - writer.write(commentStr); - } -} diff --git a/util/src/main/java/org/jf/util/IndentingWriter.java b/util/src/main/java/org/jf/util/IndentingWriter.java index f7eb378b..40401405 100644 --- a/util/src/main/java/org/jf/util/IndentingWriter.java +++ b/util/src/main/java/org/jf/util/IndentingWriter.java @@ -43,9 +43,6 @@ public class IndentingWriter extends Writer { this.writer = writer; } - protected void writeLineStart() throws IOException { - } - protected void writeIndent() throws IOException { for (int i=0; i 0) { + writeIndent(); + beginningOfLine = false; + } + writer.write(chars, start, len); + } + + + /** + * Writes out a block of text that contains no newlines + */ + private void writeLine(String str, int start, int len) throws IOException { + if (beginningOfLine && len > 0) { + writeIndent(); + beginningOfLine = false; + } + writer.write(str, start, len); + } + @Override public void write(char[] chars) throws IOException { - for (char chr: chars) { - write(chr); - } + write(chars, 0, chars.length); } @Override public void write(char[] chars, int start, int len) throws IOException { - // TODO: it might improve performance to scan until we reach a newline, and then submit a full chunk of chars at once - len = start+len; - while (start < len) { - write(chars[start++]); + final int end = start+len; + int pos = start; + while (pos < end) { + if (chars[pos] == '\n') { + writeLine(chars, start, pos-start); + + writer.write(newLine); + beginningOfLine = true; + pos++; + start = pos; + } else { + pos++; + } } + writeLine(chars, start, pos-start); } @Override public void write(String s) throws IOException { - for (int i=0; i>>= 4; } while (value != 0); - while (bufferIndex>0) { - write(buffer[--bufferIndex]); - } + writeLine(buffer, 0, bufferIndex); } public void printSignedIntAsDec(int value) throws IOException { @@ -175,8 +206,6 @@ public class IndentingWriter extends Writer { value = value / 10; } while (value != 0); - while (bufferIndex>0) { - write(buffer[--bufferIndex]); - } + writeLine(buffer, 0, bufferIndex); } }