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