FileDocCategorySizeDatePackage
Section.javaAPI DocAndroid 1.5 API8558Wed May 06 22:41:02 BST 2009com.android.dx.dex.file

Section

public abstract class Section extends Object
A section of a .dex file. Each section consists of a list of items of some sort or other.

Fields Summary
private final String
name
null-ok; name of this part, for annotation purposes
private final DexFile
file
non-null; file that this instance is part of
private final int
alignment
> 0; alignment requirement for the final output; must be a power of 2
private int
fileOffset
>= -1; offset from the start of the file to this part, or -1 if not yet known
private boolean
prepared
whether {@link #prepare} has been called successfully on this instance
Constructors Summary
public Section(String name, DexFile file, int alignment)
Constructs an instance. The file offset is initially unknown.

param
name null-ok; the name of this instance, for annotation purposes
param
file non-null; file that this instance is part of
param
alignment > 0; alignment requirement for the final output; must be a power of 2

        if (file == null) {
            throw new NullPointerException("file == null");
        }

        validateAlignment(alignment);

        this.name = name;
        this.file = file;
        this.alignment = alignment;
        this.fileOffset = -1;
        this.prepared = false;
    
Methods Summary
protected final voidalign(com.android.dx.util.AnnotatedOutput out)
Aligns the output of the given data to the alignment of this instance.

param
out non-null; the output to align

        out.alignTo(alignment);
    
public abstract intgetAbsoluteItemOffset(Item item)
Returns the absolute file offset of the given item which must be contained in this section. This is only valid to call once this instance has been assigned a file offset (via {@link #setFileOffset}).

Note: Subclasses must implement this as appropriate for their contents.

param
item non-null; the item in question
return
>= 0; the item's absolute file offset

public final intgetAbsoluteOffset(int relative)
Returns the absolute file offset, given an offset from the start of this instance's output. This is only valid to call once this instance has been assigned a file offset (via {@link #setFileOffset}).

param
relative >= 0; the relative offset
return
>= 0; the corresponding absolute file offset

        if (relative < 0) {
            throw new IllegalArgumentException("relative < 0");
        }

        if (fileOffset < 0) {
            throw new RuntimeException("fileOffset not yet set");
        }

        return fileOffset + relative;
    
public final intgetAlignment()
Gets the alignment for this instance's final output.

return
> 0; the alignment

        return alignment;
    
public final DexFilegetFile()
Gets the file that this instance is part of.

return
non-null; the file

        return file;
    
public final intgetFileOffset()
Gets the offset from the start of the file to this part. This throws an exception if the offset has not yet been set.

return
>= 0; the file offset

        if (fileOffset < 0) {
            throw new RuntimeException("fileOffset not set");
        }

        return fileOffset;
    
protected final java.lang.StringgetName()
Returns the name of this section, for annotation purposes.

return
null-ok; name of this part, for annotation purposes

        return name;
    
public abstract java.util.Collectionitems()
Gets the collection of all the items in this section. It is not valid to attempt to change the returned list.

return
non-null; the items

public final voidprepare()
Prepares this instance for writing. This performs any necessary prerequisites, including particularly adding stuff to other sections. This method may only be called once per instance; subsequent calls will throw an exception.

        throwIfPrepared();
        prepare0();
        prepared = true;
    
protected abstract voidprepare0()
Does the main work of {@link #prepare}.

public final intsetFileOffset(int fileOffset)
Sets the file offset. It is only valid to call this method once once per instance.

param
fileOffset >= 0; the desired offset from the start of the file where this for this instance
return
>= 0; the offset that this instance should be placed at in order to meet its alignment constraint

        if (fileOffset < 0) {
            throw new IllegalArgumentException("fileOffset < 0");
        }

        if (this.fileOffset >= 0) {
            throw new RuntimeException("fileOffset already set");
        }

        int mask = alignment - 1;
        fileOffset = (fileOffset + mask) & ~mask;

        this.fileOffset = fileOffset;

        return fileOffset;
    
protected final voidthrowIfNotPrepared()
Throws an exception if {@link #prepare} has not been called on this instance.

        if (!prepared) {
            throw new RuntimeException("not prepared");
        }
    
protected final voidthrowIfPrepared()
Throws an exception if {@link #prepare} has already been called on this instance.

        if (prepared) {
            throw new RuntimeException("already prepared");
        }
    
public static voidvalidateAlignment(int alignment)
Validates an alignment.

param
alignment the alignment
throws
IllegalArgumentException thrown if alignment isn't a positive power of 2

        if ((alignment <= 0) ||
            (alignment & (alignment - 1)) != 0) {
            throw new IllegalArgumentException("invalid alignment");
        }
    
public abstract intwriteSize()
Gets the size of this instance when output, in bytes.

return
>= 0; the size of this instance, in bytes

public final voidwriteTo(com.android.dx.util.AnnotatedOutput out)
Writes this instance to the given raw data object.

param
out non-null; where to write to

        throwIfNotPrepared();        
        align(out);

        int cursor = out.getCursor();

        if (fileOffset < 0) {
            fileOffset = cursor;
        } else if (fileOffset != cursor) {
            throw new RuntimeException("alignment mismatch: for " + this +
                                       ", at " + cursor +
                                       ", but expected " + fileOffset);
        }

        if (out.annotates()) {
            if (name != null) {
                out.annotate(0, "\n" + name + ":");
            } else if (cursor != 0) {
                out.annotate(0, "\n");
            }
        }

        writeTo0(out);
    
protected abstract voidwriteTo0(com.android.dx.util.AnnotatedOutput out)
Writes this instance to the given raw data object. This gets called by {@link #writeTo} after aligning the cursor of out and verifying that either the assigned file offset matches the actual cursor out or that the file offset was not previously assigned, in which case it gets assigned to out's cursor.

param
out non-null; where to write to