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