DigestOutputStream.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* DigestOutputStream.java --- An output stream tied to a message digest
  2. Copyright (C) 1999, 2004, 2005 Free Software Foundation, Inc.
  3. This file is 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, or (at your option)
  7. 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; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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 java.security;
  32. import java.io.FilterOutputStream;
  33. import java.io.IOException;
  34. import java.io.OutputStream;
  35. /**
  36. * DigestOutputStream is a class that ties an OutputStream with a
  37. * MessageDigest. The Message Digest is used by the class to update it
  38. * self as bytes are written to the OutputStream.
  39. *
  40. * The updating to the digest depends on the on flag which is set to
  41. * true by default that tells the class to update the data in the
  42. * message digest.
  43. *
  44. * @version 0.0
  45. * @author Mark Benvenuto (ivymccough@worldnet.att.net)
  46. */
  47. public class DigestOutputStream extends FilterOutputStream
  48. {
  49. /**
  50. * The message digest for the DigestOutputStream
  51. */
  52. protected MessageDigest digest;
  53. //Manages the on flag
  54. private boolean state = true;
  55. /**
  56. * Constructs a new DigestOutputStream. It associates a
  57. * MessageDigest with the stream to compute the stream as data is
  58. * written.
  59. *
  60. * @param stream An OutputStream to associate this stream with
  61. * @param digest A MessageDigest to hash the stream with
  62. */
  63. public DigestOutputStream(OutputStream stream, MessageDigest digest)
  64. {
  65. super(stream);
  66. this.digest = digest;
  67. }
  68. /**
  69. * Returns the MessageDigest associated with this DigestOutputStream
  70. *
  71. * @return The MessageDigest used to hash this stream
  72. */
  73. public MessageDigest getMessageDigest()
  74. {
  75. return digest;
  76. }
  77. /**
  78. * Sets the current MessageDigest to current parameter
  79. *
  80. * @param digest A MessageDigest to associate with this stream
  81. */
  82. public void setMessageDigest(MessageDigest digest)
  83. {
  84. this.digest = digest;
  85. }
  86. /**
  87. * Updates the hash if the on flag is true and then writes a byte to
  88. * the underlying output stream.
  89. *
  90. * @param b A byte to write to the output stream
  91. *
  92. * @exception IOException if the underlying output stream
  93. * cannot write the byte, this is thrown.
  94. */
  95. public void write(int b) throws IOException
  96. {
  97. if (state)
  98. digest.update((byte) b);
  99. out.write(b);
  100. }
  101. /**
  102. * Updates the hash if the on flag is true and then writes the bytes
  103. * to the underlying output stream.
  104. *
  105. * @param b Bytes to write to the output stream
  106. * @param off Offset to start to start at in array
  107. * @param len Length of data to write
  108. *
  109. * @exception IOException if the underlying output stream
  110. * cannot write the bytes, this is thrown.
  111. */
  112. public void write(byte[]b, int off, int len) throws IOException
  113. {
  114. if (state)
  115. digest.update(b, off, len);
  116. out.write(b, off, len);
  117. }
  118. /**
  119. * Sets the flag specifying if this DigestOutputStream updates the
  120. * digest in the write() methods. The default is on;
  121. *
  122. * @param on True means it digests stream, false means it does not
  123. */
  124. public void on(boolean on)
  125. {
  126. state = on;
  127. }
  128. /**
  129. * Converts the output stream and underlying message digest to a string.
  130. *
  131. * @return A string representing the output stream and message digest.
  132. */
  133. public String toString()
  134. {
  135. return "[Digest Output Stream] " + digest.toString();
  136. }
  137. }