1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.io.unsync;
16  
17  import java.io.InputStream;
18  
19  /**
20   * <a href="UnsyncByteArrayInputStream.java.html"><b><i>View Source</i></b></a>
21   *
22   * <p>
23   * See http://issues.liferay.com/browse/LPS-6648.
24   * </p>
25   *
26   * @author Shuyang Zhou
27   */
28  public class UnsyncByteArrayInputStream extends InputStream {
29  
30      public UnsyncByteArrayInputStream(byte[] buffer) {
31          this.buffer = buffer;
32          this.index = 0;
33          this.capacity = buffer.length;
34      }
35  
36      public UnsyncByteArrayInputStream(byte[] buffer, int offset, int length) {
37          this.buffer = buffer;
38          this.index = offset;
39          this.capacity = Math.min(buffer.length, offset + length);
40          this.markIndex = offset;
41      }
42  
43      public int available() {
44          return capacity - index;
45      }
46  
47      public void mark(int readAheadLimit) {
48          markIndex = index;
49      }
50  
51      public boolean markSupported() {
52          return true;
53      }
54  
55      public int read() {
56          if (index < capacity) {
57              return buffer[index++] & 0xff;
58          }
59          else {
60              return -1;
61          }
62      }
63  
64      public int read(byte[] byteArray) {
65          return read(byteArray, 0, byteArray.length);
66      }
67  
68      public int read(byte[] byteArray, int offset, int length) {
69          if (length <= 0) {
70              return 0;
71          }
72  
73          if (index >= capacity) {
74              return -1;
75          }
76  
77          int read = length;
78  
79          if ((index + read) > capacity) {
80              read = capacity - index;
81          }
82  
83          System.arraycopy(buffer, index, byteArray, offset, read);
84  
85          index += read;
86  
87          return read;
88      }
89  
90      public void reset() {
91          index = markIndex;
92      }
93  
94      public long skip(long skip) {
95          if (skip < 0) {
96              return 0;
97          }
98  
99          if ((skip + index) > capacity) {
100             skip = capacity - index;
101         }
102 
103         index += skip;
104 
105         return skip;
106     }
107 
108     protected byte[] buffer;
109     protected int capacity;
110     protected int index;
111     protected int markIndex;
112 
113 }