Esempio n. 1
0
 private void writeStringTable(PackedDataOutputStream stream) throws IOException {
   stream.writePackedU32(pool.size());
   Iterator<String> iter = pool.iterator();
   while (iter.hasNext()) {
     String string = iter.next();
     stream.writeUTF(string);
   }
 }
Esempio n. 2
0
  /**
   * Writes the specified index to the associated output stream. This may be called multiple times
   * in order to write multiple indexes.
   *
   * @param index the index to write to the stream
   * @return the number of bytes written to the stream
   * @throws IOException if any i/o error occurs
   */
  public int write(Index index) throws IOException {
    PackedDataOutputStream stream = new PackedDataOutputStream(new BufferedOutputStream(out));
    stream.writeInt(MAGIC);
    stream.writeByte(VERSION);

    buildTables(index);
    writeClassTable(stream);
    writeStringTable(stream);
    writeClasses(stream, index);
    stream.flush();
    return stream.size();
  }
Esempio n. 3
0
  private void writeClassTable(PackedDataOutputStream stream) throws IOException {
    stream.writePackedU32(classTable.size());

    // Zero is reserved for null
    int pos = 1;
    for (Map.Entry<DotName, Integer> entry : classTable.entrySet()) {
      entry.setValue(pos++);
      DotName name = entry.getKey();
      assert !name.isComponentized();

      int nameDepth = 0;
      for (DotName prefix = name.prefix(); prefix != null; prefix = prefix.prefix()) nameDepth++;

      stream.writePackedU32(nameDepth);
      stream.writeUTF(name.local());
    }
  }
Esempio n. 4
0
 private void writeType(PackedDataOutputStream stream, Type type) throws IOException {
   stream.writeByte(type.kind().ordinal());
   stream.writePackedU32(positionOf(type.name()));
 }
Esempio n. 5
0
  private void writeClasses(PackedDataOutputStream stream, Index index) throws IOException {
    Collection<ClassInfo> classes = index.getKnownClasses();
    stream.writePackedU32(classes.size());
    for (ClassInfo clazz : classes) {
      stream.writePackedU32(positionOf(clazz.name()));
      stream.writePackedU32(clazz.superName() == null ? 0 : positionOf(clazz.superName()));
      stream.writeShort(clazz.flags());
      DotName[] interfaces = clazz.interfaces();
      stream.writePackedU32(interfaces.length);
      for (DotName intf : interfaces) stream.writePackedU32(positionOf(intf));

      Set<Entry<DotName, List<AnnotationTarget>>> entrySet = clazz.annotations().entrySet();
      stream.writePackedU32(entrySet.size());
      for (Entry<DotName, List<AnnotationTarget>> entry : entrySet) {
        stream.writePackedU32(positionOf(entry.getKey()));

        List<AnnotationTarget> targets = entry.getValue();
        stream.writePackedU32(targets.size());
        for (AnnotationTarget target : targets) {
          if (target instanceof FieldInfo) {
            FieldInfo field = (FieldInfo) target;
            stream.writeByte(FIELD_TAG);
            stream.writePackedU32(positionOf(field.name()));
            writeType(stream, field.type());
            stream.writeShort(field.flags());
          } else if (target instanceof MethodInfo) {
            MethodInfo method = (MethodInfo) target;
            stream.writeByte(METHOD_TAG);
            stream.writePackedU32(positionOf(method.name()));
            stream.writePackedU32(method.args().length);
            for (int i = 0; i < method.args().length; i++) {
              writeType(stream, method.args()[i]);
            }
            writeType(stream, method.returnType());
            stream.writeShort(method.flags());
          } else if (target instanceof MethodParameterInfo) {
            MethodParameterInfo param = (MethodParameterInfo) target;
            MethodInfo method = param.method();
            stream.writeByte(METHOD_PARAMATER_TAG);
            stream.writePackedU32(positionOf(method.name()));
            stream.writePackedU32(method.args().length);
            for (int i = 0; i < method.args().length; i++) {
              writeType(stream, method.args()[i]);
            }
            writeType(stream, method.returnType());
            stream.writeShort(method.flags());
            stream.writePackedU32(param.position());
          } else if (target instanceof ClassInfo) {
            stream.writeByte(CLASS_TAG);
          } else throw new IllegalStateException("Unknown target");
        }
      }
    }
  }