forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergedStream.java
More file actions
141 lines (121 loc) · 3.07 KB
/
MergedStream.java
File metadata and controls
141 lines (121 loc) · 3.07 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
package org.codehaus.jackson.io;
import java.io.*;
/**
* Simple {@link InputStream} implementation that is used to "unwind" some
* data previously read from an input stream; so that as long as some of
* that data remains, it's returned; but as long as it's read, we'll
* just use data from the underlying original stream.
* This is similar to {@link java.io.PushbackInputStream}, but here there's
* only one implicit pushback, when instance is constructed.
*/
public final class MergedStream
extends InputStream
{
final protected IOContext _context;
final InputStream _in;
byte[] _buffer;
int _ptr;
final int _end;
public MergedStream(IOContext context,
InputStream in, byte[] buf, int start, int end)
{
_context = context;
_in = in;
_buffer = buf;
_ptr = start;
_end = end;
}
public int available()
throws IOException
{
if (_buffer != null) {
return _end - _ptr;
}
return _in.available();
}
public void close()
throws IOException
{
freeMergedBuffer();
_in.close();
}
public void mark(int readlimit)
{
if (_buffer == null) {
_in.mark(readlimit);
}
}
public boolean markSupported()
{
// Only supports marks past the initial rewindable section...
return (_buffer == null) && _in.markSupported();
}
public int read()
throws IOException
{
if (_buffer != null) {
int c = _buffer[_ptr++] & 0xFF;
if (_ptr >= _end) {
freeMergedBuffer();
}
return c;
}
return _in.read();
}
public int read(byte[] b)
throws IOException
{
return read(b, 0, b.length);
}
public int read(byte[] b, int off, int len)
throws IOException
{
if (_buffer != null) {
int avail = _end - _ptr;
if (len > avail) {
len = avail;
}
System.arraycopy(_buffer, _ptr, b, off, len);
_ptr += len;
if (_ptr >= _end) {
freeMergedBuffer();
}
return len;
}
return _in.read(b, off, len);
}
public void reset()
throws IOException
{
if (_buffer == null) {
_in.reset();
}
}
public long skip(long n)
throws IOException
{
long count = 0L;
if (_buffer != null) {
int amount = _end - _ptr;
if (amount > n) { // all in pushed back segment?
_ptr += (int) n;
return n;
}
freeMergedBuffer();
count += amount;
n -= amount;
}
if (n > 0) {
count += _in.skip(n);
}
return count;
}
private void freeMergedBuffer()
{
byte[] buf = _buffer;
if (buf != null) {
_buffer = null;
_context.releaseReadIOBuffer(buf);
}
}
}