001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package components;
028:
029: import java.io.DataOutput;
030: import java.io.DataInput;
031: import java.io.IOException;
032: import util.*;
033:
034: /*
035: * A class to represent the general Attribute form
036: * in its most uninterpreted form. "data" is just an
037: * array of byte. It can also be other things, even
038: * array of ConstantObject.
039: */
040:
041: public class UninterpretedAttribute extends Attribute {
042: public byte data[];
043:
044: public UninterpretedAttribute(UnicodeConstant name, int l, byte d[]) {
045: super (name, l);
046: data = d;
047: }
048:
049: protected int writeData(DataOutput o) throws IOException {
050: int trueLength = 0;
051: o.write(data, 0, data.length);
052: return data.length;
053: }
054:
055: /*
056: * For manipulating byte data
057: */
058: public int getInt(int w) {
059: return ((int) data[w] << 24)
060: | (((int) data[w + 1] & 0xff) << 16)
061: | (((int) data[w + 2] & 0xff) << 8)
062: | ((int) data[w + 3] & 0xff);
063: }
064:
065: public int getUnsignedShort(int w) {
066: return (((int) data[w] & 0xff) << 8)
067: | ((int) data[w + 1] & 0xff);
068: }
069:
070: private void putShort(int w, short v) {
071: data[w] = (byte) (v >>> 8);
072: data[w + 1] = (byte) v;
073: }
074:
075: public static UninterpretedAttribute readAttribute(DataInput i,
076: ConstantObject globals[]) throws IOException {
077: UnicodeConstant name;
078: int l;
079: byte d[];
080:
081: name = (UnicodeConstant) globals[i.readUnsignedShort()];
082: l = i.readInt();
083: d = new byte[l];
084: i.readFully(d);
085: return new UninterpretedAttribute(name, l, d);
086: }
087:
088: //
089: // for those cases where we already read the name index
090: // and know that its not something requiring special handling.
091: //
092: public static Attribute finishReadAttribute(DataInput i,
093: UnicodeConstant name) throws IOException {
094: int l;
095: byte d[];
096:
097: l = i.readInt();
098: d = new byte[l];
099: i.readFully(d);
100: return new UninterpretedAttribute(name, l, d);
101: }
102:
103: public static Attribute[] readAttributes(DataInput i,
104: ConstantObject t[], boolean verbose) throws IOException {
105: int nattr = i.readUnsignedShort();
106: if (verbose) {
107: System.out.println(Localizer.getString(
108: "uninterpretedattribute.reading_attributes",
109: Integer.toString(nattr)));
110: }
111: if (nattr == 0)
112: return null;
113: Attribute a[] = new Attribute[nattr];
114: for (int j = 0; j < nattr; j++) {
115: a[j] = readAttribute(i, t);
116: }
117: return a;
118: }
119:
120: }
|