-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathRunLengthInputStream.java
More file actions
86 lines (78 loc) · 2.14 KB
/
RunLengthInputStream.java
File metadata and controls
86 lines (78 loc) · 2.14 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
package uk.ac.bristol.star.cdf.record;
import java.io.IOException;
import java.io.InputStream;
import uk.ac.bristol.star.cdf.CdfFormatException;
/**
* Decompression stream for CDF's version of Run Length Encoding.
*
* <p>The compressed stream is just like the uncompressed one,
* except that a byte with the special value V is followed by
* a byte giving the number of additional bytes V to consider present
* in the stream.
* Thus the compressed stream:
* <blockquote>
* 1 2 3 0 0 4 5 6 0 2
* </blockquote>
* is decompressed as
* <blockquote>
* 1 2 3 0 4 5 6 0 0 0
* </blockquote>
* (assuming a special value V=0).
*
* <p>This format was deduced from reading the cdfrle.c source file
* from the CDF distribution.
*
* @author Mark Taylor
* @since 17 May 2013
*/
class RunLengthInputStream extends InputStream {
private final InputStream base_;
private final int rleVal_;
private int vCount_;
/**
* Constructor.
*
* @param base input stream containing RLE-compressed data
* @param rleVal the byte value whose run lengths are compressed
* (always zero for CDF as far as I can tell)
*/
public RunLengthInputStream( InputStream base, byte rleVal ) {
base_ = base;
rleVal_ = rleVal & 0xff;
}
@Override
public int read() throws IOException {
if ( vCount_ > 0 ) {
vCount_--;
return rleVal_;
}
else {
int b = base_.read();
if ( b == rleVal_ ) {
int c = base_.read();
if ( c >= 0 ) {
vCount_ = c;
return rleVal_;
}
else {
throw new CdfFormatException( "Bad RLE data" );
}
}
else {
return b;
}
}
}
@Override
public int available() throws IOException {
return base_.available() + vCount_;
}
@Override
public void close() throws IOException {
base_.close();
}
@Override
public boolean markSupported() {
return false;
}
}