protected static byte[] getRandom(double compressionRatio, int length) { RandomGenerator gen = new RandomGenerator(compressionRatio); gen.getNextPosition(length); byte[] random = Arrays.copyOf(gen.data, length); assertEquals(random.length, length); return random; }
public RandomGenerator(double compressionRatio) { // We use a limited amount of data over and over again and ensure // that it is larger than the compression window (32KB), and also // large enough to serve all typical value sizes we want to write. Random rnd = new Random(301); data = new byte[1048576 + 100]; for (int i = 0; i < 1048576; i += 100) { // Add a short fragment that is as compressible as specified ratio System.arraycopy(compressibleData(rnd, compressionRatio, 100), 0, data, i, 100); } }
private static byte[] compressibleData(Random random, double compressionRatio, int length) { int raw = (int) (length * compressionRatio); if (raw < 1) { raw = 1; } byte[] rawData = generateRandomData(random, raw); // Duplicate the random data until we have filled "length" bytes byte[] dest = new byte[length]; for (int i = 0; i < length; ) { int chunkLength = Math.min(rawData.length, length - i); System.arraycopy(rawData, 0, dest, i, chunkLength); i += chunkLength; } return dest; }