Frames | No Frames |
1: /* Properties.java -- 2: Copyright (C) 2003, 2006 Free Software Foundation, Inc. 3: 4: This file is a part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2 of the License, or (at 9: your option) any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; if not, write to the Free Software 18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 19: USA 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package gnu.javax.crypto.keyring; 40: 41: import java.io.ByteArrayOutputStream; 42: import java.io.DataInputStream; 43: import java.io.DataOutputStream; 44: import java.io.IOException; 45: import java.util.HashMap; 46: import java.util.Iterator; 47: import java.util.Map; 48: 49: /** 50: * A set of <code>(name => value)</code> pairs used in keyring entries. 51: * Keys and values are simple strings, with the key never being empty and always 52: * treated case-insensitively. 53: */ 54: public class Properties 55: implements Cloneable 56: { 57: private HashMap props; 58: 59: /** 60: * Creates a new properties object. 61: */ 62: public Properties() 63: { 64: props = new HashMap(); 65: } 66: 67: /** 68: * Removes all properties from this object. 69: */ 70: public void clear() 71: { 72: props.clear(); 73: } 74: 75: /** 76: * Creates a copy of this properties object. 77: * 78: * @return The copy. 79: */ 80: public Object clone() 81: { 82: Properties result = new Properties(); 83: result.props.putAll(props); 84: return result; 85: } 86: 87: /** 88: * Tests if this object contains a given property name. 89: * 90: * @param key The key to test. 91: * @return True if this object contains the given key. 92: */ 93: public boolean containsKey(String key) 94: { 95: if (key == null || key.length() == 0) 96: return false; 97: return props.containsKey(canonicalize(key)); 98: } 99: 100: /** 101: * Tests if this object contains a given property value. 102: * 103: * @param value The value to test. 104: * @return True if this object contains the given value. 105: */ 106: public boolean containsValue(String value) 107: { 108: if (value == null) 109: return false; 110: return props.containsValue(value); 111: } 112: 113: /** 114: * Adds a new property to this object. 115: * 116: * @param key The key, which can neither be null nor empty. 117: * @param value The value, which cannot be null. 118: * @return The old value mapped by the key, if any. 119: * @throws IllegalArgumentException If either the key or value parameter is 120: * null, or if the key is empty. 121: */ 122: public String put(String key, String value) 123: { 124: if (key == null || value == null || key.length() == 0) 125: throw new IllegalArgumentException("key nor value can be null"); 126: return (String) props.put(canonicalize(key), value); 127: } 128: 129: /** 130: * Returns the value mapped by the given key, or null if there is no such 131: * mapping. 132: * 133: * @param key 134: */ 135: public String get(String key) 136: { 137: if (key == null || key.length() == 0) 138: return null; 139: return (String) props.get(canonicalize(key)); 140: } 141: 142: /** 143: * Removes a key and its value from this object. 144: * 145: * @param key The key of the property to remove. 146: * @return The old value mapped by the key, if any. 147: */ 148: public String remove(String key) 149: { 150: if (key == null || key.length() == 0) 151: return null; 152: return (String) props.remove(canonicalize(key)); 153: } 154: 155: /** 156: * Decodes a set of properties from the given input stream. 157: * 158: * @param in The input stream. 159: * @throws IOException If an I/O error occurs. 160: */ 161: public void decode(DataInputStream in) throws IOException 162: { 163: int len = in.readInt(); 164: MeteredInputStream min = new MeteredInputStream(in, len); 165: DataInputStream in2 = new DataInputStream(min); 166: while (! min.limitReached()) 167: { 168: String name = in2.readUTF(); 169: String value = in2.readUTF(); 170: put(name, value); 171: } 172: } 173: 174: /** 175: * Encodes this set of properties to the given output stream. 176: * 177: * @param out The output stream to encode to. 178: * @throws IOException If an I/O error occurs. 179: */ 180: public void encode(DataOutputStream out) throws IOException 181: { 182: ByteArrayOutputStream buf = new ByteArrayOutputStream(); 183: DataOutputStream out2 = new DataOutputStream(buf); 184: for (Iterator it = props.entrySet().iterator(); it.hasNext();) 185: { 186: Map.Entry entry = (Map.Entry) it.next(); 187: out2.writeUTF((String) entry.getKey()); 188: out2.writeUTF((String) entry.getValue()); 189: } 190: out.writeInt(buf.size()); 191: buf.writeTo(out); 192: } 193: 194: public String toString() 195: { 196: return props.toString(); 197: } 198: 199: private String canonicalize(String key) 200: { 201: return key.toLowerCase(); 202: } 203: }