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