Make it so mapping files are used and then reapplied.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / jvm / pack / HeaderStruct.java
blobcdffaf38da4366a91ff6118bf3c22bc52c0279d5
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.jvm.pack;
12 import cc.squirreljme.jvm.pack.constants.ClassInfoConstants;
13 import cc.squirreljme.runtime.cldc.annotation.Exported;
14 import java.io.DataInput;
15 import java.io.IOException;
17 /**
18 * Common header structure that is shared for all ROM formats.
20 * @since 2021/04/07
22 @Exported
23 public final class HeaderStruct
25 /** The ROM Magic Number. */
26 protected final int magicNumber;
28 /** The format version. */
29 protected final int formatVersion;
31 /** The properties used. */
32 private final int[] _properties;
34 /**
35 * Initializes the header structure.
37 * @param __romMagic The ROM Magic number.
38 * @param __formatVersion The format version.
39 * @param __properties The properties used.
40 * @throws NullPointerException On null arguments.
41 * @since 2021/04/07
43 private HeaderStruct(int __romMagic, int __formatVersion,
44 int[] __properties)
45 throws NullPointerException
47 if (__properties == null)
48 throw new NullPointerException("NARG");
50 this.magicNumber = __romMagic;
51 this.formatVersion = __formatVersion;
52 this._properties = __properties;
55 /**
56 * Obtains the given property.
58 * @param __prop The property to get.
59 * @return The value of the given property.
60 * @throws IndexOutOfBoundsException If the property is not within bounds.
61 * @since 2021/04/07
63 @Exported
64 public int getInteger(int __prop)
65 throws IndexOutOfBoundsException
67 // {@squirreljme.error ZZ55 Invalid property. (The property)}
68 int[] properties = this._properties;
69 if (__prop < 0 || __prop >= properties.length)
70 throw new IndexOutOfBoundsException("ZZ55 " + __prop);
72 return properties[__prop];
75 /**
76 * Gets the long valued property.
78 * @param __prop The property to get, the low value is first while the
79 * high value is the next property.
80 * @return The long value.
81 * @throws IndexOutOfBoundsException If the property is not within bounds.
82 * @since 2021/07/13
84 @Exported
85 @SuppressWarnings("MagicNumber")
86 public long getLong(int __prop)
87 throws IndexOutOfBoundsException
89 return (this.getInteger(__prop) & 0xFFFFFFFFL) |
90 ((long)this.getInteger(__prop) << 32);
93 /**
94 * Returns the magic number of the structure.
96 * @return The structure's magic number.
97 * @since 2021/07/11
99 @Exported
100 public int magicNumber()
102 return this.magicNumber;
106 * Returns the number of properties.
108 * @return The number of properties.
109 * @since 2021/05/16
111 @Exported
112 public final int numProperties()
114 return this._properties.length;
118 * Decodes the header.
120 * @param __in The input to decode from.
121 * @param __maxProps The maximum number of valid properties.
122 * @return The header structure.
123 * @throws IOException On read errors.
124 * @since 2021/04/07
126 @Exported
127 public static HeaderStruct decode(DataInput __in, int __maxProps)
128 throws IOException
130 // Read the magic number
131 int romMagic = __in.readInt();
133 // Read the format version
134 int formatVersion = __in.readUnsignedShort();
136 // {@squirreljme.error ZZ44 Cannot decode header because the
137 // version identifier is not known. (The format version of the header
138 // file)}
139 if (formatVersion != ClassInfoConstants.CLASS_VERSION_20201129)
140 throw new InvalidRomException("ZZ44 " + formatVersion);
142 // Read in all the data
143 int numProperties = Math.min(__in.readUnsignedShort(), __maxProps);
144 int[] properties = new int[numProperties];
145 for (int i = 0; i < numProperties; i++)
146 properties[i] = __in.readInt();
148 return new HeaderStruct(romMagic, formatVersion, properties);