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    import java.io.UnsupportedEncodingException;
020    
021    /**
022     * <p>
023     * See http://issues.liferay.com/browse/LPS-6648.
024     * </p>
025     *
026     * @author Shuyang Zhou
027     */
028    public class UnsyncByteArrayOutputStream extends OutputStream {
029    
030            public UnsyncByteArrayOutputStream() {
031                    this(32);
032            }
033    
034            public UnsyncByteArrayOutputStream(int size) {
035                    buffer = new byte[size];
036            }
037    
038            public void reset() {
039                    index = 0;
040            }
041    
042            public int size() {
043                    return index;
044            }
045    
046            public byte[] toByteArray() {
047                    byte[] newBuffer = new byte[index];
048    
049                    System.arraycopy(buffer, 0, newBuffer, 0, index);
050    
051                    return newBuffer;
052            }
053    
054            public String toString() {
055                    return new String(buffer, 0, index);
056            }
057    
058            public String toString(String charsetName)
059                    throws UnsupportedEncodingException {
060    
061                    return new String(buffer, 0, index, charsetName);
062            }
063    
064            public byte[] unsafeGetByteArray() {
065                    return buffer;
066            }
067    
068            public void write(byte[] byteArray) {
069                    write(byteArray, 0, byteArray.length);
070            }
071    
072            public void write(byte[] byteArray, int offset, int length) {
073                    if (length <= 0) {
074                            return;
075                    }
076    
077                    int newIndex = index + length;
078    
079                    if (newIndex > buffer.length) {
080                            int newBufferSize = Math.max(buffer.length << 1, newIndex);
081    
082                            byte[] newBuffer = new byte[newBufferSize];
083    
084                            System.arraycopy(buffer, 0, newBuffer, 0, index);
085    
086                            buffer = newBuffer;
087                    }
088    
089                    System.arraycopy(byteArray, offset, buffer, index, length);
090    
091                    index = newIndex;
092            }
093    
094            public void write(int b) {
095                    int newIndex = index + 1;
096    
097                    if (newIndex > buffer.length) {
098                            int newBufferSize = Math.max(buffer.length << 1, newIndex);
099    
100                            byte[] newBuffer = new byte[newBufferSize];
101    
102                            System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
103    
104                            buffer = newBuffer;
105                    }
106    
107                    buffer[index] = (byte)b;
108    
109                    index = newIndex;
110            }
111    
112            public void writeTo(OutputStream outputStream) throws IOException {
113                    outputStream.write(buffer, 0, index);
114            }
115    
116            protected byte[] buffer;
117            protected int index;
118    
119    }