-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUncachedImageInputStream.java
More file actions
79 lines (67 loc) · 2.06 KB
/
UncachedImageInputStream.java
File metadata and controls
79 lines (67 loc) · 2.06 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
/*
* @(#)UncachedImageInputStream.java
*
* 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.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
/**
* An implementation of {@code ImageInputStream} that gets its input from a
* regular {@code InputStream}. No caching is used and thus backward seeking is
* not supported.
*
* @author Werner Randelshofer
* @version $Id: UncachedImageInputStream.java 134 2011-12-02 16:23:00Z werner $
*/
public class UncachedImageInputStream extends ImageInputStreamImpl2 {
private InputStream in;
public UncachedImageInputStream(InputStream in) {
this(in, ByteOrder.BIG_ENDIAN);
}
public UncachedImageInputStream(InputStream in, ByteOrder bo) {
this.in = in;
this.byteOrder=bo;
}
@Override
public int read() throws IOException {
int b = in.read();
if (b >= 0) {
streamPos++;
}
return b;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int count = in.read(b, off, len);
if (count > 0) {
streamPos += count;
}
return count;
}
@Override
public void seek(long pos) throws IOException {
checkClosed();
// This test also covers pos < 0
if (pos < flushedPos) {
throw new IndexOutOfBoundsException("pos < flushedPos!");
}
if (pos < streamPos) {
throw new IndexOutOfBoundsException("pos < streamPos!");
}
this.bitOffset = 0;
while (streamPos < pos) {
long skipped = in.skip(pos - streamPos);
if (skipped < 0) {
throw new EOFException("EOF reached while trying to seek to " + pos);
}
streamPos += skipped;
}
}
}