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();
  }
Encodes the handler lists.

@param file {@code non-null;} file this instance is part of
android/android_framework/dex-maker/src/main/java/com/android/dx/dex/file/CatchStructs.java