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