/* * Created on Feb 25, 2005 */ package org.apache.bcel.classfile; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.Vector; import org.apache.bcel.Constants; /** * @author yann */ public class FreshAttribute extends Attribute { static final long serialVersionUID = 0; public static final int cellSize = 3; private short[][] offsets; public FreshAttribute(int name_index, int length, short[][] offsets, ConstantPool cp) { super(Constants.ATTR_FRESH_MAP,name_index,length,cp); this.offsets = offsets; } public FreshAttribute(int name_index, int length, DataInputStream str, ConstantPool cp) throws IOException { this(name_index, length, (short [][])null, cp); int nb = (length / cellSize); if(nb > 0) { Vector> vectors = new Vector>(); short offset; byte type; for(int i = 0; i < nb; i++) { offset = str.readShort(); type = str.readByte(); while(type >= vectors.size()) { vectors.add(new Vector()); } vectors.elementAt(type).add(new Short(offset)); } offsets = new short[vectors.size()][]; for(int i = 0; i < vectors.size(); i++) { offsets[i] = new short[vectors.elementAt(i).size()]; for(int j = 0; j < vectors.elementAt(i).size(); j++) { offsets[i][j] = vectors.elementAt(i).elementAt(j).shortValue(); } } } } public void accept(Visitor v) { v.visitFreshMap(this); } public Attribute copy(ConstantPool cp) { FreshAttribute copy = (FreshAttribute)clone(); System.arraycopy(offsets,0,copy.offsets,0,offsets.length); return copy; } public final void dump(DataOutputStream file) throws IOException { super.dump(file); // FIXME: sort the output ? for(int i = 0; i < offsets.length; i++) { for(int j = 0; j < offsets[i].length; j++) { file.writeShort(offsets[i][j]); file.writeByte(i%2); } } } public short[][] getOffsets() { return offsets; } public final String toString() { StringBuffer buf = new StringBuffer("FreshMap("); for(int i=0; i < offsets.length; i++) { buf.append("[ "); for(int j = 0; j < offsets[i].length; j++) { buf.append("" + offsets[i][j] + " "); } buf.append("]"); if(i < offsets.length - 1) buf.append(", "); } buf.append(')'); return buf.toString(); } }