Improve the performance of the IndentingWriter

This commit is contained in:
Ben Gruver 2013-04-09 17:40:19 -07:00
parent 8c2d92d954
commit 7e58d497ef
2 changed files with 54 additions and 73 deletions

View File

@ -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);
}
}

View File

@ -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<indentLevel; i++) {
writer.write(' ');
@ -54,9 +51,6 @@ public class IndentingWriter extends Writer {
@Override
public void write(int chr) throws IOException {
if (beginningOfLine) {
writeLineStart();
}
if (chr == '\n') {
writer.write(newLine);
beginningOfLine = true;
@ -69,34 +63,73 @@ public class IndentingWriter extends Writer {
}
}
/**
* Writes out a block of text that contains no newlines
*/
private void writeLine(char[] chars, int start, int len) throws IOException {
if (beginningOfLine && len > 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<s.length(); i++) {
write(s.charAt(i));
}
write(s, 0, s.length());
}
@Override
public void write(String str, int start, int len) throws IOException {
len = start+len;
while (start < len) {
write(str.charAt(start++));
final int end = start+len;
int pos = start;
while (pos < end) {
pos = str.indexOf('\n', start);
if (pos == -1) {
writeLine(str, start, end-start);
return;
} else {
writeLine(str, start, pos-start);
writer.write(newLine);
beginningOfLine = true;
start = pos+1;
}
}
}
@ -155,9 +188,7 @@ public class IndentingWriter extends Writer {
value >>>= 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);
}
}