1. /*
  2. * @(#)BufferedOutputStream.java 1.27 00/02/02
  3. *
  4. * Copyright 1994-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.io;
  11. /**
  12. * The class implements a buffered output stream. By setting up such
  13. * an output stream, an application can write bytes to the underlying
  14. * output stream without necessarily causing a call to the underlying
  15. * system for each byte written. The data is written into an internal
  16. * buffer, and then written to the underlying stream if the buffer
  17. * reaches its capacity, the buffer output stream is closed, or the
  18. * buffer output stream is explicitly flushed.
  19. *
  20. * @author Arthur van Hoff
  21. * @version 1.27, 02/02/00
  22. * @since JDK1.0
  23. */
  24. public
  25. class BufferedOutputStream extends FilterOutputStream {
  26. /**
  27. * The internal buffer where data is stored.
  28. */
  29. protected byte buf[];
  30. /**
  31. * The number of valid bytes in the buffer. This value is always
  32. * in the range <tt>0</tt> through <tt>buf.length</tt> elements
  33. * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid
  34. * byte data.
  35. */
  36. protected int count;
  37. /**
  38. * Creates a new buffered output stream to write data to the
  39. * specified underlying output stream with a default 512-byte
  40. * buffer size.
  41. *
  42. * @param out the underlying output stream.
  43. */
  44. public BufferedOutputStream(OutputStream out) {
  45. this(out, 512);
  46. }
  47. /**
  48. * Creates a new buffered output stream to write data to the
  49. * specified underlying output stream with the specified buffer
  50. * size.
  51. *
  52. * @param out the underlying output stream.
  53. * @param size the buffer size.
  54. * @exception IllegalArgumentException if size <= 0.
  55. */
  56. public BufferedOutputStream(OutputStream out, int size) {
  57. super(out);
  58. if (size <= 0) {
  59. throw new IllegalArgumentException("Buffer size <= 0");
  60. }
  61. buf = new byte[size];
  62. }
  63. /** Flush the internal buffer */
  64. private void flushBuffer() throws IOException {
  65. if (count > 0) {
  66. out.write(buf, 0, count);
  67. count = 0;
  68. }
  69. }
  70. /**
  71. * Writes the specified byte to this buffered output stream.
  72. *
  73. * @param b the byte to be written.
  74. * @exception IOException if an I/O error occurs.
  75. */
  76. public synchronized void write(int b) throws IOException {
  77. if (count >= buf.length) {
  78. flushBuffer();
  79. }
  80. buf[count++] = (byte)b;
  81. }
  82. /**
  83. * Writes <code>len</code> bytes from the specified byte array
  84. * starting at offset <code>off</code> to this buffered output stream.
  85. *
  86. * <p> Ordinarily this method stores bytes from the given array into this
  87. * stream's buffer, flushing the buffer to the underlying output stream as
  88. * needed. If the requested length is at least as large as this stream's
  89. * buffer, however, then this method will flush the buffer and write the
  90. * bytes directly to the underlying output stream. Thus redundant
  91. * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
  92. *
  93. * @param b the data.
  94. * @param off the start offset in the data.
  95. * @param len the number of bytes to write.
  96. * @exception IOException if an I/O error occurs.
  97. */
  98. public synchronized void write(byte b[], int off, int len) throws IOException {
  99. if (len >= buf.length) {
  100. /* If the request length exceeds the size of the output buffer,
  101. flush the output buffer and then write the data directly.
  102. In this way buffered streams will cascade harmlessly. */
  103. flushBuffer();
  104. out.write(b, off, len);
  105. return;
  106. }
  107. if (len > buf.length - count) {
  108. flushBuffer();
  109. }
  110. System.arraycopy(b, off, buf, count, len);
  111. count += len;
  112. }
  113. /**
  114. * Flushes this buffered output stream. This forces any buffered
  115. * output bytes to be written out to the underlying output stream.
  116. *
  117. * @exception IOException if an I/O error occurs.
  118. * @see java.io.FilterOutputStream#out
  119. */
  120. public synchronized void flush() throws IOException {
  121. flushBuffer();
  122. out.flush();
  123. }
  124. }