-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppendableByteArrayInputStream.java
More file actions
160 lines (147 loc) · 5.72 KB
/
AppendableByteArrayInputStream.java
File metadata and controls
160 lines (147 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* @(#)AppendableByteArrayInputStream.java 1.0 2011-08-28
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import java.io.ByteArrayInputStream;
import java.util.zip.Adler32;
/**
* A {@code ByteArrayInputStream} which allows to replace the byte buffer underneath.
*
* @author Werner Randelshofer
* @version 1.0 2011-08-28 Created.
*/
public class AppendableByteArrayInputStream extends ByteArrayInputStream {
public AppendableByteArrayInputStream(byte[] buf, int offset, int length) {
super(buf, offset, length);
// System.out.println("AppendableByteArrayInputStream pos="+pos+" count="+count);
}
public AppendableByteArrayInputStream(byte[] buf) {
super(buf);
// System.out.println("AppendableByteArrayInputStream pos="+pos+" count="+count);
}
@Override
public synchronized int read() {
int b = super.read();
/*
if (b!=-1) {
String hex="0"+Integer.toHexString(b);
System.out.println(hex.substring(hex.length()-2));
}*/
return b;
}
@Override
public synchronized int read(byte[] b, int off, int len) {
// System.out.println("AppendableByteArrayInputStream.read... pos="+pos+" count="+count);
int count = super.read(b, off, len);
/*
if (count>0) {
for (int i=0;i<count;i++) {
if (i%16==15) System.out.println();
String hex="0"+Integer.toHexString(b[i]);
System.out.print(hex.substring(hex.length()-2));
}
System.out.println(" pos="+pos+" count="+this.count);
}*/
return count;
}
/** Appends new data to the buffer.
*
* @param buf Data.
* @param offset Offset in the data.
* @param length Length of the data.
* @param discard True if data which has already been read can be discarded.
*/
public void appendBuffer(byte[] buf, int offset, int length, boolean discard) {
if (discard) {
if (this.buf.length >= count - pos + length) {
// => the buffer has enough space for the existing data and the new data
System.arraycopy(this.buf, pos, this.buf, 0, count - pos);
System.arraycopy(buf, offset, this.buf, count - pos, length);
this.count = count - pos + length;
this.pos = 0;
this.mark = 0;
} else {
// => the buffer does not have enough space for the new data
byte[] newBuf = new byte[(count - pos + length + 31) / 32 * 32];
System.arraycopy(this.buf, pos, newBuf, 0, count - pos);
System.arraycopy(buf, offset, newBuf, count - pos, length);
this.buf = newBuf;
this.count = count - pos + length;
this.pos = 0;
this.mark = 0;
}
} else {
if (this.buf.length >= count + length) {
// => the buffer has enough space for the existing data and the new data
System.arraycopy(buf, offset, this.buf, count, length);
this.count = count + length;
} else {
// => the buffer does not have enough space for the new data
byte[] newBuf = new byte[(this.buf.length + length + 31) / 32 * 32];
System.arraycopy(this.buf, 0, newBuf, 0, count);
System.arraycopy(buf, offset, newBuf, count, length);
this.buf = newBuf;
this.count = count + length;
}
}
//System.out.println("AppendableByteArrayInputStream.appendBuffer pos="+pos+" count="+count);
}
/** Sets the buffer and resets the stream.
* This will overwrite the data array in the buffer, if it is large enough.
* Otherwise it will create a new data array and copy the data into it.
*
* @param buf Data.
* @param offset Offset in the data.
* @param length Length of the data.
*/
public void setBuffer(byte[] buf, int offset, int length) {
if (this.buf.length >= length) {
// => the buffer has enough space for the existing data and the new data
System.arraycopy(buf, offset, this.buf, 0, length);
this.count = length;
this.pos = 0;
this.mark = 0;
} else {
// => the buffer may not be overwritten or does not have enough space for the new data
this.buf = null;
this.buf = new byte[(length + 31) & ~31];
System.arraycopy(buf, offset, this.buf, 0, length);
this.count = length;
this.pos = 0;
this.mark = 0;
}
}
public static void main(String[] args) {
byte[] b = new byte[5];
int count = 0;
for (int i = 0; i < b.length; i++) {
b[i] = (byte) count++;
}
AppendableByteArrayInputStream in = new AppendableByteArrayInputStream(b);
for (int j = 0; j < 3; j++) {
System.out.println(in.read());
}
b = new byte[4];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) count++;
}
in.appendBuffer(b, 0, b.length, true);
for (int j = 0; j < 3; j++) {
System.out.println(in.read());
}
b = new byte[6];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) count++;
}
in.appendBuffer(b, 0, b.length, true);
for (int d = in.read(); d >= 0; d = in.read()) {
System.out.println(d);
}
}
}