|
29 | 29 | import com.bc.zarr.storage.Store; |
30 | 30 | import ucar.ma2.Array; |
31 | 31 | import ucar.ma2.DataType; |
| 32 | +import ucar.ma2.IndexIterator; |
32 | 33 |
|
33 | 34 | import javax.imageio.stream.ImageInputStream; |
34 | 35 | import javax.imageio.stream.ImageOutputStream; |
@@ -69,18 +70,44 @@ public Array read(String storeKey) throws IOException { |
69 | 70 | } |
70 | 71 | } |
71 | 72 |
|
| 73 | + protected boolean isFillOnly(Array array) { |
| 74 | + if (fill == null) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + final IndexIterator iter = array.getIndexIterator(); |
| 78 | + final double fillValue = fill.doubleValue(); |
| 79 | + if (Double.isNaN(fillValue)) { |
| 80 | + while (iter.hasNext()) { |
| 81 | + if (! Double.isNaN(iter.getDoubleNext())) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + } else { |
| 86 | + while (iter.hasNext()) { |
| 87 | + if (iter.getDoubleNext() != fillValue) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
72 | 95 | @Override |
73 | 96 | public void write(String storeKey, Array array) throws IOException { |
74 | | - try ( |
75 | | - final ImageOutputStream iis = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); |
76 | | - final InputStream is = new ZarrInputStreamAdapter(iis); |
77 | | - final OutputStream os = store.getOutputStream(storeKey) |
78 | | - ) { |
79 | | - final double[] doubles = (double[]) array.get1DJavaArray(DataType.DOUBLE); |
80 | | - iis.setByteOrder(order); |
81 | | - iis.writeDoubles(doubles, 0, doubles.length); |
82 | | - iis.seek(0); |
83 | | - compressor.compress(is, os); |
| 97 | + if (isFillOnly(array)) { |
| 98 | + store.delete(storeKey); |
| 99 | + } else { |
| 100 | + try ( |
| 101 | + final ImageOutputStream iis = new MemoryCacheImageOutputStream(new ByteArrayOutputStream()); |
| 102 | + final InputStream is = new ZarrInputStreamAdapter(iis); |
| 103 | + final OutputStream os = store.getOutputStream(storeKey) |
| 104 | + ) { |
| 105 | + final double[] doubles = (double[]) array.get1DJavaArray(DataType.DOUBLE); |
| 106 | + iis.setByteOrder(order); |
| 107 | + iis.writeDoubles(doubles, 0, doubles.length); |
| 108 | + iis.seek(0); |
| 109 | + compressor.compress(is, os); |
| 110 | + } |
84 | 111 | } |
85 | 112 | } |
86 | 113 | } |
0 commit comments