001
014
015 package com.liferay.portal.kernel.io.unsync;
016
017 import java.io.IOException;
018 import java.io.OutputStream;
019
020
027 public class UnsyncBufferedOutputStream extends UnsyncFilterOutputStream {
028
029 public UnsyncBufferedOutputStream(OutputStream outputStream) {
030 this(outputStream, _DEFAULT_BUFFER_SIZE);
031 }
032
033 public UnsyncBufferedOutputStream(OutputStream outputStream, int size) {
034 super(outputStream);
035
036 buffer = new byte[size];
037 }
038
039 public void flush() throws IOException {
040 if (count > 0) {
041 outputStream.write(buffer, 0, count);
042
043 count = 0;
044 }
045
046 outputStream.flush();
047 }
048
049 public void write(byte[] byteArray, int offset, int length)
050 throws IOException {
051
052 if (length >= buffer.length) {
053 if (count > 0) {
054 outputStream.write(buffer, 0, count);
055
056 count = 0;
057 }
058
059 outputStream.write(byteArray, offset, length);
060
061 return;
062 }
063
064 if (count > 0 && length > buffer.length - count) {
065 outputStream.write(buffer, 0, count);
066
067 count = 0;
068 }
069
070 System.arraycopy(byteArray, offset, buffer, count, length);
071
072 count += length;
073 }
074
075 public void write(byte[] byteArray) throws IOException {
076 write(byteArray, 0, byteArray.length);
077 }
078
079 public void write(int b) throws IOException {
080 if (count >= buffer.length) {
081 outputStream.write(buffer, 0, count);
082
083 count = 0;
084 }
085
086 buffer[count++] = (byte)b;
087 }
088
089 protected byte[] buffer;
090 protected int count;
091
092 private static int _DEFAULT_BUFFER_SIZE = 8192;
093
094 }