java.util.zip中Deflater类的setLevel()函数将当前压缩级别设置为指定值。压缩级别是从0到9的整数值。函数签名:
public void setLevel(int level)
用法:
d.setLevel(int);
参数:该函数需要一个整数值,该整数值是指定的压缩值
返回类型:该函数不返回任何值。
异常:如果压缩级别无效,该函数将抛出IllegalArgumentException。
放气水平的一些恒定值:
- BEST_COMPRESSION:压缩级别以获得最佳压缩
- BEST_SPEED:压缩级别,可实现最快的压缩。
- DEFAULT_COMPRESSION:默认压缩级别。
- NO_COMPRESSION:无压缩的压缩级别。
范例1:
// Java program to describe the use
// of setLevel() function
import java.util.zip.*;
import java.io.UnsupportedEncodingException;
class GFG {
// Function to compress the string to the given level
static void compression(int level, String text)
throws UnsupportedEncodingException
{
// deflater
Deflater d = new Deflater(level);
// set the Input for deflator
d.setInput(text.getBytes("UTF-8"));
// finish
d.finish();
// output bytes
byte output[] = new byte[1024];
// compress the data
int size = d.deflate(output);
// compressed String
System.out.println("Compressed String with level ="
+ level + ":"
+ new String(output)
+ "\n Size " + size);
d.end();
}
// Driver code
public static void main(String args[])
throws UnsupportedEncodingException
{
// get the text
String pattern = "GeeksforGeeks", text = "";
// generate the text
for (int i = 0; i < 4; i++)
text += pattern;
// original String
System.out.println("Original String:" + text
+ "\n Size " + text.length());
// default
compression(Deflater.DEFAULT_COMPRESSION, text);
// no compression
compression(Deflater.NO_COMPRESSION, text);
// Best compression
compression(Deflater.BEST_COMPRESSION, text);
// Best Speed
compression(Deflater.BEST_SPEED, text);
}
}
输出:
Original String:GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks Size 52 Compressed String with level =-1:x?sOM?.N?/r???q?? Size 21 Compressed String with level =0:x4??GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks?? Size 63 Compressed String with level =9:x?sOM?.N?/r???q?? Size 21 Compressed String with level =1:xsOM?.N?/r?`?0?? Size 22
参考: https://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#setLevel()
相关用法
- Java Deflater deflate()用法及代码示例
- Java Deflater finished()用法及代码示例
- Java Deflater needsInput()用法及代码示例
- Java Deflater getTotalIn()用法及代码示例
- Java Deflater setInput()用法及代码示例
- Java Deflater getTotalOut()用法及代码示例
- Java Deflater getBytesWritten()用法及代码示例
- Java Deflater getBytesRead()用法及代码示例
- Java Deflater getAdler()用法及代码示例
- Java Logger setLevel()用法及代码示例
- Java LogRecord setLevel()用法及代码示例
- Java Java.util.function.LongPredicate用法及代码示例
- Java Java.util.function.DoublePredicate用法及代码示例
- Java Java.util.function.BiPredicate用法及代码示例
- Java Java.util.function.IntPredicate用法及代码示例
注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 Deflater setLevel() function in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。