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"); } } } }
/** * 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(); }
private void writeType(PackedDataOutputStream stream, Type type) throws IOException { stream.writeByte(type.kind().ordinal()); stream.writePackedU32(positionOf(type.name())); }