1 package nom.tam.image.compression.bintable;
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 import java.nio.Buffer;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Future;
37
38 import nom.tam.fits.Header;
39 import nom.tam.fits.HeaderCardException;
40 import nom.tam.fits.compression.algorithm.api.ICompressorControl;
41 import nom.tam.fits.compression.provider.CompressorProvider;
42 import nom.tam.fits.header.Compression;
43 import nom.tam.util.ColumnTable;
44 import nom.tam.util.type.ElementType;
45
46
47
48
49
50 @SuppressWarnings("javadoc")
51 public abstract class BinaryTableTile implements Runnable {
52
53 protected final ColumnTable<?> data;
54
55
56
57
58 protected final int rowStart;
59
60
61
62
63 protected final int rowEnd;
64
65 protected final int column;
66
67 protected String compressionAlgorithm;
68
69 protected final ElementType<Buffer> type;
70
71 protected final int length;
72
73 protected final int tileIndex;
74
75 private Future<?> future;
76
77 public BinaryTableTile(ColumnTable<?> data, BinaryTableTileDescription description) {
78 this.data = data;
79 rowStart = description.getRowStart();
80 rowEnd = description.getRowEnd();
81 column = description.getColumn();
82 tileIndex = description.getTileIndex() - 1;
83 compressionAlgorithm = description.getCompressionAlgorithm();
84 type = ElementType.forClass(data.getElementClass(column));
85 length = (rowEnd - rowStart) * data.getElementSize(column);
86 }
87
88 public void execute(ExecutorService threadPool) {
89 future = threadPool.submit(this);
90 }
91
92
93
94
95 public void fillHeader(Header header) throws HeaderCardException {
96 header.card(Compression.ZCTYPn.n(column + 1)).value(compressionAlgorithm);
97 }
98
99
100
101
102
103
104 public int getTileIndex() {
105 return tileIndex;
106 }
107
108 public void waitForResult() {
109 try {
110 future.get();
111 } catch (Exception e) {
112 throw new IllegalStateException(
113 "could not process tile " + (tileIndex + 1) + ", column " + column + ": " + e.getMessage(), e);
114 }
115 }
116
117 protected ICompressorControl getCompressorControl() {
118 return CompressorProvider.findCompressorControl(null, compressionAlgorithm, type.primitiveClass());
119 }
120
121 protected ICompressorControl getCompressorControl(Class<?> dataType) {
122 return CompressorProvider.findCompressorControl(null, compressionAlgorithm, dataType);
123 }
124
125 protected ICompressorControl getGZipCompressorControl() {
126 return CompressorProvider.findCompressorControl(null, Compression.ZCMPTYPE_GZIP_1, byte.class);
127 }
128
129 protected int getUncompressedSizeInBytes() {
130 return length * type.size();
131 }
132 }