Entry.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Entry.java --
  2. Copyright (C) 2003, 2006, 2010 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 gnu.java.security.Configuration;
  33. import java.io.DataInputStream;
  34. import java.io.DataOutputStream;
  35. import java.io.IOException;
  36. import java.util.logging.Logger;
  37. /**
  38. * An immutable class representing a single entry in a keyring.
  39. */
  40. public abstract class Entry
  41. {
  42. private static final Logger log = Configuration.DEBUG ?
  43. Logger.getLogger(Entry.class.getName()) : null;
  44. private static final String[] TYPES = new String[] {
  45. "Encrypted",
  46. "PasswordEncrypted",
  47. "Authenticated",
  48. "PasswordAuthenticated",
  49. "Compressed",
  50. "Certificate",
  51. "PublicKey",
  52. "PrivateKey",
  53. "CertPath",
  54. "BinaryData" };
  55. /** This entry's type identifier. */
  56. protected int type;
  57. /** This entry's property set. */
  58. protected Properties properties;
  59. /** This entry's payload. */
  60. protected byte[] payload;
  61. /**
  62. * Creates a new Entry.
  63. *
  64. * @param type This entry's type.
  65. * @param properties This entry's properties.
  66. * @throws IllegalArgumentException If the properties argument is null, or if
  67. * the type is out of range.
  68. */
  69. protected Entry(int type, Properties properties)
  70. {
  71. if (type < 0 || type > 255)
  72. throw new IllegalArgumentException("invalid packet type");
  73. if (properties == null)
  74. throw new IllegalArgumentException("no properties");
  75. this.type = type;
  76. this.properties = (Properties) properties.clone();
  77. }
  78. /**
  79. * Constructor for use by subclasses.
  80. */
  81. protected Entry(final int type)
  82. {
  83. if (type < 0 || type > 255)
  84. throw new IllegalArgumentException("invalid packet type");
  85. this.type = type;
  86. properties = new Properties();
  87. }
  88. /**
  89. * Returns this entry's properties object. The properties are cloned before
  90. * being returned.
  91. *
  92. * @return The properties.
  93. */
  94. public Properties getProperties()
  95. {
  96. return (Properties) properties.clone();
  97. }
  98. /**
  99. * Returns this entry's payload data, or null if
  100. */
  101. public byte[] getPayload()
  102. {
  103. if (payload == null)
  104. return null;
  105. return (byte[]) payload.clone();
  106. }
  107. /**
  108. * This method is called when this entry needs to be written to an output
  109. * stream.
  110. *
  111. * @param out The stream to write to.
  112. * @throws IOException If an I/O exception occurs.
  113. */
  114. public void encode(DataOutputStream out) throws IOException
  115. {
  116. if (payload == null)
  117. encodePayload();
  118. if (out == null)
  119. return;
  120. out.write(type);
  121. properties.encode(out);
  122. out.writeInt(payload.length);
  123. out.write(payload);
  124. }
  125. public String toString()
  126. {
  127. return new StringBuilder("Entry{")
  128. .append("type=").append(TYPES[type])
  129. .append(", properties=").append(properties)
  130. .append(", payload=")
  131. .append(payload == null ? "-" : "byte[" + payload.length + "]")
  132. .append( "}")
  133. .toString();
  134. }
  135. /**
  136. * Generic decoding method, which simply decodes the properties field
  137. * and reads the payload field.
  138. *
  139. * @param in The input data stream.
  140. * @throws IOException If an I/O error occurs.
  141. */
  142. protected void defaultDecode(DataInputStream in) throws IOException
  143. {
  144. properties = new Properties();
  145. properties.decode(in);
  146. int len = in.readInt();
  147. if (len < 0)
  148. throw new IOException("corrupt length");
  149. if (Configuration.DEBUG)
  150. log.fine("About to instantiate new payload byte array for " + this);
  151. payload = new byte[len];
  152. in.readFully(payload);
  153. }
  154. /**
  155. * This method is called of subclasses when the payload data needs to be
  156. * created.
  157. *
  158. * @throws IOException If an encoding error occurs.
  159. */
  160. protected abstract void encodePayload() throws IOException;
  161. }