Deflater.setLevel() does not work as expected for me.
static void test1() throws Exception {
byte[] output = new byte[20];
Deflater compresser = new Deflater();
// compresser.setLevel(Deflater.BEST_COMPRESSION);
compresser.setInput("blah".getBytes("UTF-8"));
compresser.finish();
int len = compresser.deflate(output);
System.out.println("len="+ len+ " " +Arrays.toString(output));
}
The above works ok for me (Java 7), but when I uncomment the compresser.setLevel()
line, it breaks (deflate()
returns 0 bytes). The same happens with any compression level, except for DEFAULT
. More specifically, it only "works" (rather, it's harmless) when the level set is the same that was set (explictly or implicitly, as here) in the constructor - that is to say, it only can be used when it's useless.
See an example at Ideone.
This question points to the same problem, and the accepted answer basically says: do't set the level with the setter, do it in the constructor. Far from satisfactory, IMO - why does setLevel()
exists? Is it broken or are we missing something?