1
22
23 package com.liferay.portal.kernel.io.unsync;
24
25 import java.io.InputStream;
26
27
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 }