aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorBen Gruver <bgruv@google.com>2013-04-09 17:40:19 -0700
committerBen Gruver <bgruv@google.com>2013-04-09 17:42:33 -0700
commit7e58d497ef4ff49f9cc11930ae3d9fb3fc191346 (patch)
tree3cb46607b828f0687edf327bce33b6ec8d84a5db /util
parent8c2d92d9546163d274feb0b535ad615942123cfd (diff)
downloadsmali-7e58d497ef4ff49f9cc11930ae3d9fb3fc191346.tar.gz
Improve the performance of the IndentingWriter
Diffstat (limited to 'util')
-rw-r--r--util/src/main/java/org/jf/util/CommentingIndentingWriter.java48
-rw-r--r--util/src/main/java/org/jf/util/IndentingWriter.java79
2 files changed, 54 insertions, 73 deletions
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<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);
}
}