001: /*
002: * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.java;
027:
028: import java.io.IOException;
029: import java.io.DataInputStream;
030: import java.io.DataOutputStream;
031:
032: /**
033: * This class is used to represent an attribute from a binary class.
034: * This class should go away once arrays are objects.
035: *
036: * WARNING: The contents of this source file are not part of any
037: * supported API. Code that depends on them does so at its own risk:
038: * they are subject to change or removal without notice.
039: */
040: public final class BinaryAttribute implements Constants {
041: Identifier name;
042: byte data[];
043: BinaryAttribute next;
044:
045: /**
046: * Constructor
047: */
048: BinaryAttribute(Identifier name, byte data[], BinaryAttribute next) {
049: this .name = name;
050: this .data = data;
051: this .next = next;
052: }
053:
054: /**
055: * Load a list of attributes
056: */
057: public static BinaryAttribute load(DataInputStream in,
058: BinaryConstantPool cpool, int mask) throws IOException {
059: BinaryAttribute atts = null;
060: int natt = in.readUnsignedShort(); // JVM 4.6 method_info.attrutes_count
061:
062: for (int i = 0; i < natt; i++) {
063: // id from JVM 4.7 attribute_info.attribute_name_index
064: Identifier id = cpool.getIdentifier(in.readUnsignedShort());
065: // id from JVM 4.7 attribute_info.attribute_length
066: int len = in.readInt();
067:
068: if (id.equals(idCode) && ((mask & ATT_CODE) == 0)) {
069: in.skipBytes(len);
070: } else {
071: byte data[] = new byte[len];
072: in.readFully(data);
073: atts = new BinaryAttribute(id, data, atts);
074: }
075: }
076: return atts;
077: }
078:
079: // write out the Binary attributes to the given stream
080: // (note that attributes may be null)
081: static void write(BinaryAttribute attributes, DataOutputStream out,
082: BinaryConstantPool cpool, Environment env)
083: throws IOException {
084: // count the number of attributes
085: int attributeCount = 0;
086: for (BinaryAttribute att = attributes; att != null; att = att.next)
087: attributeCount++;
088: out.writeShort(attributeCount);
089:
090: // write out each attribute
091: for (BinaryAttribute att = attributes; att != null; att = att.next) {
092: Identifier name = att.name;
093: byte data[] = att.data;
094: // write the identifier
095: out.writeShort(cpool.indexString(name.toString(), env));
096: // write the length
097: out.writeInt(data.length);
098: // write the data
099: out.write(data, 0, data.length);
100: }
101: }
102:
103: /**
104: * Accessors
105: */
106:
107: public Identifier getName() {
108: return name;
109: }
110:
111: public byte getData()[] {
112: return data;
113: }
114:
115: public BinaryAttribute getNextAttribute() {
116: return next;
117: }
118:
119: }
|