001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.io.unsync;
016    
017    import java.io.InputStream;
018    
019    /**
020     * <p>
021     * See http://issues.liferay.com/browse/LPS-6648.
022     * </p>
023     *
024     * @author Shuyang Zhou
025     */
026    public class UnsyncByteArrayInputStream extends InputStream {
027    
028            public UnsyncByteArrayInputStream(byte[] buffer) {
029                    this.buffer = buffer;
030                    this.index = 0;
031                    this.capacity = buffer.length;
032            }
033    
034            public UnsyncByteArrayInputStream(byte[] buffer, int offset, int length) {
035                    this.buffer = buffer;
036                    this.index = offset;
037                    this.capacity = Math.min(buffer.length, offset + length);
038                    this.markIndex = offset;
039            }
040    
041            public int available() {
042                    return capacity - index;
043            }
044    
045            public void mark(int readAheadLimit) {
046                    markIndex = index;
047            }
048    
049            public boolean markSupported() {
050                    return true;
051            }
052    
053            public int read() {
054                    if (index < capacity) {
055                            return buffer[index++] & 0xff;
056                    }
057                    else {
058                            return -1;
059                    }
060            }
061    
062            public int read(byte[] byteArray) {
063                    return read(byteArray, 0, byteArray.length);
064            }
065    
066            public int read(byte[] byteArray, int offset, int length) {
067                    if (length <= 0) {
068                            return 0;
069                    }
070    
071                    if (index >= capacity) {
072                            return -1;
073                    }
074    
075                    int read = length;
076    
077                    if ((index + read) > capacity) {
078                            read = capacity - index;
079                    }
080    
081                    System.arraycopy(buffer, index, byteArray, offset, read);
082    
083                    index += read;
084    
085                    return read;
086            }
087    
088            public void reset() {
089                    index = markIndex;
090            }
091    
092            public long skip(long skip) {
093                    if (skip < 0) {
094                            return 0;
095                    }
096    
097                    if ((skip + index) > capacity) {
098                            skip = capacity - index;
099                    }
100    
101                    index += skip;
102    
103                    return skip;
104            }
105    
106            protected byte[] buffer;
107            protected int capacity;
108            protected int index;
109            protected int markIndex;
110    
111    }