Java Code Examples for org.jf.dexlib2.writer.io.DeferredOutputStream#writeTo()

The following examples show how to use org.jf.dexlib2.writer.io.DeferredOutputStream#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 2
Source File: DexWriter.java    From zjdroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 3
Source File: DexWriter.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}
 
Example 4
Source File: DexWriter.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
private void writeDebugAndCodeItems(@Nonnull DexDataWriter offsetWriter,
                                    @Nonnull DeferredOutputStream temp) throws IOException {
    ByteArrayOutputStream ehBuf = new ByteArrayOutputStream();
    debugSectionOffset = offsetWriter.getPosition();
    DebugWriter<StringKey, TypeKey> debugWriter =
            new DebugWriter<StringKey, TypeKey>(stringSection, typeSection, offsetWriter);

    DexDataWriter codeWriter = new DexDataWriter(temp, 0);

    List<CodeItemOffset<MethodKey>> codeOffsets = Lists.newArrayList();

    for (ClassKey classKey: classSection.getSortedClasses()) {
        Collection<? extends MethodKey> directMethods = classSection.getSortedDirectMethods(classKey);
        Collection<? extends MethodKey> virtualMethods = classSection.getSortedVirtualMethods(classKey);

        Iterable<MethodKey> methods = Iterables.concat(directMethods, virtualMethods);

        for (MethodKey methodKey: methods) {
            List<? extends TryBlock<? extends ExceptionHandler>> tryBlocks =
                    classSection.getTryBlocks(methodKey);
            Iterable<? extends Instruction> instructions = classSection.getInstructions(methodKey);
            Iterable<? extends DebugItem> debugItems = classSection.getDebugItems(methodKey);

            if (instructions != null && stringSection.hasJumboIndexes()) {
                boolean needsFix = false;
                for (Instruction instruction: instructions) {
                    if (instruction.getOpcode() == Opcode.CONST_STRING) {
                        if (stringSection.getItemIndex(
                                (StringRef)((ReferenceInstruction)instruction).getReference()) >= 65536) {
                            needsFix = true;
                            break;
                        }
                    }
                }

                if (needsFix) {
                    MutableMethodImplementation mutableMethodImplementation =
                            classSection.makeMutableMethodImplementation(methodKey);
                    fixInstructions(mutableMethodImplementation);

                    instructions = mutableMethodImplementation.getInstructions();
                    tryBlocks = mutableMethodImplementation.getTryBlocks();
                    debugItems = mutableMethodImplementation.getDebugItems();
                }
            }

            int debugItemOffset = writeDebugItem(offsetWriter, debugWriter,
                    classSection.getParameterNames(methodKey), debugItems);
            int codeItemOffset = writeCodeItem(codeWriter, ehBuf, methodKey, tryBlocks, instructions, debugItemOffset);

            if (codeItemOffset != -1) {
                codeOffsets.add(new CodeItemOffset<MethodKey>(methodKey, codeItemOffset));
            }
        }
    }

    offsetWriter.align();
    codeSectionOffset = offsetWriter.getPosition();

    codeWriter.close();
    temp.writeTo(offsetWriter);
    temp.close();

    for (CodeItemOffset<MethodKey> codeOffset: codeOffsets) {
        classSection.setCodeItemOffset(codeOffset.method, codeSectionOffset + codeOffset.codeOffset);
    }
}