aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorBen Gruver <bgruv@google.com>2013-05-03 19:35:47 -0700
committerBen Gruver <bgruv@google.com>2013-05-03 19:35:47 -0700
commitd38f3a79838220a666f80c7c2495a184d319a960 (patch)
treed0da6a724758fc7f6c1a97568805a99034cc5673 /util
parent6fc32629c25d351119395922a6eb6701f09dffa4 (diff)
downloadsmali-d38f3a79838220a666f80c7c2495a184d319a960.tar.gz
Fix up line number handling to be unsigned int safe
Diffstat (limited to 'util')
-rw-r--r--util/src/main/java/org/jf/util/IndentingWriter.java30
-rw-r--r--util/src/test/java/org/jf/util/IndentingWriterTest.java51
2 files changed, 81 insertions, 0 deletions
diff --git a/util/src/main/java/org/jf/util/IndentingWriter.java b/util/src/main/java/org/jf/util/IndentingWriter.java
index 517b4b9d..95d6c320 100644
--- a/util/src/main/java/org/jf/util/IndentingWriter.java
+++ b/util/src/main/java/org/jf/util/IndentingWriter.java
@@ -192,6 +192,26 @@ public class IndentingWriter extends Writer {
writeLine(buffer, bufferIndex, 24-bufferIndex);
}
+ public void printSignedLongAsDec(long value) throws IOException {
+ int bufferIndex = 23;
+
+ if (value < 0) {
+ value *= -1;
+ write('-');
+ }
+
+ do {
+ long digit = value % 10;
+ buffer[bufferIndex--] = (char)(digit + '0');
+
+ value = value / 10;
+ } while (value != 0);
+
+ bufferIndex++;
+
+ writeLine(buffer, bufferIndex, 24-bufferIndex);
+ }
+
public void printSignedIntAsDec(int value) throws IOException {
int bufferIndex = 15;
@@ -211,4 +231,14 @@ public class IndentingWriter extends Writer {
writeLine(buffer, bufferIndex, 16-bufferIndex);
}
+
+ public void printUnsignedIntAsDec(int value) throws IOException {
+ int bufferIndex = 15;
+
+ if (value < 0) {
+ printSignedLongAsDec(value & 0xFFFFFFFFL);
+ } else {
+ printSignedIntAsDec(value);
+ }
+ }
}
diff --git a/util/src/test/java/org/jf/util/IndentingWriterTest.java b/util/src/test/java/org/jf/util/IndentingWriterTest.java
new file mode 100644
index 00000000..457262b5
--- /dev/null
+++ b/util/src/test/java/org/jf/util/IndentingWriterTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2013, 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 junit.framework.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+public class IndentingWriterTest {
+ @Test
+ public void testPrintSignedLongAsDec() throws IOException {
+ StringWriter stringWriter = new StringWriter();
+ IndentingWriter writer = new IndentingWriter(stringWriter);
+
+ writer.printUnsignedIntAsDec(-1);
+ writer.close();
+
+ Assert.assertEquals("4294967295", stringWriter.toString());
+ }
+}