1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 net
.multiphasicapps
.classfile
;
12 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
13 import java
.io
.DataInputStream
;
14 import java
.io
.IOException
;
15 import java
.util
.Arrays
;
16 import java
.util
.LinkedHashMap
;
20 * This represents the attribute table that exists within a class.
24 public final class AttributeTable
26 /** The attribute table. */
27 private final Map
<String
, Attribute
> _attributes
;
30 * Initializes the attribute table.
32 * @param __a The input attributes.
33 * @throws NullPointerException On null arguments.
36 public AttributeTable(Attribute
... __a
)
37 throws NullPointerException
39 this(Arrays
.<Attribute
>asList((__a
!= null ? __a
:
44 * Initializes the attribute table.
46 * @param __a The input attributes.
47 * @throws NullPointerException On null arguments.
50 public AttributeTable(Iterable
<Attribute
> __a
)
51 throws NullPointerException
54 throw new NullPointerException("NARG");
56 Map
<String
, Attribute
> attributes
= new LinkedHashMap
<>();
57 for (Attribute a
: __a
)
60 throw new NullPointerException("NARG");
63 if (!attributes
.containsKey(n
))
66 this._attributes
= attributes
;
74 public final boolean equals(Object __o
)
76 throw Debugging
.todo();
80 * Returns the attribute that uses the specified name.
82 * @param __n The name of the attribute to get.
83 * @return The specified attribute or {@code null} if it does not exist.
84 * @throws NullPointerException On null arguments.
87 public final Attribute
get(String __n
)
88 throws NullPointerException
91 throw new NullPointerException("NARG");
93 return this._attributes
.get(__n
);
101 public final int hashCode()
103 throw Debugging
.todo();
107 * Opens the attribute by the specified key.
109 * @param __n The name of the attribute to open.
110 * @return The stream to the given attribute or {@code null} if it is not
112 * @throws NullPointerException On null arguments.
115 public final DataInputStream
open(String __n
)
116 throws NullPointerException
119 throw new NullPointerException("NARG");
121 Attribute a
= this.get(__n
);
132 public final String
toString()
134 throw Debugging
.todo();
138 * Returns the length of the attribute table.
140 * @return The attribute table length.
143 public final int size()
145 return this._attributes
.size();
149 * Parses the attribute table.
151 * @param __in The input stream.
152 * @param __pool The constant pool.
153 * @return The attribute table.
154 * @throws InvalidClassFormatException If the table is not correct.
155 * @throws IOException On read errors.
156 * @throws NullPointerException On null arguments.
159 public static AttributeTable
parse(Pool __pool
, DataInputStream __in
)
160 throws InvalidClassFormatException
, IOException
, NullPointerException
162 if (__in
== null || __pool
== null)
163 throw new NullPointerException("NARG");
165 int n
= __in
.readUnsignedShort();
166 Attribute
[] rv
= new Attribute
[n
];
169 for (int i
= 0; i
< n
; i
++)
171 String name
= __pool
.<UTFConstantEntry
>require(
172 UTFConstantEntry
.class, __in
.readUnsignedShort()).toString();
174 // {@squirreljme.error JC1x Attributes with a size larger than two
175 // gigabytes are not supported.}
176 int len
= __in
.readInt();
178 throw new InvalidClassFormatException("JC1x");
180 byte[] data
= new byte[len
];
181 __in
.readFully(data
);
183 rv
[i
] = new Attribute(name
, data
, 0, len
);
186 return new AttributeTable(rv
);