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 public int major; 028 public int minor; 029 private final int magic = 0xCAFEBABE; 030 public ClassConstantPool pool = new ClassConstantPool(); 031 public int accessFlags; 032 public int thisClass; 033 public int superClass; 034 public int[] interfaces; 035 public ClassFileEntry[] fields; 036 public ClassFileEntry[] methods; 037 public Attribute[] attributes; 038 039 public void write(final DataOutputStream dos) throws IOException { 040 dos.writeInt(magic); 041 dos.writeShort(minor); 042 dos.writeShort(major); 043 dos.writeShort(pool.size() + 1); 044 for (int i = 1; i <= pool.size(); i++) { 045 ConstantPoolEntry entry; 046 (entry = (ConstantPoolEntry) pool.get(i)).doWrite(dos); 047 // Doubles and longs take up two spaces in the pool, but only one 048 // gets written 049 if (entry.getTag() == ConstantPoolEntry.CP_Double || entry.getTag() == ConstantPoolEntry.CP_Long) { 050 i++; 051 } 052 } 053 dos.writeShort(accessFlags); 054 dos.writeShort(thisClass); 055 dos.writeShort(superClass); 056 dos.writeShort(interfaces.length); 057 for (int i = 0; i < interfaces.length; i++) { 058 dos.writeShort(interfaces[i]); 059 } 060 dos.writeShort(fields.length); 061 for (int i = 0; i < fields.length; i++) { 062 fields[i].write(dos); 063 } 064 dos.writeShort(methods.length); 065 for (int i = 0; i < methods.length; i++) { 066 methods[i].write(dos); 067 } 068 dos.writeShort(attributes.length); 069 for (int i = 0; i < attributes.length; i++) { 070 attributes[i].write(dos); 071 } 072 } 073}