1 25 26 package org.netbeans.modules.classfile; 27 28 import java.io.*; 29 import java.util.*; 30 31 43 public final class AttributeMap { 44 45 Map map; 46 47 50 static AttributeMap load(DataInputStream in, ConstantPool pool) 51 throws IOException { 52 return load(in, pool, false); 53 } 54 55 static AttributeMap load(DataInputStream in, ConstantPool pool, 56 boolean includeCode) throws IOException { 57 int count = in.readUnsignedShort(); 58 Map<String ,byte[]> map = new HashMap<String ,byte[]>(count + 1, (float)1.0); 59 for (int i = 0; i < count; i++) { 60 Object o = pool.get(in.readUnsignedShort()); 61 if (!(o instanceof CPUTF8Info)) 62 throw new InvalidClassFormatException(); 63 CPUTF8Info entry = (CPUTF8Info)o; 64 String name = entry.getName(); 65 int len = in.readInt(); 66 if (!includeCode && "Code".equals(name)) { 67 int n; 68 while ((n = (int)in.skip(len)) > 0 && n < len) 69 len -= n; 70 } else { 71 byte[] attr = new byte[len]; 72 in.readFully(attr); 73 map.put(name, attr); 74 } 75 } 76 return new AttributeMap(map); 77 } 78 79 AttributeMap(Map<String ,byte[]> attributes) { 80 this.map = attributes; 81 } 82 83 DataInputStream getStream(String name) { 84 byte[] attr = (byte[])map.get(name); 85 return attr != null ? 86 new DataInputStream(new ByteArrayInputStream(attr)) : null; 87 } 88 89 96 byte[] get(String name) { 97 return (byte[])map.get(name); 98 } 99 100 103 public int size() { 104 return map.size(); 105 } 106 107 110 public boolean isEmpty() { 111 return map.isEmpty(); 112 } 113 114 117 public boolean containsAttribute(String key) { 118 return map.containsKey(key); 119 } 120 121 124 public Set keySet() { 125 return map.keySet(); 126 } 127 128 } 129 | Popular Tags |