當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Deflater setInput()用法及代碼示例



java.util.zip中Deflater類的setInput()函數用於設置輸入數據以進行壓縮。當needsInput()返回true指示輸入緩衝區為空時,應調用此函數。

函數簽名:

public void setInput(byte[] b)
public void setInput(byte[] b, int offset, int len)

用法:


d.setInput(byte[]);
d.setInput(byte[], int, int);

參數:這些重載函數接受的各種參數是:

  • byte[] b:這是要放氣的輸入數組
  • int offset:這是要從給定數組中讀取值的起始偏移量
  • int length:這是從起始偏移量開始壓縮的最大長度。

返回類型:該函數不返回任何內容。

異常:該函數不會引發任何異常

範例1:

// Java program to describe the use 
// of setInput(byte[] b) function 
  
import java.util.zip.*; 
import java.io.UnsupportedEncodingException; 
  
class GFG { 
    public static void main(String args[]) 
        throws UnsupportedEncodingException 
    { 
        // deflater 
        Deflater d = new Deflater(); 
  
        // get the text 
        String pattern = "GeeksforGeeks", text = ""; 
  
        // generate the text 
        for (int i = 0; i < 4; i++) 
            text += pattern; 
  
        // 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:"
                           + new String(output) 
                           + "\n Size " + size); 
  
        // original String 
        System.out.println("Original String:" + text 
                           + "\n Size " + text.length()); 
  
        // end 
        d.end(); 
    } 
}

輸出:

Compressed String:x?sOM?.N?/r???q??
 Size 21
Original String:GeeksforGeeksGeeksforGeeksGeeksforGeeksGeeksforGeeks
 Size 52

範例2:

// Java program to describe the use 
// of setInput(byte[] b, int offset, int len) function 
  
import java.util.zip.*; 
import java.io.UnsupportedEncodingException; 
  
class GFG { 
    public static void main(String args[]) 
        throws UnsupportedEncodingException 
    { 
        // deflater 
        Deflater d = new Deflater(); 
  
        // get the text 
        String pattern = "GeeksforGeeks", text = ""; 
  
        // generate the text 
        for (int i = 0; i < 4; i++) 
            text += pattern; 
  
        // set the Input for deflator 
        d.setInput(text.getBytes("UTF-8"), 10, 30); 
  
        // 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:"
                           + new String(output) 
                           + "\n Size " + size); 
  
        // end 
        d.end(); 
    } 
}

輸出:

Compressed String:x?K?.vOM?.N?/????
 Size 22

參考: https://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#setInput()



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 Deflater setInput() function in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。