    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021
    022/**
    023 * ClassFile is used to represent and write out Java class files.
    024 */
    025public class ClassFile {
    026
    027    private static final int MAGIC = 0xCAFEBABE;
    028
    029    public int major;
    030    public int minor;
    031    public ClassConstantPool pool = new ClassConstantPool();
    032    public int accessFlags;
    033    public int thisClass;
    034    public int superClass;
    035    public int[] interfaces;
    036    public ClassFileEntry[] fields;
    037    public ClassFileEntry[] methods;
    038    public Attribute[] attributes;
    039
    040    public void write(final DataOutputStream dos) throws IOException {
    041        dos.writeInt(MAGIC);
    042        dos.writeShort(minor);
    043        dos.writeShort(major);
    044        dos.writeShort(pool.size() + 1);
    045        for (int i = 1; i <= pool.size(); i++) {
    046            ConstantPoolEntry entry;
    047            (entry = (ConstantPoolEntry) pool.get(i)).doWrite(dos);
    048            // Doubles and longs take up two spaces in the pool, but only one
    049            // gets written
    050            if (entry.getTag() == ConstantPoolEntry.CP_Double || entry.getTag() == ConstantPoolEntry.CP_Long) {
    051                i++;
    052            }
    053        }
    054        dos.writeShort(accessFlags);
    055        dos.writeShort(thisClass);
    056        dos.writeShort(superClass);
    057        dos.writeShort(interfaces.length);
    058        for (final int element : interfaces) {
    059            dos.writeShort(element);
    060        }
    061        dos.writeShort(fields.length);
    062        for (final ClassFileEntry field : fields) {
    063            field.write(dos);
    064        }
    065        dos.writeShort(methods.length);
    066        for (final ClassFileEntry method : methods) {
    067            method.write(dos);
    068        }
    069        dos.writeShort(attributes.length);
    070        for (final Attribute attribute : attributes) {
    071            attribute.write(dos);
    072        }
    073    }
    074}