1 11 12 package org.eclipse.osgi.framework.util; 13 14 import java.io.*; 15 import java.util.*; 16 import org.eclipse.osgi.framework.internal.core.Msg; 17 import org.eclipse.osgi.util.ManifestElement; 18 import org.eclipse.osgi.util.NLS; 19 import org.osgi.framework.BundleException; 20 21 32 public class Headers extends Dictionary implements Map { 33 private boolean readOnly = false; 34 private Object [] headers; 35 private Object [] values; 36 private int size = 0; 37 38 43 public Headers(int initialCapacity) { 44 super(); 45 headers = new Object [initialCapacity]; 46 values = new Object [initialCapacity]; 47 } 48 49 56 public Headers(Dictionary values) { 57 this(values.size()); 58 59 Enumeration keys = values.keys(); 60 while (keys.hasMoreElements()) { 61 Object key = keys.nextElement(); 62 set(key, values.get(key)); 63 } 64 } 65 66 69 public synchronized Enumeration keys() { 70 return new ArrayEnumeration(headers, size); 71 } 72 73 76 public synchronized Enumeration elements() { 77 return new ArrayEnumeration(values, size); 78 } 79 80 private int getIndex(Object key) { 81 boolean stringKey = key instanceof String ; 82 for (int i = 0; i < size; i++) { 83 if (stringKey && (headers[i] instanceof String )) { 84 if (((String ) headers[i]).equalsIgnoreCase((String ) key)) 85 return i; 86 } else { 87 if (headers[i].equals(key)) 88 return i; 89 } 90 } 91 return -1; 92 } 93 94 private Object remove(int remove) { 95 Object removed = values[remove]; 96 for (int i = remove; i < size; i++) { 97 if (i == headers.length - 1) { 98 headers[i] = null; 99 values[i] = null; 100 } else { 101 headers[i] = headers[i + 1]; 102 values[i] = values[i + 1]; 103 } 104 } 105 if (remove < size) 106 size--; 107 return removed; 108 } 109 110 private void add(Object header, Object value) { 111 if (size == headers.length) { 112 Object [] newHeaders = new Object [headers.length + 10]; 114 Object [] newValues = new Object [values.length + 10]; 115 System.arraycopy(headers, 0, newHeaders, 0, headers.length); 116 System.arraycopy(values, 0, newValues, 0, values.length); 117 headers = newHeaders; 118 values = newValues; 119 } 120 headers[size] = header; 121 values[size] = value; 122 size++; 123 } 124 125 130 public synchronized Object get(Object key) { 131 int i = -1; 132 if ((i = getIndex(key)) != -1) 133 return values[i]; 134 return null; 135 } 136 137 153 public synchronized Object set(Object key, Object value, boolean replace) { 154 if (readOnly) 155 throw new UnsupportedOperationException (); 156 if (key instanceof String ) 157 key = ((String ) key).intern(); 158 int i = getIndex(key); 159 if (value == null) { 160 if (i != -1) 161 return remove(i); 162 } else { 163 if (i != -1) { 164 if (!replace) 165 throw new IllegalArgumentException (NLS.bind(Msg.HEADER_DUPLICATE_KEY_EXCEPTION, key)); 166 Object oldVal = values[i]; 167 values[i] = value; 168 return oldVal; 169 } 170 add(key, value); 171 } 172 return null; 173 } 174 175 186 public synchronized Object set(Object key, Object value) { 187 return set(key, value, false); 188 } 189 190 public synchronized void setReadOnly() { 191 readOnly = true; 192 } 193 194 199 public synchronized int size() { 200 return size; 201 } 202 203 211 public synchronized boolean isEmpty() { 212 return size == 0; 213 } 214 215 222 public synchronized Object put(Object key, Object value) { 223 if (readOnly) 224 throw new UnsupportedOperationException (); 225 return set(key, value, true); 226 } 227 228 234 public Object remove(Object key) { 235 throw new UnsupportedOperationException (); 236 } 237 238 public String toString() { 239 return (values.toString()); 240 } 241 242 public static Headers parseManifest(InputStream in) throws BundleException { 243 Headers headers = new Headers(10); 244 try { 245 ManifestElement.parseBundleManifest(in, headers); 246 } catch (IOException e) { 247 throw new BundleException(Msg.MANIFEST_IOEXCEPTION, e); 248 } 249 headers.setReadOnly(); 250 return headers; 251 } 252 253 class ArrayEnumeration implements Enumeration { 254 private Object [] array; 255 int cur = 0; 256 257 public ArrayEnumeration(Object [] array, int size) { 258 this.array = new Object [size]; 259 System.arraycopy(array, 0, this.array, 0, this.array.length); 260 } 261 262 public boolean hasMoreElements() { 263 return cur < array.length; 264 } 265 266 public Object nextElement() { 267 return array[cur++]; 268 } 269 } 270 271 public synchronized void clear() { 272 if (readOnly) 273 throw new UnsupportedOperationException (); 274 } 275 276 public synchronized boolean containsKey(Object key) { 277 return getIndex(key) >= 0; 278 } 279 280 public boolean containsValue(Object var0) { 281 throw new UnsupportedOperationException (); 282 } 283 284 public Set entrySet() { 285 throw new UnsupportedOperationException (); 286 } 287 288 public Set keySet() { 289 throw new UnsupportedOperationException (); 290 } 291 292 public void putAll(Map var0) { 293 throw new UnsupportedOperationException (); 294 } 295 296 public Collection values() { 297 throw new UnsupportedOperationException (); 298 } 299 } 300 | Popular Tags |