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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。