Search in sources :

Example 1 with ByteArrayAnnotatedOutput

use of com.android.dx.util.ByteArrayAnnotatedOutput in project buck by facebook.

the class CatchStructs method encode.

/**
     * Encodes the handler lists.
     *
     * @param file {@code non-null;} file this instance is part of
     */
public void encode(DexFile file) {
    finishProcessingIfNecessary();
    TypeIdsSection typeIds = file.getTypeIds();
    int size = table.size();
    handlerOffsets = new TreeMap<CatchHandlerList, Integer>();
    /*
         * First add a map entry for each unique list. The tree structure
         * will ensure they are sorted when we reiterate later.
         */
    for (int i = 0; i < size; i++) {
        handlerOffsets.put(table.get(i).getHandlers(), null);
    }
    if (handlerOffsets.size() > 65535) {
        throw new UnsupportedOperationException("too many catch handlers");
    }
    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
    // Write out the handlers "header" consisting of its size in entries.
    encodedHandlerHeaderSize = out.writeUleb128(handlerOffsets.size());
    // Now write the lists out in order, noting the offset of each.
    for (Map.Entry<CatchHandlerList, Integer> mapping : handlerOffsets.entrySet()) {
        CatchHandlerList list = mapping.getKey();
        int listSize = list.size();
        boolean catchesAll = list.catchesAll();
        // Set the offset before we do any writing.
        mapping.setValue(out.getCursor());
        if (catchesAll) {
            // A size <= 0 means that the list ends with a catch-all.
            out.writeSleb128(-(listSize - 1));
            listSize--;
        } else {
            out.writeSleb128(listSize);
        }
        for (int i = 0; i < listSize; i++) {
            CatchHandlerList.Entry entry = list.get(i);
            out.writeUleb128(typeIds.indexOf(entry.getExceptionType()));
            out.writeUleb128(entry.getHandler());
        }
        if (catchesAll) {
            out.writeUleb128(list.get(listSize).getHandler());
        }
    }
    encodedHandlers = out.toByteArray();
}
Also used : CatchHandlerList(com.android.dx.dex.code.CatchHandlerList) ByteArrayAnnotatedOutput(com.android.dx.util.ByteArrayAnnotatedOutput) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 2 with ByteArrayAnnotatedOutput

use of com.android.dx.util.ByteArrayAnnotatedOutput in project buck by facebook.

the class ClassDataItem method place0.

/** {@inheritDoc} */
@Override
protected void place0(Section addedTo, int offset) {
    // Encode the data and note the size.
    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
    encodeOutput(addedTo.getFile(), out);
    encodedForm = out.toByteArray();
    setWriteSize(encodedForm.length);
}
Also used : ByteArrayAnnotatedOutput(com.android.dx.util.ByteArrayAnnotatedOutput)

Example 3 with ByteArrayAnnotatedOutput

use of com.android.dx.util.ByteArrayAnnotatedOutput in project buck by facebook.

the class DexFile method toDex0.

/**
     * Returns the contents of this instance as a {@code .dex} file,
     * in a {@link ByteArrayAnnotatedOutput} instance.
     *
     * @param annotate whether or not to keep annotations
     * @param verbose if annotating, whether to be verbose
     * @return {@code non-null;} a {@code .dex} file for this instance
     */
private ByteArrayAnnotatedOutput toDex0(boolean annotate, boolean verbose) {
    /*
         * The following is ordered so that the prepare() calls which
         * add items happen before the calls to the sections that get
         * added to.
         */
    classDefs.prepare();
    classData.prepare();
    wordData.prepare();
    byteData.prepare();
    methodIds.prepare();
    fieldIds.prepare();
    protoIds.prepare();
    typeLists.prepare();
    typeIds.prepare();
    stringIds.prepare();
    stringData.prepare();
    header.prepare();
    // Place the sections within the file.
    int count = sections.length;
    int offset = 0;
    for (int i = 0; i < count; i++) {
        Section one = sections[i];
        int placedAt = one.setFileOffset(offset);
        if (placedAt < offset) {
            throw new RuntimeException("bogus placement for section " + i);
        }
        try {
            if (one == map) {
                /*
                     * Inform the map of all the sections, and add it
                     * to the file. This can only be done after all
                     * the other items have been sorted and placed.
                     */
                MapItem.addMap(sections, map);
                map.prepare();
            }
            if (one instanceof MixedItemSection) {
                /*
                     * Place the items of a MixedItemSection that just
                     * got placed.
                     */
                ((MixedItemSection) one).placeItems();
            }
            offset = placedAt + one.writeSize();
        } catch (RuntimeException ex) {
            throw ExceptionWithContext.withContext(ex, "...while writing section " + i);
        }
    }
    // Write out all the sections.
    fileSize = offset;
    byte[] barr = new byte[fileSize];
    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(barr);
    if (annotate) {
        out.enableAnnotations(dumpWidth, verbose);
    }
    for (int i = 0; i < count; i++) {
        try {
            Section one = sections[i];
            int zeroCount = one.getFileOffset() - out.getCursor();
            if (zeroCount < 0) {
                throw new ExceptionWithContext("excess write of " + (-zeroCount));
            }
            out.writeZeroes(one.getFileOffset() - out.getCursor());
            one.writeTo(out);
        } catch (RuntimeException ex) {
            ExceptionWithContext ec;
            if (ex instanceof ExceptionWithContext) {
                ec = (ExceptionWithContext) ex;
            } else {
                ec = new ExceptionWithContext(ex);
            }
            ec.addContext("...while writing section " + i);
            throw ec;
        }
    }
    if (out.getCursor() != fileSize) {
        throw new RuntimeException("foreshortened write");
    }
    // Perform final bookkeeping.
    calcSignature(barr);
    calcChecksum(barr);
    if (annotate) {
        wordData.writeIndexAnnotation(out, ItemType.TYPE_CODE_ITEM, "\nmethod code index:\n\n");
        getStatistics().writeAnnotation(out);
        out.finishAnnotating();
    }
    return out;
}
Also used : ExceptionWithContext(com.android.dex.util.ExceptionWithContext) ByteArrayAnnotatedOutput(com.android.dx.util.ByteArrayAnnotatedOutput)

Example 4 with ByteArrayAnnotatedOutput

use of com.android.dx.util.ByteArrayAnnotatedOutput in project buck by facebook.

the class DexFile method writeTo.

/**
     * Writes the contents of this instance as either a binary or a
     * human-readable form, or both.
     *
     * @param out {@code null-ok;} where to write to
     * @param humanOut {@code null-ok;} where to write human-oriented output to
     * @param verbose whether to be verbose when writing human-oriented output
     */
public void writeTo(OutputStream out, Writer humanOut, boolean verbose) throws IOException {
    boolean annotate = (humanOut != null);
    ByteArrayAnnotatedOutput result = toDex0(annotate, verbose);
    if (out != null) {
        out.write(result.getArray());
    }
    if (annotate) {
        result.writeAnnotationsTo(humanOut);
    }
}
Also used : ByteArrayAnnotatedOutput(com.android.dx.util.ByteArrayAnnotatedOutput)

Example 5 with ByteArrayAnnotatedOutput

use of com.android.dx.util.ByteArrayAnnotatedOutput in project buck by facebook.

the class IndexMap method adjustEncodedValue.

public EncodedValue adjustEncodedValue(EncodedValue encodedValue) {
    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(32);
    new EncodedValueTransformer(out).transform(new EncodedValueReader(encodedValue));
    return new EncodedValue(out.toByteArray());
}
Also used : EncodedValue(com.android.dex.EncodedValue) EncodedValueReader(com.android.dex.EncodedValueReader) ByteArrayAnnotatedOutput(com.android.dx.util.ByteArrayAnnotatedOutput)

Aggregations

ByteArrayAnnotatedOutput (com.android.dx.util.ByteArrayAnnotatedOutput)20 EncodedValue (com.android.dex.EncodedValue)6 EncodedValueReader (com.android.dex.EncodedValueReader)4 Annotation (com.android.dex.Annotation)2 ExceptionWithContext (com.android.dex.util.ExceptionWithContext)2 CatchHandlerList (com.android.dx.dex.code.CatchHandlerList)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2