Class ByteArrayAnnotatedOutput

java.lang.Object
com.android.dx.util.ByteArrayAnnotatedOutput
All Implemented Interfaces:
ByteOutput, AnnotatedOutput, Output

public final class ByteArrayAnnotatedOutput extends Object implements AnnotatedOutput, ByteOutput
Implementation of AnnotatedOutput which stores the written data into a byte[].

Note: As per the Output interface, multi-byte writes all use little-endian order.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a "stretchy" instance.
    Constructs an instance with a fixed maximum size.
    Constructs a "stretchy" instance with initial size size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    alignTo(int alignment)
    Adds extra bytes if necessary (with value 0) to force alignment of the output cursor as given.
    void
    annotate(int amt, String msg)
    Add an annotation for a specified amount of subsequent output.
    void
    Add an annotation for the subsequent output.
    boolean
    Get whether this instance will actually keep annotations.
    void
    assertCursor(int expectedCursor)
    Asserts that the cursor is the given value.
    void
    enableAnnotations(int annotationWidth, boolean verbose)
    Indicates that this instance should keep annotations.
    void
    End the most recent annotation.
    void
    Finishes up annotation processing.
    int
    Get the maximum width of the annotated output.
    byte[]
    Gets the underlying byte[] of this instance, which may be larger than the number of bytes written
    int
    Gets the current cursor position.
    boolean
    Get whether this instance is intended to keep verbose annotations.
    byte[]
    Constructs and returns a new byte[] that contains the written contents exactly (that is, with no extra unwritten bytes at the end).
    void
    write(byte[] bytes)
    Writes a byte[] to this instance.
    void
    write(byte[] bytes, int offset, int length)
    Writes a portion of a byte[] to this instance.
    void
    Writes a ByteArray to this instance.
    void
    Writes the annotated content of this instance to the given writer.
    void
    writeByte(int value)
    Writes a byte to this instance.
    void
    writeInt(int value)
    Writes an int to this instance.
    void
    writeLong(long value)
    Writes a long to this instance.
    void
    writeShort(int value)
    Writes a short to this instance.
    int
    writeSleb128(int value)
    Writes a DWARFv3-style unsigned LEB128 integer.
    int
    writeUleb128(int value)
    Writes a DWARFv3-style unsigned LEB128 integer.
    void
    writeZeroes(int count)
    Writes the given number of 0 bytes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteArrayAnnotatedOutput

      public ByteArrayAnnotatedOutput(byte[] data)
      Constructs an instance with a fixed maximum size. Note that the given array is the only one that will be used to store data. In particular, no reallocation will occur in order to expand the capacity of the resulting instance. Also, the constructed instance does not keep annotations by default.
      Parameters:
      data - non-null; data array to use for output
    • ByteArrayAnnotatedOutput

      public ByteArrayAnnotatedOutput()
      Constructs a "stretchy" instance. The underlying array may be reallocated. The constructed instance does not keep annotations by default.
    • ByteArrayAnnotatedOutput

      public ByteArrayAnnotatedOutput(int size)
      Constructs a "stretchy" instance with initial size size. The underlying array may be reallocated. The constructed instance does not keep annotations by default.
  • Method Details

    • getArray

      public byte[] getArray()
      Gets the underlying byte[] of this instance, which may be larger than the number of bytes written
      Returns:
      non-null; the byte[]
      See Also:
    • toByteArray

      public byte[] toByteArray()
      Constructs and returns a new byte[] that contains the written contents exactly (that is, with no extra unwritten bytes at the end).
      Returns:
      non-null; an appropriately-constructed array
      See Also:
    • getCursor

      public int getCursor()
      Gets the current cursor position. This is the same as the number of bytes written to this instance.
      Specified by:
      getCursor in interface Output
      Returns:
      >= 0; the cursor position
    • assertCursor

      public void assertCursor(int expectedCursor)
      Asserts that the cursor is the given value.
      Specified by:
      assertCursor in interface Output
      Parameters:
      expectedCursor - the expected cursor value
    • writeByte

      public void writeByte(int value)
      Writes a byte to this instance.
      Specified by:
      writeByte in interface ByteOutput
      Specified by:
      writeByte in interface Output
      Parameters:
      value - the value to write; all but the low 8 bits are ignored
    • writeShort

      public void writeShort(int value)
      Writes a short to this instance.
      Specified by:
      writeShort in interface Output
      Parameters:
      value - the value to write; all but the low 16 bits are ignored
    • writeInt

      public void writeInt(int value)
      Writes an int to this instance.
      Specified by:
      writeInt in interface Output
      Parameters:
      value - the value to write
    • writeLong

      public void writeLong(long value)
      Writes a long to this instance.
      Specified by:
      writeLong in interface Output
      Parameters:
      value - the value to write
    • writeUleb128

      public int writeUleb128(int value)
      Writes a DWARFv3-style unsigned LEB128 integer. For details, see the "Dalvik Executable Format" document or DWARF v3 section 7.6.
      Specified by:
      writeUleb128 in interface Output
      Parameters:
      value - value to write, treated as an unsigned value
      Returns:
      1..5; the number of bytes actually written
    • writeSleb128

      public int writeSleb128(int value)
      Writes a DWARFv3-style unsigned LEB128 integer. For details, see the "Dalvik Executable Format" document or DWARF v3 section 7.6.
      Specified by:
      writeSleb128 in interface Output
      Parameters:
      value - value to write
      Returns:
      1..5; the number of bytes actually written
    • write

      public void write(ByteArray bytes)
      Writes a ByteArray to this instance.
      Specified by:
      write in interface Output
      Parameters:
      bytes - non-null; the array to write
    • write

      public void write(byte[] bytes, int offset, int length)
      Writes a portion of a byte[] to this instance.
      Specified by:
      write in interface Output
      Parameters:
      bytes - non-null; the array to write
      offset - >= 0; offset into bytes for the first byte to write
      length - >= 0; number of bytes to write
    • write

      public void write(byte[] bytes)
      Writes a byte[] to this instance. This is just a convenient shorthand for write(bytes, 0, bytes.length).
      Specified by:
      write in interface Output
      Parameters:
      bytes - non-null; the array to write
    • writeZeroes

      public void writeZeroes(int count)
      Writes the given number of 0 bytes.
      Specified by:
      writeZeroes in interface Output
      Parameters:
      count - >= 0; the number of zeroes to write
    • alignTo

      public void alignTo(int alignment)
      Adds extra bytes if necessary (with value 0) to force alignment of the output cursor as given.
      Specified by:
      alignTo in interface Output
      Parameters:
      alignment - > 0; the alignment; must be a power of two
    • annotates

      public boolean annotates()
      Get whether this instance will actually keep annotations.
      Specified by:
      annotates in interface AnnotatedOutput
      Returns:
      true iff annotations are being kept
    • isVerbose

      public boolean isVerbose()
      Get whether this instance is intended to keep verbose annotations. Annotators may use the result of calling this method to inform their annotation activity.
      Specified by:
      isVerbose in interface AnnotatedOutput
      Returns:
      true iff annotations are to be verbose
    • annotate

      public void annotate(String msg)
      Add an annotation for the subsequent output. Any previously open annotation will be closed by this call, and the new annotation marks all subsequent output until another annotation call.
      Specified by:
      annotate in interface AnnotatedOutput
      Parameters:
      msg - non-null; the annotation message
    • annotate

      public void annotate(int amt, String msg)
      Add an annotation for a specified amount of subsequent output. Any previously open annotation will be closed by this call. If there is already pending annotation from one or more previous calls to this method, the new call "consumes" output after all the output covered by the previous calls.
      Specified by:
      annotate in interface AnnotatedOutput
      Parameters:
      amt - >= 0; the amount of output for this annotation to cover
      msg - non-null; the annotation message
    • endAnnotation

      public void endAnnotation()
      End the most recent annotation. Subsequent output will be unannotated, until the next call to AnnotatedOutput.annotate(java.lang.String).
      Specified by:
      endAnnotation in interface AnnotatedOutput
    • getAnnotationWidth

      public int getAnnotationWidth()
      Get the maximum width of the annotated output. This is advisory: Implementations of this interface are encouraged to deal with too-wide output, but annotaters are encouraged to attempt to avoid exceeding the indicated width.
      Specified by:
      getAnnotationWidth in interface AnnotatedOutput
      Returns:
      >= 1; the maximum width
    • enableAnnotations

      public void enableAnnotations(int annotationWidth, boolean verbose)
      Indicates that this instance should keep annotations. This method may be called only once per instance, and only before any data has been written to the it.
      Parameters:
      annotationWidth - >= 40; the desired maximum annotation width
      verbose - whether or not to indicate verbose annotations
    • finishAnnotating

      public void finishAnnotating()
      Finishes up annotation processing. This closes off any open annotations and removes annotations that don't refer to written data.
    • writeAnnotationsTo

      public void writeAnnotationsTo(Writer out) throws IOException
      Writes the annotated content of this instance to the given writer.
      Parameters:
      out - non-null; where to write to
      Throws:
      IOException