mirror of
https://github.com/revanced/smali.git
synced 2025-05-29 12:20:11 +02:00
Refactor/rewrite the AnnotatedBytes class and helpers
This allows annotations to be added out-of-order, rather than the previous requirement of forward-only annotating
This commit is contained in:
parent
46cefa3d5b
commit
31d87776c4
@ -32,18 +32,20 @@ import org.jf.dexlib2.dexbacked.DexBackedDexFile;
|
|||||||
import org.jf.dexlib2.dexbacked.raw.RawDexFile;
|
import org.jf.dexlib2.dexbacked.raw.RawDexFile;
|
||||||
import org.jf.util.ConsoleUtil;
|
import org.jf.util.ConsoleUtil;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
public class dump {
|
public class dump {
|
||||||
public static void dump(DexBackedDexFile dexFile, String dumpFileName)
|
public static void dump(DexBackedDexFile dexFile, String dumpFileName)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
if (dumpFileName != null) {
|
if (dumpFileName != null) {
|
||||||
FileWriter writer = null;
|
Writer writer = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
writer = new FileWriter(dumpFileName);
|
writer = new BufferedWriter(new FileWriter(dumpFileName));
|
||||||
|
|
||||||
int consoleWidth = ConsoleUtil.getConsoleWidth();
|
int consoleWidth = ConsoleUtil.getConsoleWidth();
|
||||||
if (consoleWidth <= 0) {
|
if (consoleWidth <= 0) {
|
||||||
|
@ -54,21 +54,17 @@ public class ClassDataItem {
|
|||||||
@Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) {
|
@Override protected void annotateItem(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile, int itemIndex) {
|
||||||
DexReader reader = dexFile.readerAt(out.getCursor());
|
DexReader reader = dexFile.readerAt(out.getCursor());
|
||||||
|
|
||||||
int mark = reader.getOffset();
|
|
||||||
int staticFieldsSize = reader.readSmallUleb128();
|
int staticFieldsSize = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "static_fields_size = %d", staticFieldsSize);
|
out.annotateTo(reader.getOffset(), "static_fields_size = %d", staticFieldsSize);
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int instanceFieldsSize = reader.readSmallUleb128();
|
int instanceFieldsSize = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "instance_fields_size = %d", instanceFieldsSize);
|
out.annotateTo(reader.getOffset(), "instance_fields_size = %d", instanceFieldsSize);
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int directMethodsSize = reader.readSmallUleb128();
|
int directMethodsSize = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "direct_methods_size = %d", directMethodsSize);
|
out.annotateTo(reader.getOffset(), "direct_methods_size = %d", directMethodsSize);
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int virtualMethodsSize = reader.readSmallUleb128();
|
int virtualMethodsSize = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "virtual_methods_size = %d", virtualMethodsSize);
|
out.annotateTo(reader.getOffset(), "virtual_methods_size = %d", virtualMethodsSize);
|
||||||
|
|
||||||
int previousIndex = 0;
|
int previousIndex = 0;
|
||||||
for (int i=0; i<staticFieldsSize; i++) {
|
for (int i=0; i<staticFieldsSize; i++) {
|
||||||
@ -105,15 +101,13 @@ public class ClassDataItem {
|
|||||||
|
|
||||||
private int annotateEncodedField(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
|
private int annotateEncodedField(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
|
||||||
@Nonnull DexReader reader, int previousIndex) {
|
@Nonnull DexReader reader, int previousIndex) {
|
||||||
int mark = reader.getOffset();
|
|
||||||
int indexDelta = reader.readSmallUleb128();
|
int indexDelta = reader.readSmallUleb128();
|
||||||
int fieldIndex = previousIndex + indexDelta;
|
int fieldIndex = previousIndex + indexDelta;
|
||||||
out.annotate(reader.getOffset() - mark, "field_idx_diff = %d: %s", indexDelta,
|
out.annotateTo(reader.getOffset(), "field_idx_diff = %d: %s", indexDelta,
|
||||||
FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));
|
FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int accessFlags = reader.readSmallUleb128();
|
int accessFlags = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "access_flags = 0x%x: %s", accessFlags,
|
out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags,
|
||||||
Joiner.on('|').join(AccessFlags.getAccessFlagsForField(accessFlags)));
|
Joiner.on('|').join(AccessFlags.getAccessFlagsForField(accessFlags)));
|
||||||
|
|
||||||
return fieldIndex;
|
return fieldIndex;
|
||||||
@ -121,20 +115,17 @@ public class ClassDataItem {
|
|||||||
|
|
||||||
private int annotateEncodedMethod(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
|
private int annotateEncodedMethod(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
|
||||||
@Nonnull DexReader reader, int previousIndex) {
|
@Nonnull DexReader reader, int previousIndex) {
|
||||||
int mark = reader.getOffset();
|
|
||||||
int indexDelta = reader.readSmallUleb128();
|
int indexDelta = reader.readSmallUleb128();
|
||||||
int methodIndex = previousIndex + indexDelta;
|
int methodIndex = previousIndex + indexDelta;
|
||||||
out.annotate(reader.getOffset() - mark, "method_idx_diff = %d: %s", indexDelta,
|
out.annotateTo(reader.getOffset(), "method_idx_diff = %d: %s", indexDelta,
|
||||||
MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
|
MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int accessFlags = reader.readSmallUleb128();
|
int accessFlags = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "access_flags = 0x%x: %s", accessFlags,
|
out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags,
|
||||||
Joiner.on('|').join(AccessFlags.getAccessFlagsForMethod(accessFlags)));
|
Joiner.on('|').join(AccessFlags.getAccessFlagsForMethod(accessFlags)));
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int codeOffset = reader.readSmallUleb128();
|
int codeOffset = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "code_item[0x%x]", codeOffset);
|
out.annotateTo(reader.getOffset(), "code_item[0x%x]", codeOffset);
|
||||||
|
|
||||||
return methodIndex;
|
return methodIndex;
|
||||||
}
|
}
|
||||||
|
@ -145,17 +145,15 @@ public class CodeItem {
|
|||||||
}
|
}
|
||||||
out.deindent();
|
out.deindent();
|
||||||
|
|
||||||
int mark = reader.getOffset();
|
|
||||||
int handlerListCount = reader.readSmallUleb128();
|
int handlerListCount = reader.readSmallUleb128();
|
||||||
out.annotate(0, "encoded_catch_handler_list:");
|
out.annotate(0, "encoded_catch_handler_list:");
|
||||||
out.annotate(reader.getOffset() - mark, "size = %d", handlerListCount);
|
out.annotateTo(reader.getOffset(), "size = %d", handlerListCount);
|
||||||
out.indent();
|
out.indent();
|
||||||
for (int i=0; i<handlerListCount; i++) {
|
for (int i=0; i<handlerListCount; i++) {
|
||||||
out.annotate(0, "encoded_catch_handler[%d]", i);
|
out.annotate(0, "encoded_catch_handler[%d]", i);
|
||||||
out.indent();
|
out.indent();
|
||||||
mark = reader.getOffset();
|
|
||||||
int handlerCount = reader.readSleb128();
|
int handlerCount = reader.readSleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "size = %d", handlerCount);
|
out.annotateTo(reader.getOffset(), "size = %d", handlerCount);
|
||||||
boolean hasCatchAll = handlerCount <= 0;
|
boolean hasCatchAll = handlerCount <= 0;
|
||||||
handlerCount = Math.abs(handlerCount);
|
handlerCount = Math.abs(handlerCount);
|
||||||
if (handlerCount != 0) {
|
if (handlerCount != 0) {
|
||||||
@ -164,22 +162,18 @@ public class CodeItem {
|
|||||||
for (int j=0; j<handlerCount; j++) {
|
for (int j=0; j<handlerCount; j++) {
|
||||||
out.annotate(0, "encoded_type_addr_pair[%d]", i);
|
out.annotate(0, "encoded_type_addr_pair[%d]", i);
|
||||||
out.indent();
|
out.indent();
|
||||||
mark = reader.getOffset();
|
|
||||||
int typeIndex = reader.readSmallUleb128();
|
int typeIndex = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark,
|
out.annotateTo(reader.getOffset(), TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));
|
||||||
TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));
|
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int handlerAddress = reader.readSmallUleb128();
|
int handlerAddress = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "addr = 0x%x", handlerAddress);
|
out.annotateTo(reader.getOffset(), "addr = 0x%x", handlerAddress);
|
||||||
out.deindent();
|
out.deindent();
|
||||||
}
|
}
|
||||||
out.deindent();
|
out.deindent();
|
||||||
}
|
}
|
||||||
if (hasCatchAll) {
|
if (hasCatchAll) {
|
||||||
mark = reader.getOffset();
|
|
||||||
int catchAllAddress = reader.readSmallUleb128();
|
int catchAllAddress = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "catch_all_addr = 0x%x", catchAllAddress);
|
out.annotateTo(reader.getOffset(), "catch_all_addr = 0x%x", catchAllAddress);
|
||||||
}
|
}
|
||||||
out.deindent();
|
out.deindent();
|
||||||
}
|
}
|
||||||
|
@ -129,21 +129,18 @@ public class EncodedValue {
|
|||||||
public static void annotateEncodedAnnotation(@Nonnull AnnotatedBytes out, @Nonnull DexReader reader) {
|
public static void annotateEncodedAnnotation(@Nonnull AnnotatedBytes out, @Nonnull DexReader reader) {
|
||||||
assert out.getCursor() == reader.getOffset();
|
assert out.getCursor() == reader.getOffset();
|
||||||
|
|
||||||
int mark = reader.getOffset();
|
|
||||||
int typeIndex = reader.readSmallUleb128();
|
int typeIndex = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, TypeIdItem.getReferenceAnnotation(reader.dexBuf, typeIndex));
|
out.annotateTo(reader.getOffset(), TypeIdItem.getReferenceAnnotation(reader.dexBuf, typeIndex));
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int size = reader.readSmallUleb128();
|
int size = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "size: %d", size);
|
out.annotateTo(reader.getOffset(), "size: %d", size);
|
||||||
|
|
||||||
for (int i=0; i<size; i++) {
|
for (int i=0; i<size; i++) {
|
||||||
out.annotate(0, "element[%d]", i);
|
out.annotate(0, "element[%d]", i);
|
||||||
out.indent();
|
out.indent();
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
int nameIndex = reader.readSmallUleb128();
|
int nameIndex = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "name = %s",
|
out.annotateTo(reader.getOffset(), "name = %s",
|
||||||
StringIdItem.getReferenceAnnotation(reader.dexBuf, nameIndex));
|
StringIdItem.getReferenceAnnotation(reader.dexBuf, nameIndex));
|
||||||
|
|
||||||
annotateEncodedValue(out, reader);
|
annotateEncodedValue(out, reader);
|
||||||
@ -155,9 +152,8 @@ public class EncodedValue {
|
|||||||
public static void annotateEncodedArray(@Nonnull AnnotatedBytes out, @Nonnull DexReader reader) {
|
public static void annotateEncodedArray(@Nonnull AnnotatedBytes out, @Nonnull DexReader reader) {
|
||||||
assert out.getCursor() == reader.getOffset();
|
assert out.getCursor() == reader.getOffset();
|
||||||
|
|
||||||
int mark = reader.getOffset();
|
|
||||||
int size = reader.readSmallUleb128();
|
int size = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "size: %d", size);
|
out.annotateTo(reader.getOffset(), "size: %d", size);
|
||||||
|
|
||||||
for (int i=0; i<size; i++) {
|
for (int i=0; i<size; i++) {
|
||||||
out.annotate(0, "element[%d]", i);
|
out.annotate(0, "element[%d]", i);
|
||||||
|
@ -193,7 +193,7 @@ public class HeaderItem {
|
|||||||
out.annotate(4, "data_off: 0x%x", dexFile.readInt(out.getCursor()));
|
out.annotate(4, "data_off: 0x%x", dexFile.readInt(out.getCursor()));
|
||||||
|
|
||||||
if (headerSize > ITEM_SIZE) {
|
if (headerSize > ITEM_SIZE) {
|
||||||
out.annotate(headerSize - ITEM_SIZE, "header padding");
|
out.annotateTo(headerSize, "header padding");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -108,42 +108,42 @@ public class RawDexFile extends DexBackedDexFile.Impl {
|
|||||||
|
|
||||||
int stringCount = headerItem.getStringCount();
|
int stringCount = headerItem.getStringCount();
|
||||||
if (stringCount > 0) {
|
if (stringCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getStringOffset());
|
annotatedBytes.moveTo(headerItem.getStringOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount);
|
StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
int typeCount = headerItem.getTypeCount();
|
int typeCount = headerItem.getTypeCount();
|
||||||
if (typeCount > 0) {
|
if (typeCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getTypeOffset());
|
annotatedBytes.moveTo(headerItem.getTypeOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount);
|
TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
int protoCount = headerItem.getProtoCount();
|
int protoCount = headerItem.getProtoCount();
|
||||||
if (protoCount > 0) {
|
if (protoCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getProtoOffset());
|
annotatedBytes.moveTo(headerItem.getProtoOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount);
|
ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fieldCount = headerItem.getFieldCount();
|
int fieldCount = headerItem.getFieldCount();
|
||||||
if (fieldCount > 0) {
|
if (fieldCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getFieldOffset());
|
annotatedBytes.moveTo(headerItem.getFieldOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount);
|
FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
int methodCount = headerItem.getMethodCount();
|
int methodCount = headerItem.getMethodCount();
|
||||||
if (methodCount > 0) {
|
if (methodCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getMethodOffset());
|
annotatedBytes.moveTo(headerItem.getMethodOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount);
|
MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
int classCount = headerItem.getClassCount();
|
int classCount = headerItem.getClassCount();
|
||||||
if (classCount > 0) {
|
if (classCount > 0) {
|
||||||
annotatedBytes.skipTo(headerItem.getClassOffset());
|
annotatedBytes.moveTo(headerItem.getClassOffset());
|
||||||
annotatedBytes.annotate(0, " ");
|
annotatedBytes.annotate(0, " ");
|
||||||
ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount);
|
ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount);
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ public class RawDexFile extends DexBackedDexFile.Impl {
|
|||||||
for (MapItem mapItem: getMapItems()) {
|
for (MapItem mapItem: getMapItems()) {
|
||||||
SectionAnnotator annotator = annotators.get(mapItem.getType());
|
SectionAnnotator annotator = annotators.get(mapItem.getType());
|
||||||
if (annotator != null) {
|
if (annotator != null) {
|
||||||
annotatedBytes.skipTo(mapItem.getOffset());
|
annotatedBytes.moveTo(mapItem.getOffset());
|
||||||
annotator.annotateSection(annotatedBytes, this, mapItem.getItemCount());
|
annotator.annotateSection(annotatedBytes, this, mapItem.getItemCount());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ public abstract class SectionAnnotator {
|
|||||||
out.annotate(0, "");
|
out.annotate(0, "");
|
||||||
|
|
||||||
for (int i=0; i<itemCount; i++) {
|
for (int i=0; i<itemCount; i++) {
|
||||||
out.skipTo(AlignmentUtils.alignOffset(out.getCursor(), itemAlignment));
|
out.moveTo(AlignmentUtils.alignOffset(out.getCursor(), itemAlignment));
|
||||||
|
|
||||||
String itemIdentity = getItemIdentity(dexFile, i, out.getCursor());
|
String itemIdentity = getItemIdentity(dexFile, i, out.getCursor());
|
||||||
if (itemIdentity != null) {
|
if (itemIdentity != null) {
|
||||||
|
@ -49,13 +49,11 @@ public class StringDataItem {
|
|||||||
int itemIndex) {
|
int itemIndex) {
|
||||||
|
|
||||||
DexReader reader = dexFile.readerAt(out.getCursor());
|
DexReader reader = dexFile.readerAt(out.getCursor());
|
||||||
int mark = reader.getOffset();
|
|
||||||
int utf16Length = reader.readSmallUleb128();
|
int utf16Length = reader.readSmallUleb128();
|
||||||
out.annotate(reader.getOffset() - mark, "utf16_size = %d", utf16Length);
|
out.annotateTo(reader.getOffset(), "utf16_size = %d", utf16Length);
|
||||||
|
|
||||||
mark = reader.getOffset();
|
|
||||||
String value = reader.readString(utf16Length);
|
String value = reader.readString(utf16Length);
|
||||||
out.annotate(reader.getOffset() - mark + 1, "data = \"%s\"", StringUtils.escapeString(value));
|
out.annotateTo(reader.getOffset() + 1, "data = \"%s\"", StringUtils.escapeString(value));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -31,20 +31,41 @@
|
|||||||
|
|
||||||
package org.jf.dexlib2.util;
|
package org.jf.dexlib2.util;
|
||||||
|
|
||||||
|
import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import org.jf.util.Hex;
|
import org.jf.util.Hex;
|
||||||
import org.jf.util.TwoColumnOutput;
|
import org.jf.util.TwoColumnOutput;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Collects/presents a set of textual annotations, each associated with a range of bytes
|
* Collects/presents a set of textual annotations, each associated with a range of bytes or a specific point
|
||||||
|
* between bytes.
|
||||||
|
*
|
||||||
|
* Point annotations cannot occur within the middle of a range annotation, only at the endpoints, or some other area
|
||||||
|
* with no range annotation.
|
||||||
|
*
|
||||||
|
* Multiple point annotations can be defined for a given point. They will be printed in insertion order.
|
||||||
|
*
|
||||||
|
* Only a single range annotation may exist for any given range of bytes. Range annotations may not overlap.
|
||||||
*/
|
*/
|
||||||
public class AnnotatedBytes {
|
public class AnnotatedBytes {
|
||||||
@Nonnull private List<AnnotationItem> annotations = Lists.newArrayList();
|
/**
|
||||||
|
* This defines the bytes ranges and their associated range and point annotations.
|
||||||
|
*
|
||||||
|
* A range is defined by 2 consecutive keys in the map. The first key is the inclusive start point, the second key
|
||||||
|
* is the exclusive end point. The range annotation for a range is associated with the first key for that range.
|
||||||
|
* The point annotations for a point are associated with the key at that point.
|
||||||
|
*/
|
||||||
|
@Nonnull private TreeMap<Integer, AnnotationEndpoint> annotatations = Maps.newTreeMap();
|
||||||
|
|
||||||
private int cursor;
|
private int cursor;
|
||||||
private int indentLevel;
|
private int indentLevel;
|
||||||
|
|
||||||
@ -62,33 +83,141 @@ public class AnnotatedBytes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Skips a portion of the binary output. This is equivalent to calling
|
* Moves the cursor to a new location
|
||||||
* annotate(offset-cursor, "");
|
|
||||||
*
|
*
|
||||||
* @param offset The offset to skip to
|
* @param offset The offset to move to
|
||||||
*/
|
*/
|
||||||
public void skipTo(int offset) {
|
public void moveTo(int offset) {
|
||||||
if (offset < cursor) {
|
cursor = offset;
|
||||||
throw new IllegalArgumentException("skipTo can only skip forward");
|
}
|
||||||
}
|
|
||||||
int delta = offset - cursor;
|
/**
|
||||||
if (delta != 0) {
|
* Moves the cursor forward or backward by some amount
|
||||||
annotate(delta, "");
|
*
|
||||||
}
|
* @param offset The amount to move the cursor
|
||||||
|
*/
|
||||||
|
public void moveBy(int offset) {
|
||||||
|
cursor += offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void annotateTo(int offset, @Nonnull String msg, Object... formatArgs) {
|
||||||
|
annotate(offset - cursor, msg, formatArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an annotation of the given length at the current location.
|
* Add an annotation of the given length at the current location.
|
||||||
*
|
*
|
||||||
|
* The location
|
||||||
|
*
|
||||||
|
*
|
||||||
* @param length the length of data being annotated
|
* @param length the length of data being annotated
|
||||||
* @param msg the annotation message
|
* @param msg the annotation message
|
||||||
* @param formatArgs format arguments to pass to String.format
|
* @param formatArgs format arguments to pass to String.format
|
||||||
*/
|
*/
|
||||||
public void annotate(int length, @Nonnull String msg, Object... formatArgs) {
|
public void annotate(int length, @Nonnull String msg, Object... formatArgs) {
|
||||||
annotations.add(new AnnotationItem(cursor, indentLevel, String.format(msg, formatArgs)));
|
String formattedMsg = String.format(msg, formatArgs);
|
||||||
|
int exclusiveEndOffset = cursor + length;
|
||||||
|
|
||||||
|
AnnotationEndpoint endPoint = null;
|
||||||
|
|
||||||
|
// Do we have an endpoint at the beginning of this annotation already?
|
||||||
|
AnnotationEndpoint startPoint = annotatations.get(cursor);
|
||||||
|
if (startPoint == null) {
|
||||||
|
// Nope. We need to check that we're not in the middle of an existing range annotation.
|
||||||
|
Map.Entry<Integer, AnnotationEndpoint> previousEntry = annotatations.lowerEntry(cursor);
|
||||||
|
if (previousEntry != null) {
|
||||||
|
AnnotationEndpoint previousAnnotations = previousEntry.getValue();
|
||||||
|
AnnotationItem previousRangeAnnotation = previousAnnotations.rangeAnnotation;
|
||||||
|
if (previousRangeAnnotation != null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Cannot add annotation %s, due to existing annotation %s",
|
||||||
|
formatAnnotation(cursor, cursor + length, formattedMsg),
|
||||||
|
formatAnnotation(previousEntry.getKey(), previousRangeAnnotation.annotation)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (length > 0) {
|
||||||
|
AnnotationItem existingRangeAnnotation = startPoint.rangeAnnotation;
|
||||||
|
if (existingRangeAnnotation != null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Cannot add annotation %s, due to existing annotation %s",
|
||||||
|
formatAnnotation(cursor, cursor + length, formattedMsg),
|
||||||
|
formatAnnotation(cursor, existingRangeAnnotation.annotation)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length > 0) {
|
||||||
|
// Ensure that there is no later annotation that would intersect with this one
|
||||||
|
Map.Entry<Integer, AnnotationEndpoint> nextEntry = annotatations.higherEntry(cursor);
|
||||||
|
if (nextEntry != null) {
|
||||||
|
int nextKey = nextEntry.getKey();
|
||||||
|
if (nextKey < exclusiveEndOffset) {
|
||||||
|
// there is an endpoint that would intersect with this annotation. Find one of the annotations
|
||||||
|
// associated with the endpoint, to print in the error message
|
||||||
|
AnnotationEndpoint nextEndpoint = nextEntry.getValue();
|
||||||
|
AnnotationItem nextRangeAnnotation = nextEndpoint.rangeAnnotation;
|
||||||
|
if (nextRangeAnnotation != null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Cannot add annotation %s, due to existing annotation %s",
|
||||||
|
formatAnnotation(cursor, cursor + length, formattedMsg),
|
||||||
|
formatAnnotation(nextKey, nextRangeAnnotation.annotation)));
|
||||||
|
}
|
||||||
|
if (nextEndpoint.pointAnnotations.size() > 0) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Cannot add annotation %s, due to existing annotation %s",
|
||||||
|
formatAnnotation(cursor, cursor + length, formattedMsg),
|
||||||
|
formatAnnotation(nextKey, nextKey,
|
||||||
|
nextEndpoint.pointAnnotations.get(0).annotation)));
|
||||||
|
}
|
||||||
|
// There are no annotations on this endpoint. This "shouldn't" happen. We can still throw an exception.
|
||||||
|
throw new IllegalStateException(
|
||||||
|
String.format("Cannot add annotation %s, due to existing annotation endpoint at %d",
|
||||||
|
formatAnnotation(cursor, cursor + length, formattedMsg),
|
||||||
|
nextKey));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextKey == exclusiveEndOffset) {
|
||||||
|
// the next endpoint matches the end of the annotation we are adding
|
||||||
|
endPoint = nextEntry.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now, actually add the annotation
|
||||||
|
// If startPoint is null, we need to create a new one and add it to annotations. Otherwise, we just need to add
|
||||||
|
// the annotation to the existing AnnotationEndpoint
|
||||||
|
// the range annotation
|
||||||
|
if (startPoint == null) {
|
||||||
|
startPoint = new AnnotationEndpoint();
|
||||||
|
annotatations.put(cursor, startPoint);
|
||||||
|
}
|
||||||
|
if (length == 0) {
|
||||||
|
startPoint.pointAnnotations.add(new AnnotationItem(indentLevel, formattedMsg));
|
||||||
|
} else {
|
||||||
|
startPoint.rangeAnnotation = new AnnotationItem(indentLevel, formattedMsg);
|
||||||
|
|
||||||
|
// If endPoint is null, we need to create a new, empty one and add it to annotations
|
||||||
|
if (endPoint == null) {
|
||||||
|
endPoint = new AnnotationEndpoint();
|
||||||
|
annotatations.put(exclusiveEndOffset, endPoint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cursor += length;
|
cursor += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String formatAnnotation(int offset, String annotationMsg) {
|
||||||
|
Integer endOffset = annotatations.higherKey(offset);
|
||||||
|
return formatAnnotation(offset, endOffset, annotationMsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatAnnotation(int offset, Integer endOffset, String annotationMsg) {
|
||||||
|
if (endOffset != null) {
|
||||||
|
return String.format("[0x%x, 0x%x) \"%s\"", offset, endOffset, annotationMsg);
|
||||||
|
} else {
|
||||||
|
return String.format("[0x%x, ) \"%s\"", offset, annotationMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void indent() {
|
public void indent() {
|
||||||
indentLevel++;
|
indentLevel++;
|
||||||
}
|
}
|
||||||
@ -104,13 +233,20 @@ public class AnnotatedBytes {
|
|||||||
return cursor;
|
return cursor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class AnnotationEndpoint {
|
||||||
|
/** Annotations that are associated with a specific point between bytes */
|
||||||
|
@Nonnull
|
||||||
|
public final List<AnnotationItem> pointAnnotations = Lists.newArrayList();
|
||||||
|
/** Annotations that are associated with a range of bytes */
|
||||||
|
@Nullable
|
||||||
|
public AnnotationItem rangeAnnotation = null;
|
||||||
|
}
|
||||||
|
|
||||||
private static class AnnotationItem {
|
private static class AnnotationItem {
|
||||||
public final int offset;
|
|
||||||
public final int indentLevel;
|
public final int indentLevel;
|
||||||
public final String annotation;
|
public final String annotation;
|
||||||
|
|
||||||
public AnnotationItem(int offset, int indentLevel, String annotation) {
|
public AnnotationItem(int indentLevel, String annotation) {
|
||||||
this.offset = offset;
|
|
||||||
this.indentLevel = indentLevel;
|
this.indentLevel = indentLevel;
|
||||||
this.annotation = annotation;
|
this.annotation = annotation;
|
||||||
}
|
}
|
||||||
@ -135,52 +271,45 @@ public class AnnotatedBytes {
|
|||||||
int rightWidth = getAnnotationWidth();
|
int rightWidth = getAnnotationWidth();
|
||||||
int leftWidth = outputWidth - rightWidth - 1;
|
int leftWidth = outputWidth - rightWidth - 1;
|
||||||
|
|
||||||
StringBuilder padding = new StringBuilder();
|
String padding = Strings.repeat(" ", 1000);
|
||||||
for (int i=0; i<1000; i++) {
|
|
||||||
padding.append(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
TwoColumnOutput twoc = new TwoColumnOutput(out, leftWidth, rightWidth, "|");
|
TwoColumnOutput twoc = new TwoColumnOutput(out, leftWidth, rightWidth, "|");
|
||||||
Writer left = twoc.getLeft();
|
|
||||||
Writer right = twoc.getRight();
|
|
||||||
int leftAt = 0; // left-hand byte output cursor
|
|
||||||
int rightAt = 0; // right-hand annotation index
|
|
||||||
int rightSz = annotations.size();
|
|
||||||
|
|
||||||
while ((leftAt < cursor) && (rightAt < rightSz)) {
|
Integer[] keys = new Integer[annotatations.size()];
|
||||||
AnnotationItem a = annotations.get(rightAt);
|
keys = annotatations.keySet().toArray(keys);
|
||||||
int start = a.offset;
|
|
||||||
int end;
|
|
||||||
|
|
||||||
if (rightAt + 1 < annotations.size()) {
|
AnnotationEndpoint[] values = new AnnotationEndpoint[annotatations.size()];
|
||||||
end = annotations.get(rightAt+1).offset;
|
values = annotatations.values().toArray(values);
|
||||||
} else {
|
|
||||||
end = cursor;
|
for (int i=0; i<keys.length-1; i++) {
|
||||||
|
int rangeStart = keys[i];
|
||||||
|
int rangeEnd = keys[i+1];
|
||||||
|
|
||||||
|
AnnotationEndpoint annotations = values[i];
|
||||||
|
|
||||||
|
for (AnnotationItem pointAnnotation: annotations.pointAnnotations) {
|
||||||
|
String paddingSub = padding.substring(0, pointAnnotation.indentLevel*2);
|
||||||
|
twoc.write("", paddingSub + pointAnnotation.annotation);
|
||||||
}
|
}
|
||||||
String text;
|
|
||||||
|
|
||||||
// This is an area with an annotation.
|
String right;
|
||||||
text = padding.substring(0, a.indentLevel * 2) + a.annotation;
|
AnnotationItem rangeAnnotation = annotations.rangeAnnotation;
|
||||||
rightAt++;
|
if (rangeAnnotation != null) {
|
||||||
|
right = padding.substring(0, rangeAnnotation.indentLevel*2);
|
||||||
|
right += rangeAnnotation.annotation;
|
||||||
|
} else {
|
||||||
|
right = "";
|
||||||
|
}
|
||||||
|
|
||||||
left.write(Hex.dump(data, start, end - start, start, hexCols, 6));
|
String left = Hex.dump(data, rangeStart, rangeEnd - rangeStart, rangeStart, hexCols, 6);
|
||||||
right.write(text);
|
|
||||||
twoc.flush();
|
twoc.write(left, right);
|
||||||
leftAt = end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftAt < cursor) {
|
int lastKey = keys[keys.length-1];
|
||||||
// There is unannotated output at the end.
|
if (lastKey < data.length) {
|
||||||
left.write(Hex.dump(data, leftAt, cursor - leftAt, leftAt,
|
String left = Hex.dump(data, lastKey, data.length - lastKey, lastKey, hexCols, 6);
|
||||||
hexCols, 6));
|
twoc.write(left, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (rightAt < rightSz) {
|
|
||||||
// There are zero-byte annotations at the end.
|
|
||||||
right.write(annotations.get(rightAt).annotation);
|
|
||||||
rightAt++;
|
|
||||||
}
|
|
||||||
|
|
||||||
twoc.flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
106
util/src/main/java/org/jf/util/StringWrapper.java
Normal file
106
util/src/main/java/org/jf/util/StringWrapper.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* 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 javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
public class StringWrapper {
|
||||||
|
/**
|
||||||
|
* Splits the given string into lines using on any embedded newlines, and wrapping the text as needed to conform to
|
||||||
|
* the given maximum line width.
|
||||||
|
*
|
||||||
|
* This uses and assumes unix-style newlines
|
||||||
|
*
|
||||||
|
* @param str The string to split
|
||||||
|
* @param maxWidth The maximum length of any line
|
||||||
|
* @param output If given, try to use this array as the return value. If there are more values than will fit
|
||||||
|
* into the array, a new array will be allocated and returned, while the given array will be filled
|
||||||
|
* with as many lines as would fit.
|
||||||
|
* @return The split lines from the original, as an array of Strings. The returned array may be larger than the
|
||||||
|
* number of lines. If this is the case, the end of the split lines will be denoted by a null entry in the
|
||||||
|
* array. If there is no null entry, then the size of the array exactly matches the number of lines.
|
||||||
|
* The returned lines will not contain an ending newline
|
||||||
|
*/
|
||||||
|
public static String[] wrapString(@Nonnull String str, int maxWidth, @Nullable String[] output) {
|
||||||
|
if (output == null) {
|
||||||
|
output = new String[(int)((str.length() / maxWidth) * 1.5d + 1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
int lineStart = 0;
|
||||||
|
int arrayIndex = 0;
|
||||||
|
int i;
|
||||||
|
for (i=0; i<str.length(); i++) {
|
||||||
|
char c = str.charAt(i);
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
output = addString(output, str.substring(lineStart, i), arrayIndex++);
|
||||||
|
lineStart = i+1;
|
||||||
|
} else if (i - lineStart == maxWidth) {
|
||||||
|
output = addString(output, str.substring(lineStart, i), arrayIndex++);
|
||||||
|
lineStart = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (lineStart != i || i == 0) {
|
||||||
|
output = addString(output, str.substring(lineStart), arrayIndex++, output.length+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arrayIndex < output.length) {
|
||||||
|
output[arrayIndex] = null;
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] addString(@Nonnull String[] arr, String str, int index) {
|
||||||
|
if (index >= arr.length) {
|
||||||
|
arr = enlargeArray(arr, (int)(Math.ceil((arr.length + 1) * 1.5)));
|
||||||
|
}
|
||||||
|
|
||||||
|
arr[index] = str;
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] addString(@Nonnull String[] arr, String str, int index, int newLength) {
|
||||||
|
if (index >= arr.length) {
|
||||||
|
arr = enlargeArray(arr, newLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
arr[index] = str;
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String[] enlargeArray(String[] arr, int newLength) {
|
||||||
|
String[] newArr = new String[newLength];
|
||||||
|
System.arraycopy(arr, 0, newArr, 0, arr.length);
|
||||||
|
return newArr;
|
||||||
|
}
|
||||||
|
}
|
@ -31,7 +31,11 @@
|
|||||||
|
|
||||||
package org.jf.util;
|
package org.jf.util;
|
||||||
|
|
||||||
import java.io.*;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that takes a combined output destination and provides two
|
* Class that takes a combined output destination and provides two
|
||||||
@ -45,48 +49,9 @@ public final class TwoColumnOutput {
|
|||||||
/** > 0; the left column width */
|
/** > 0; the left column width */
|
||||||
private final int leftWidth;
|
private final int leftWidth;
|
||||||
|
|
||||||
/** non-null; pending left column output */
|
private final int rightWidth;
|
||||||
private final StringBuffer leftBuf;
|
|
||||||
|
|
||||||
/** non-null; pending right column output */
|
private final String spacer;
|
||||||
private final StringBuffer rightBuf;
|
|
||||||
|
|
||||||
/** non-null; left column writer */
|
|
||||||
private final WrappedIndentingWriter leftColumn;
|
|
||||||
|
|
||||||
/** non-null; right column writer */
|
|
||||||
private final WrappedIndentingWriter rightColumn;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Turns the given two strings (with widths) and spacer into a formatted
|
|
||||||
* two-column string.
|
|
||||||
*
|
|
||||||
* @param s1 non-null; first string
|
|
||||||
* @param width1 > 0; width of the first column
|
|
||||||
* @param spacer non-null; spacer string
|
|
||||||
* @param s2 non-null; second string
|
|
||||||
* @param width2 > 0; width of the second column
|
|
||||||
* @return non-null; an appropriately-formatted string
|
|
||||||
*/
|
|
||||||
public static String toString(String s1, int width1, String spacer,
|
|
||||||
String s2, int width2) {
|
|
||||||
int len1 = s1.length();
|
|
||||||
int len2 = s2.length();
|
|
||||||
|
|
||||||
StringWriter sw = new StringWriter((len1 + len2) * 3);
|
|
||||||
TwoColumnOutput twoOut =
|
|
||||||
new TwoColumnOutput(sw, width1, width2, spacer);
|
|
||||||
|
|
||||||
try {
|
|
||||||
twoOut.getLeft().write(s1);
|
|
||||||
twoOut.getRight().write(s2);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new RuntimeException("shouldn't happen", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
twoOut.flush();
|
|
||||||
return sw.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an instance.
|
* Constructs an instance.
|
||||||
@ -96,11 +61,8 @@ public final class TwoColumnOutput {
|
|||||||
* @param rightWidth > 0; width of the right column, in characters
|
* @param rightWidth > 0; width of the right column, in characters
|
||||||
* @param spacer non-null; spacer string to sit between the two columns
|
* @param spacer non-null; spacer string to sit between the two columns
|
||||||
*/
|
*/
|
||||||
public TwoColumnOutput(Writer out, int leftWidth, int rightWidth,
|
public TwoColumnOutput(@Nonnull Writer out, int leftWidth, int rightWidth,
|
||||||
String spacer) {
|
@Nonnull String spacer) {
|
||||||
if (out == null) {
|
|
||||||
throw new NullPointerException("out == null");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (leftWidth < 1) {
|
if (leftWidth < 1) {
|
||||||
throw new IllegalArgumentException("leftWidth < 1");
|
throw new IllegalArgumentException("leftWidth < 1");
|
||||||
@ -110,20 +72,10 @@ public final class TwoColumnOutput {
|
|||||||
throw new IllegalArgumentException("rightWidth < 1");
|
throw new IllegalArgumentException("rightWidth < 1");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (spacer == null) {
|
|
||||||
throw new NullPointerException("spacer == null");
|
|
||||||
}
|
|
||||||
|
|
||||||
StringWriter leftWriter = new StringWriter(1000);
|
|
||||||
StringWriter rightWriter = new StringWriter(1000);
|
|
||||||
|
|
||||||
this.out = out;
|
this.out = out;
|
||||||
this.leftWidth = leftWidth;
|
this.leftWidth = leftWidth;
|
||||||
this.leftBuf = leftWriter.getBuffer();
|
this.rightWidth = rightWidth;
|
||||||
this.rightBuf = rightWriter.getBuffer();
|
this.spacer = spacer;
|
||||||
this.leftColumn = new WrappedIndentingWriter(leftWriter, leftWidth);
|
|
||||||
this.rightColumn =
|
|
||||||
new WrappedIndentingWriter(rightWriter, rightWidth, spacer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -139,114 +91,52 @@ public final class TwoColumnOutput {
|
|||||||
this(new OutputStreamWriter(out), leftWidth, rightWidth, spacer);
|
this(new OutputStreamWriter(out), leftWidth, rightWidth, spacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private String[] leftLines = null;
|
||||||
* Gets the writer to use to write to the left column.
|
private String[] rightLines = null;
|
||||||
*
|
public void write(String left, String right) throws IOException {
|
||||||
* @return non-null; the left column writer
|
leftLines = StringWrapper.wrapString(left, leftWidth, leftLines);
|
||||||
*/
|
rightLines = StringWrapper.wrapString(right, rightWidth, rightLines);
|
||||||
public Writer getLeft() {
|
int leftCount = leftLines.length;
|
||||||
return leftColumn;
|
int rightCount = rightLines.length;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
for (int i=0; i<leftCount || i <rightCount; i++) {
|
||||||
* Gets the writer to use to write to the right column.
|
String leftLine = null;
|
||||||
*
|
String rightLine = null;
|
||||||
* @return non-null; the right column writer
|
|
||||||
*/
|
|
||||||
public Writer getRight() {
|
|
||||||
return rightColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (i < leftCount) {
|
||||||
* Flushes the output. If there are more lines of pending output in one
|
leftLine = leftLines[i];
|
||||||
* column, then the other column will get filled with blank lines.
|
if (leftLine == null) {
|
||||||
*/
|
leftCount = i;
|
||||||
public void flush() {
|
}
|
||||||
try {
|
|
||||||
appendNewlineIfNecessary(leftBuf, leftColumn);
|
|
||||||
appendNewlineIfNecessary(rightBuf, rightColumn);
|
|
||||||
outputFullLines();
|
|
||||||
flushLeft();
|
|
||||||
flushRight();
|
|
||||||
} catch (IOException ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Outputs to the final destination as many full line pairs as
|
|
||||||
* there are in the pending output, removing those lines from
|
|
||||||
* their respective buffers. This method terminates when at
|
|
||||||
* least one of the two column buffers is empty.
|
|
||||||
*/
|
|
||||||
private void outputFullLines() throws IOException {
|
|
||||||
for (;;) {
|
|
||||||
int leftLen = leftBuf.indexOf("\n");
|
|
||||||
if (leftLen < 0) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int rightLen = rightBuf.indexOf("\n");
|
if (i < rightCount) {
|
||||||
if (rightLen < 0) {
|
rightLine = rightLines[i];
|
||||||
return;
|
if (rightLine == null) {
|
||||||
|
rightCount = i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftLen != 0) {
|
if (leftLine != null || rightLine != null) {
|
||||||
out.write(leftBuf.substring(0, leftLen));
|
int written = 0;
|
||||||
|
if (leftLine != null) {
|
||||||
|
out.write(leftLine);
|
||||||
|
written = leftLine.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
int remaining = leftWidth - written;
|
||||||
|
if (remaining > 0) {
|
||||||
|
writeSpaces(out, remaining);
|
||||||
|
}
|
||||||
|
|
||||||
|
out.write(spacer);
|
||||||
|
|
||||||
|
if (rightLine != null) {
|
||||||
|
out.write(rightLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
out.write('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rightLen != 0) {
|
|
||||||
writeSpaces(out, leftWidth - leftLen);
|
|
||||||
out.write(rightBuf.substring(0, rightLen));
|
|
||||||
}
|
|
||||||
|
|
||||||
out.write('\n');
|
|
||||||
|
|
||||||
leftBuf.delete(0, leftLen + 1);
|
|
||||||
rightBuf.delete(0, rightLen + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flushes the left column buffer, printing it and clearing the buffer.
|
|
||||||
* If the buffer is already empty, this does nothing.
|
|
||||||
*/
|
|
||||||
private void flushLeft() throws IOException {
|
|
||||||
appendNewlineIfNecessary(leftBuf, leftColumn);
|
|
||||||
|
|
||||||
while (leftBuf.length() != 0) {
|
|
||||||
rightColumn.write('\n');
|
|
||||||
outputFullLines();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flushes the right column buffer, printing it and clearing the buffer.
|
|
||||||
* If the buffer is already empty, this does nothing.
|
|
||||||
*/
|
|
||||||
private void flushRight() throws IOException {
|
|
||||||
appendNewlineIfNecessary(rightBuf, rightColumn);
|
|
||||||
|
|
||||||
while (rightBuf.length() != 0) {
|
|
||||||
leftColumn.write('\n');
|
|
||||||
outputFullLines();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Appends a newline to the given buffer via the given writer, but
|
|
||||||
* only if it isn't empty and doesn't already end with one.
|
|
||||||
*
|
|
||||||
* @param buf non-null; the buffer in question
|
|
||||||
* @param out non-null; the writer to use
|
|
||||||
*/
|
|
||||||
private static void appendNewlineIfNecessary(StringBuffer buf,
|
|
||||||
Writer out)
|
|
||||||
throws IOException {
|
|
||||||
int len = buf.length();
|
|
||||||
|
|
||||||
if ((len != 0) && (buf.charAt(len - 1) != '\n')) {
|
|
||||||
out.write('\n');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
118
util/src/test/java/org/jf/util/StringWrapperTest.java
Normal file
118
util/src/test/java/org/jf/util/StringWrapperTest.java
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* 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 org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class StringWrapperTest {
|
||||||
|
@Test
|
||||||
|
public void testWrapString() {
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abc", "abcdef", "abcdef"},
|
||||||
|
StringWrapper.wrapString("abc\nabcdefabcdef", 6, null));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abc"},
|
||||||
|
StringWrapper.wrapString("abc", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abc"},
|
||||||
|
StringWrapper.wrapString("abc", 6, new String[0]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abc"},
|
||||||
|
StringWrapper.wrapString("abc", 6, new String[1]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{""},
|
||||||
|
StringWrapper.wrapString("", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abcdef"},
|
||||||
|
StringWrapper.wrapString("abcdef", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abcdef", "abcdef"},
|
||||||
|
StringWrapper.wrapString("abcdef\nabcdef", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"abc", "", "def"},
|
||||||
|
StringWrapper.wrapString("abc\n\ndef", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "abcdef"},
|
||||||
|
StringWrapper.wrapString("\nabcdef", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef"},
|
||||||
|
StringWrapper.wrapString("\n\nabcdef", 6, new String[3]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef"},
|
||||||
|
StringWrapper.wrapString("\n\nabcdef", 6, new String[4]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef", ""},
|
||||||
|
StringWrapper.wrapString("\n\nabcdef\n\n", 6, new String[4]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef", "a", ""},
|
||||||
|
StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[4]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef", "a", ""},
|
||||||
|
StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[0]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "abcdef", "a", ""},
|
||||||
|
StringWrapper.wrapString("\n\nabcdefa\n\n", 6, new String[5]));
|
||||||
|
|
||||||
|
validateResult(
|
||||||
|
new String[]{"", "", "a", "b", "c", "d", "e", "f", "a", ""},
|
||||||
|
StringWrapper.wrapString("\n\nabcdefa\n\n", 1, new String[5]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void validateResult(String[] expected, String[] actual) {
|
||||||
|
Assert.assertTrue(actual.length >= expected.length);
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i=0; i<actual.length; i++) {
|
||||||
|
if (actual[i] == null) {
|
||||||
|
Assert.assertTrue(i == expected.length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Assert.assertTrue(i < expected.length);
|
||||||
|
Assert.assertEquals(expected[i], actual[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user