private ByteArrayAnnotatedOutput toDex0(boolean annotate, boolean verbose, Storage storage) {
    /*
     * 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();
    if (dexOptions.apiIsSupported(DexFormat.API_METHOD_HANDLES)) {
      // Prepare call site ids before byteData where the call site items are placed.
      callSiteIds.prepare();
    }
    byteData.prepare();
    if (dexOptions.apiIsSupported(DexFormat.API_METHOD_HANDLES)) {
      // Prepare method handles after call site items placed in byteData.
      methodHandles.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];
      if ((one == callSiteIds || one == methodHandles) && one.items().isEmpty()) {
        /*
         * Skip call site or method handles sections if they have no items as
         * they may change the alignment of what follows even if empty as
         * Section.setFileOffset() always ensures appropriate alignment for the section.
         */
        continue;
      }

      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 = storage == null ? new byte[fileSize] : storage.getStorage(fileSize);

    ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput(barr);

    if (annotate) {
      out.enableAnnotations(dumpWidth, verbose);
    }

    for (int i = 0; i < count; i++) {
      try {
        final Section one = sections[i];
        if ((one == callSiteIds || one == methodHandles) && one.items().isEmpty()) {
          continue;
        }
        final int zeroCount = one.getFileOffset() - out.getCursor();
        if (zeroCount < 0) {
          throw new ExceptionWithContext("excess write of " + (-zeroCount));
        }
        out.writeZeroes(zeroCount);
        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, out.getCursor());
    calcChecksum(barr, out.getCursor());

    if (annotate) {
      wordData.writeIndexAnnotation(out, ItemType.TYPE_CODE_ITEM, "\nmethod code index:\n\n");
      getStatistics().writeAnnotation(out);
      out.finishAnnotating();
    }

    return out;
  }
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
android/android_framework/dex-maker/src/main/java/com/android/dx/dex/file/DexFile.java