1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.synchronoss.cpo;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28
29
30
31
32
33 public class CpoByteArrayInputStream extends ByteArrayInputStream implements java.io.Serializable, java.lang.Cloneable {
34
35
36
37
38 private static final long serialVersionUID = 1L;
39 private byte[] buffer_ = null;
40 private int offset_ = 0;
41 private int size_ = 0;
42
43 public CpoByteArrayInputStream(byte[] buffer) {
44 super(buffer);
45 buffer_ =buffer;
46 }
47
48 public CpoByteArrayInputStream(byte[] buffer, int offset, int length) {
49 super(buffer, offset, length);
50 buffer_ = buffer;
51 offset_ = offset;
52 size_ = length;
53 }
54
55 protected void setBuffer(byte[] buffer) {
56 buffer_ = buffer;
57 }
58
59 protected byte[] getBuffer() {
60 return buffer_;
61 }
62
63 protected void setOffset(int offset) {
64 offset_ = offset;
65 }
66
67 protected int getOffset() {
68 return offset_;
69 }
70
71 protected void setSize(int size) {
72 size_ = size;
73 }
74
75 protected int getSize() {
76 return size_;
77 }
78
79 public int getLength() {
80 int l;
81
82 if (getOffset() == 0) {
83 l = getBuffer().length;
84 } else {
85 l = getSize() < getBuffer().length - getOffset() ? getSize() : getBuffer().length - getOffset();
86 }
87 return l;
88 }
89
90 static public CpoByteArrayInputStream getCpoStream(InputStream is) {
91 CpoByteArrayInputStream cbais = null;
92
93 if (is instanceof CpoByteArrayInputStream) {
94 cbais = ((CpoByteArrayInputStream) is);
95 } else {
96
97 int b;
98 try {
99 ByteArrayOutputStream baos = new ByteArrayOutputStream();
100 while ((b = is.read()) != -1) {
101 baos.write(b);
102 }
103 cbais = new CpoByteArrayInputStream(baos.toByteArray());
104 } catch (IOException ioe) {
105
106 } finally {
107 try {
108 is.close();
109 } catch (IOException ioe) {
110 }
111 }
112
113 }
114
115 return cbais;
116 }
117 }