描述
這個java.util.zip.GZIPInputStream.read(byte[] buf, int off, int len)方法將未壓縮的數據讀入字節數組。如果 len 不為零,則該方法將阻塞,直到可以解壓縮某些輸入;否則,不讀取任何字節並返回 0。
聲明
以下是聲明java.util.zip.GZIPInputStream.read(byte[] buf, int off, int len)方法。
public int read(byte[] buf, int off, int len) throws IOException
參數
buf- 讀取數據的緩衝區。
off- 目標數組中的起始偏移量 b。
len- 讀取的最大字節數。
返回
讀取的實際字節數,如果到達流的末尾,則為 -1。
異常
NullPointerException − 如果 buf 為空。
IndexOutOfBoundsException − 如果off 為負,len 為負,或者len 大於buf.length - off。
ZipException − 如果壓縮的輸入數據已損壞。
IOException − 如果發生 I/O 錯誤。
示例
下麵的例子展示了 java.util.zip.GZIPInputStream.read(byte[] buf, int off, int len) 方法的用法。
package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.DataFormatException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPInputStreamDemo {
public static void main(String[] args) throws DataFormatException, IOException {
String message = "Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;"
+"Welcome to TutorialsPoint.com;";
System.out.println("Original Message length:" + message.length());
byte[] input = message.getBytes("UTF-8");
// Compress the bytes
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream outputStream = new GZIPOutputStream(arrayOutputStream);
outputStream.write(input);
outputStream.close();
//Read and decompress the data
byte[] readBuffer = new byte[5000];
ByteArrayInputStream arrayInputStream =
new ByteArrayInputStream(arrayOutputStream.toByteArray());
GZIPInputStream inputStream = new GZIPInputStream(arrayInputStream);
int read = inputStream.read(readBuffer,0,readBuffer.length);
inputStream.close();
//Should hold the original (reconstructed) data
byte[] result = Arrays.copyOf(readBuffer, read);
// Decode the bytes into a String
message = new String(result, "UTF-8");
System.out.println("UnCompressed Message length:" + message.length());
}
}
讓我們編譯並運行上麵的程序,這將產生以下結果 -
Original Message length:300 UnCompressed Message length:300
相關用法
- Java java.util.zip.GZIPInputStream.close()用法及代碼示例
- Java java.util.zip.GZIPOutputStream.finish()用法及代碼示例
- Java java.util.zip.GZIPOutputStream.write()用法及代碼示例
- Java java.util.zip.Deflater.end()用法及代碼示例
- Java java.util.zip.Inflater.available()用法及代碼示例
- Java java.util.zip.InflaterOutputStream.finish()用法及代碼示例
- Java java.util.zip.Deflater.setInput()用法及代碼示例
- Java java.util.zip.ZipEntry.getComment()用法及代碼示例
- Java java.util.zip.ZipEntry.getCrc()用法及代碼示例
- Java java.util.zip.Deflater.finished()用法及代碼示例
- Java java.util.zip.DeflaterOutputStream.write()用法及代碼示例
- Java java.util.zip.ZipEntry.getTime()用法及代碼示例
- Java java.util.zip.ZipEntry.setCrc()用法及代碼示例
- Java java.util.zip.DeflaterInputStream.read()用法及代碼示例
- Java java.util.zip.Inflater.finished()用法及代碼示例
- Java java.util.zip.ZipFile.getName()用法及代碼示例
- Java java.util.zip.CheckedOutputStream.write()用法及代碼示例
- Java java.util.zip.ZipEntry.isDirectory()用法及代碼示例
- Java java.util.zip.ZipEntry.hashCode()用法及代碼示例
- Java java.util.zip.Inflater.getBytesWritten()用法及代碼示例
注:本文由純淨天空篩選整理自 java.util.zip.GZIPInputStream.read() Method Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。