View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200.bytecode;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * ClassFile is used to represent and write out Java class files.
26   */
27  public class ClassFile {
28  
29      private static final int MAGIC = 0xCAFEBABE;
30  
31      public int major;
32      public int minor;
33      public ClassConstantPool pool = new ClassConstantPool();
34      public int accessFlags;
35      public int thisClass;
36      public int superClass;
37      public int[] interfaces;
38      public ClassFileEntry[] fields;
39      public ClassFileEntry[] methods;
40      public Attribute[] attributes;
41  
42      public void write(final DataOutputStream dos) throws IOException {
43          dos.writeInt(MAGIC);
44          dos.writeShort(minor);
45          dos.writeShort(major);
46          dos.writeShort(pool.size() + 1);
47          for (int i = 1; i <= pool.size(); i++) {
48              final ConstantPoolEntry entry;
49              (entry = (ConstantPoolEntry) pool.get(i)).doWrite(dos);
50              // Doubles and longs take up two spaces in the pool, but only one
51              // gets written
52              if (entry.getTag() == ConstantPoolEntry.CP_Double || entry.getTag() == ConstantPoolEntry.CP_Long) {
53                  i++;
54              }
55          }
56          dos.writeShort(accessFlags);
57          dos.writeShort(thisClass);
58          dos.writeShort(superClass);
59          dos.writeShort(interfaces.length);
60          for (final int element : interfaces) {
61              dos.writeShort(element);
62          }
63          dos.writeShort(fields.length);
64          for (final ClassFileEntry field : fields) {
65              field.write(dos);
66          }
67          dos.writeShort(methods.length);
68          for (final ClassFileEntry method : methods) {
69              method.write(dos);
70          }
71          dos.writeShort(attributes.length);
72          for (final Attribute attribute : attributes) {
73              attribute.write(dos);
74          }
75      }
76  }