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