Properties.java 5.7 KB

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