DigestInputStream.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* DigestInputStream.java --- An Input stream tied to a message digest
  2. Copyright (C) 1999, 2003, 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.FilterInputStream;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. /**
  36. * DigestInputStream is a class that ties an InputStream with a
  37. * MessageDigest. The Message Digest is used by the class to
  38. * update it self as bytes are read from the InputStream.
  39. *
  40. * The updating to the digest depends on the on flag which is set
  41. * to true by default to tell the class to update the data
  42. * in the message digest.
  43. *
  44. * @version 0.0
  45. * @author Mark Benvenuto (ivymccough@worldnet.att.net)
  46. */
  47. public class DigestInputStream extends FilterInputStream
  48. {
  49. /**
  50. * The message digest for the DigestInputStream
  51. */
  52. protected MessageDigest digest;
  53. //Manages the on flag
  54. private boolean state = true;
  55. /**
  56. * Constructs a new DigestInputStream.
  57. * It associates a MessageDigest with the stream to
  58. * compute the stream as data is written.
  59. *
  60. * @param stream An InputStream to associate this stream with
  61. * @param digest A MessageDigest to hash the stream with
  62. */
  63. public DigestInputStream(InputStream stream, MessageDigest digest)
  64. {
  65. super(stream);
  66. //this.in = stream;
  67. this.digest = digest;
  68. }
  69. /**
  70. * Returns the MessageDigest associated with this DigestInputStream
  71. *
  72. * @return The MessageDigest used to hash this stream
  73. */
  74. public MessageDigest getMessageDigest()
  75. {
  76. return digest;
  77. }
  78. /**
  79. * Sets the current MessageDigest to current parameter
  80. *
  81. * @param digest A MessageDigest to associate with this stream
  82. */
  83. public void setMessageDigest(MessageDigest digest)
  84. {
  85. this.digest = digest;
  86. }
  87. /**
  88. * Reads a byte from the input stream and updates the digest.
  89. * This method reads the underlying input stream and if the
  90. * on flag is true then updates the message digest.
  91. *
  92. * @return Returns a byte from the input stream, -1 is returned to indicate that
  93. * the end of stream was reached before this read call
  94. *
  95. * @throws IOException if an IO error occurs in the underlying input stream,
  96. * this error is thrown
  97. */
  98. public int read() throws IOException
  99. {
  100. int temp = in.read();
  101. if (state == true && temp != -1)
  102. digest.update((byte) temp);
  103. return temp;
  104. }
  105. /**
  106. * Reads bytes from the input stream and updates the digest.
  107. * This method reads the underlying input stream and if the
  108. * on flag is true then updates the message digest.
  109. *
  110. * @param b a byte array to store the data from the input stream
  111. * @param off an offset to start at in the array
  112. * @param len length of data to read
  113. * @return Returns count of bytes read, -1 is returned to indicate that
  114. * the end of stream was reached before this read call
  115. *
  116. * @throws IOException if an IO error occurs in the underlying input stream,
  117. * this error is thrown
  118. */
  119. public int read(byte[]b, int off, int len) throws IOException
  120. {
  121. int temp = in.read(b, off, len);
  122. if (state == true && temp != -1)
  123. digest.update(b, off, temp);
  124. return temp;
  125. }
  126. /**
  127. * Sets the flag specifing if this DigestInputStream updates the
  128. * digest in the write() methods. The default is on;
  129. *
  130. * @param on True means it digests stream, false means it does not
  131. */
  132. public void on(boolean on)
  133. {
  134. state = on;
  135. }
  136. /**
  137. * Converts the input stream and underlying message digest to a string.
  138. *
  139. * @return A string representing the input stream and message digest.
  140. */
  141. public String toString()
  142. {
  143. return "[Digest Input Stream] " + digest.toString();
  144. }
  145. }