1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.common.bytesource;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.apache.commons.imaging.common.BinaryFunctions;
23
24 public abstract class ByteSource {
25 private final String fileName;
26
27 public ByteSource(final String fileName) {
28 this.fileName = fileName;
29 }
30
31 public final InputStream getInputStream(final long start) throws IOException {
32 InputStream is = null;
33 boolean succeeded = false;
34 try {
35 is = getInputStream();
36 BinaryFunctions.skipBytes(is, start);
37 succeeded = true;
38 } finally {
39 if (!succeeded && is != null) {
40 is.close();
41 }
42 }
43 return is;
44 }
45
46 public abstract InputStream getInputStream() throws IOException;
47
48 public byte[] getBlock(final int start, final int length) throws IOException {
49 return getBlock(0xFFFFffffL & start, length);
50 }
51
52 public abstract byte[] getBlock(long start, int length) throws IOException;
53
54 public abstract byte[] getAll() throws IOException;
55
56
57
58
59
60
61
62
63 public abstract long getLength() throws IOException;
64
65 public abstract String getDescription();
66
67 public final String getFileName() {
68 return fileName;
69 }
70
71 }