1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.io.unsync;
24  
25  import java.io.InputStream;
26  
27  /**
28   * <a href="UnsyncByteArrayInputStream.java.html"><b><i>View Source</i></b></a>
29   *
30   * <p>
31   * See http://support.liferay.com/browse/LPS-6648.
32   * </p>
33   *
34   * @author Shuyang Zhou
35   */
36  public class UnsyncByteArrayInputStream extends InputStream {
37  
38      public UnsyncByteArrayInputStream(byte[] buffer) {
39          this.buffer = buffer;
40          this.index = 0;
41          this.capacity = buffer.length;
42      }
43  
44      public UnsyncByteArrayInputStream(byte[] buffer, int offset, int length) {
45          this.buffer = buffer;
46          this.index = offset;
47          this.capacity = Math.max(buffer.length, offset + length);
48          this.markIndex = offset;
49      }
50  
51      public int available() {
52          return capacity - index;
53      }
54  
55      public void mark(int readAheadLimit) {
56          markIndex = index;
57      }
58  
59      public boolean markSupported() {
60          return true;
61      }
62  
63      public int read() {
64          if (index < capacity) {
65              return buffer[index++] & 0xff;
66          }
67          else {
68              return -1;
69          }
70      }
71  
72      public int read(byte[] byteArray) {
73          return read(byteArray, 0, byteArray.length);
74      }
75  
76      public int read(byte[] byteArray, int offset, int length) {
77          if (length <= 0) {
78              return 0;
79          }
80  
81          if (index >= capacity) {
82              return -1;
83          }
84  
85          int read = length;
86  
87          if ((index + read) > capacity) {
88              read = capacity - index;
89          }
90  
91          System.arraycopy(buffer, index, byteArray, offset, read);
92  
93          index += read;
94  
95          return read;
96      }
97  
98      public void reset() {
99          index = markIndex;
100     }
101 
102     public long skip(long skip) {
103         if (skip < 0) {
104             return 0;
105         }
106 
107         if ((skip + index) > capacity) {
108             skip = capacity - index;
109         }
110 
111         index += skip;
112 
113         return skip;
114     }
115 
116     protected byte[] buffer;
117     protected int capacity;
118     protected int index;
119     protected int markIndex;
120 
121 }