1
14
15 package com.liferay.portal.kernel.io.unsync;
16
17 import java.io.InputStream;
18
19
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 }