001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io.unsync;
016    
017    import java.io.IOException;
018    import java.io.OutputStream;
019    
020    /**
021     * <p>
022     * See http://issues.liferay.com/browse/LPS-6648.
023     * </p>
024     *
025     * @author Shuyang Zhou
026     */
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    }