FileDocCategorySizeDatePackage
BaseDumper.javaAPI DocAndroid 5.1 API8677Thu Mar 12 22:18:30 GMT 2015com.android.dx.command.dump

BaseDumper

public abstract class BaseDumper extends Object implements com.android.dx.cf.iface.ParseObserver
Base class for the various human-friendly dumpers.

Fields Summary
private final byte[]
bytes
{@code non-null;} array of data being dumped
private final boolean
rawBytes
whether or not to include the raw bytes (in a column on the left)
private final PrintStream
out
{@code non-null;} where to dump to
private final int
width
width of the output in columns
private final String
filePath
{@code non-null;} the file path for the class, excluding any base directory specification
private final boolean
strictParse
whether to be strict about parsing
private final int
hexCols
number of bytes per line in hex dumps
private int
indent
the current level of indentation
private String
separator
{@code non-null;} the current column separator string
private int
at
the offset of the next byte to dump
protected Args
args
commandline parsedArgs
Constructors Summary
public BaseDumper(byte[] bytes, PrintStream out, String filePath, Args args)
Constructs an instance.

param
bytes {@code non-null;} bytes of the (alleged) class file on the left)
param
out {@code non-null;} where to dump to
param
filePath the file path for the class, excluding any base directory specification

        this.bytes = bytes;
        this.rawBytes = args.rawBytes;
        this.out = out;
        this.width = (args.width <= 0) ? 79 : args.width;
        this.filePath = filePath;
        this.strictParse = args.strictParse;
        this.indent = 0;
        this.separator = rawBytes ? "|" : "";
        this.at = 0;
        this.args = args;

        int hexCols = (((width - 5) / 15) + 1) & ~1;
        if (hexCols < 6) {
            hexCols = 6;
        } else if (hexCols > 10) {
            hexCols = 10;
        }
        this.hexCols = hexCols;
    
Methods Summary
public voidchangeIndent(int indentDelta)
{@inheritDoc}

        indent += indentDelta;

        separator = rawBytes ? "|" : "";
        for (int i = 0; i < indent; i++) {
            separator += "  ";
        }
    
static intcomputeParamWidth(com.android.dx.cf.code.ConcreteMethod meth, boolean isStatic)
Computes the total width, in register-units, of the parameters for this method.

param
meth method to process
return
width in register-units

        return meth.getEffectiveDescriptor().getParameterTypes().
            getWordCount();
    
public voidendParsingMember(com.android.dx.util.ByteArray bytes, int offset, java.lang.String name, java.lang.String descriptor, com.android.dx.cf.iface.Member member)
{@inheritDoc}

        // This space intentionally left blank.
    
protected final intgetAt()
Gets the current dump cursor (that is, the offset of the expected next byte to dump).

return
{@code >= 0;} the dump cursor

        return at;
    
protected final byte[]getBytes()
Gets the array of {@code byte}s to process.

return
{@code non-null;} the bytes

        return bytes;
    
protected final java.lang.StringgetFilePath()
Gets the filesystem/jar path of the file being dumped.

return
{@code non-null;} the path

        return filePath;
    
protected final booleangetRawBytes()
Gets whether this dump is to include raw bytes.

return
the raw bytes flag

        return rawBytes;
    
protected final booleangetStrictParse()
Gets whether to be strict about parsing.

return
whether to be strict about parsing

        return strictParse;
    
protected final intgetWidth1()
Gets the width of the first column of output. This is {@code 0} unless raw bytes are being included in the output.

return
{@code >= 0;} the width of the first column

        if (rawBytes) {
            return 5 + (hexCols * 2) + (hexCols / 2);
        }

        return 0;
    
protected final intgetWidth2()
Gets the width of the second column of output.

return
{@code >= 0;} the width of the second column

        int w1 = rawBytes ? (getWidth1() + 1) : 0;
        return width - w1 - (indent * 2);
    
protected final java.lang.StringhexDump(int offset, int len)
Constructs a hex data dump of the given portion of {@link #bytes}.

param
offset offset to start dumping at
param
len length to dump
return
{@code non-null;} the dump

        return Hex.dump(bytes, offset, len, offset, hexCols, 4);
    
public voidparsed(com.android.dx.util.ByteArray bytes, int offset, int len, java.lang.String human)
{@inheritDoc}

        offset = bytes.underlyingOffset(offset, getBytes());

        boolean rawBytes = getRawBytes();

        if (offset < at) {
            println("<dump skipped backwards to " + Hex.u4(offset) + ">");
            at = offset;
        } else if (offset > at) {
            String hex = rawBytes ? hexDump(at, offset - at) : "";
            print(twoColumns(hex, "<skipped to " + Hex.u4(offset) + ">"));
            at = offset;
        }

        String hex = rawBytes ? hexDump(offset, len) : "";
        print(twoColumns(hex, human));
        at += len;
    
protected final voidprint(java.lang.String s)
Prints the given string to this instance's output stream.

param
s {@code null-ok;} string to print

        out.print(s);
    
protected final voidprintln(java.lang.String s)
Prints the given string to this instance's output stream, followed by a newline.

param
s {@code null-ok;} string to print

        out.println(s);
    
protected final voidsetAt(com.android.dx.util.ByteArray arr, int offset)
Sets the dump cursor to the indicated offset in the given array.

param
arr {@code non-null;} array in question
param
offset {@code >= 0;} offset into the array

        at = arr.underlyingOffset(offset, bytes);
    
public voidstartParsingMember(com.android.dx.util.ByteArray bytes, int offset, java.lang.String name, java.lang.String descriptor)
{@inheritDoc}

        // This space intentionally left blank.
    
protected final java.lang.StringtwoColumns(java.lang.String s1, java.lang.String s2)
Combines a pair of strings as two columns, or if this is one-column output, format the otherwise-second column.

param
s1 {@code non-null;} the first column's string
param
s2 {@code non-null;} the second column's string
return
{@code non-null;} the combined output

        int w1 = getWidth1();
        int w2 = getWidth2();

        try {
            if (w1 == 0) {
                int len2 = s2.length();
                StringWriter sw = new StringWriter(len2 * 2);
                IndentingWriter iw = new IndentingWriter(sw, w2, separator);

                iw.write(s2);
                if ((len2 == 0) || (s2.charAt(len2 - 1) != '\n")) {
                    iw.write('\n");
                }
                iw.flush();

                return sw.toString();
            } else {
                return TwoColumnOutput.toString(s1, w1, separator, s2, w2);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }