Closed bsura closed 7 years ago
Well, the original idea was that flipByte() ensures that there's always at least 2 free words in the array (and thus we don't need to call expandCheck inside the writeLong). Since all the longs are at most 64 bits long and we should always have >= 65 bits free in the array. Can you provide a test scenario where this check fails?
Here's a main method that runs into out of bounds exception.
` public static void main(String args[]) {
long now = System.currentTimeMillis();
LongArrayOutput output = new LongArrayOutput(500);
GorillaCompressor c = new GorillaCompressor(LocalDateTime.now(ZoneOffset.UTC).truncatedTo(ChronoUnit.HOURS).
toInstant(ZoneOffset.UTC).toEpochMilli(), output);
Map<Long, Double> dps = new TreeMap<>();
int n = 3600;
Random r = new Random();
for(int i=0; i<n; i++) {
dps.put(now + (1000 * i) + r.nextInt(100), 100.0);
}
for(Map.Entry<Long, Double> entry : dps.entrySet()) {
c.addValue(entry.getKey(), entry.getValue());
}
c.close();
LongArrayInput input = new LongArrayInput(output.getLongArray());
GorillaDecompressor dc = new GorillaDecompressor(input);
while(true) {
Pair pair = dc.readPair();
if(pair == null)
break;
if(!dps.containsKey(pair.getTimestamp())) {
System.out.println("There was a mismatch.");
}
}
System.out.println("Done.");
}
`
Fixed in 2.0.1
private void checkAndFlipByte() { // Wish I could avoid this check in most cases... if(bitsLeft == 0) { flipByte(); } }
We only call flipByte() (and in turn expand allocation) when bitsLeft is exactly 0. This might not always be the case. Also when bits > bitsLeft, we call flipByteWithoutExpandCheck and if position has already reached the long array size, then we run into ArrayIndexOutOfBoundsException.