1. /*
  2. * @(#)CheckedOutputStream.java 1.15 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util.zip;
  11. import java.io.FilterOutputStream;
  12. import java.io.OutputStream;
  13. import java.io.IOException;
  14. /**
  15. * An output stream that also maintains a checksum of the data being
  16. * written. The checksum can then be used to verify the integrity of
  17. * the output data.
  18. *
  19. * @see Checksum
  20. * @version 1.15, 02/02/00
  21. * @author David Connelly
  22. */
  23. public
  24. class CheckedOutputStream extends FilterOutputStream {
  25. private Checksum cksum;
  26. /**
  27. * Creates an output stream with the specified Checksum.
  28. * @param out the output stream
  29. * @param cksum the checksum
  30. */
  31. public CheckedOutputStream(OutputStream out, Checksum cksum) {
  32. super(out);
  33. this.cksum = cksum;
  34. }
  35. /**
  36. * Writes a byte. Will block until the byte is actually written.
  37. * @param b the byte to be written
  38. * @exception IOException if an I/O error has occurred
  39. */
  40. public void write(int b) throws IOException {
  41. out.write(b);
  42. cksum.update(b);
  43. }
  44. /**
  45. * Writes an array of bytes. Will block until the bytes are
  46. * actually written.
  47. * @param b the data to be written
  48. * @param off the start offset of the data
  49. * @param len the number of bytes to be written
  50. * @exception IOException if an I/O error has occurred
  51. */
  52. public void write(byte[] b, int off, int len) throws IOException {
  53. out.write(b, off, len);
  54. cksum.update(b, off, len);
  55. }
  56. /**
  57. * Returns the Checksum for this output stream.
  58. * @return the Checksum
  59. */
  60. public Checksum getChecksum() {
  61. return cksum;
  62. }
  63. }