1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 
  26 package jdk.tools.jaotc.binformat;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.DataOutputStream;
  30 import java.io.IOException;
  31 
  32 public final class HeaderContainer {
  33 
  34     private static final int CURRENT_VERSION = 1;
  35     private final ReadOnlyDataContainer container;
  36 
  37     // int _version;
  38     // int _class_count;
  39     // int _method_count;
  40     // int _klasses_got_size;
  41     // int _metadata_got_size;
  42     // int _oop_got_size;
  43     // int _jvm_version_offset;
  44 
  45     public HeaderContainer(String jvmVersion, ReadOnlyDataContainer container) {
  46         try {
  47             byte[] filler = new byte[4 * 7];
  48             container.appendBytes(filler);
  49 
  50             // Store JVM version string at the end of header section.
  51             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  52             DataOutputStream out = new DataOutputStream(bout);
  53             out.writeUTF(jvmVersion);
  54             out.writeShort(0); // Terminate by 0.
  55             byte[] b = bout.toByteArray();
  56             container.appendBytes(b, 0, b.length);
  57         } catch (IOException e) {
  58             throw new InternalError("Failed to append bytes to header section", e);
  59         }
  60 
  61         this.container = container;
  62         this.container.putIntAt(0 * 4, CURRENT_VERSION);
  63         this.container.putIntAt(6 * 4, 7 * 4); // JVM version string offset
  64     }
  65 
  66     public String getContainerName() {
  67         return container.getContainerName();
  68     }
  69 
  70     public ReadOnlyDataContainer getContainer() {
  71         return container;
  72     }
  73 
  74     public void setClassesCount(int count) {
  75         this.container.putIntAt(1 * 4, count);
  76     }
  77 
  78     public void setMethodsCount(int count) {
  79         this.container.putIntAt(2 * 4, count);
  80     }
  81 
  82     public void setKlassesGotSize(int size) {
  83         this.container.putIntAt(3 * 4, size);
  84     }
  85 
  86     public void setMetadataGotSize(int size) {
  87         this.container.putIntAt(4 * 4, size);
  88     }
  89 
  90     public void setOopGotSize(int size) {
  91         this.container.putIntAt(5 * 4, size);
  92     }
  93 
  94 }