簡體   English   中英

java GZIPInputStream和GZIPOutputStream無法按預期工作

[英]java GZIPInputStream and GZIPOutputStream not working as expected

我試圖將一系列Long寫入GZIPOutputStream ,以期稍后解壓縮數字。
當我嘗試使用少量Long時,以下程序可以正常工作,但是它將拋出許多Long的異常,例如(10240)。

請查看並運行以下代碼:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GzipTest {
    private static final String filename = "data.gz";

    public static void main(String... args) throws Exception {
        test(32);  // OK
        test(10240);  // Why won't this work? (unexpected read: 6)
    }

    public static void test(int max) throws Exception {
        System.out.println("testing with " + max);
        FileOutputStream fos = new FileOutputStream(filename);
        GZIPOutputStream gos = new GZIPOutputStream(fos, 4096);
        long num = 10241000L;
        for (int i = 0; i < max; ++i) {
            gos.write(long2bytes(num + i));
        }
        gos.close();
        fos.close();

        GZIPInputStream gis = new GZIPInputStream(new FileInputStream(filename), 4096);
        byte[] buf = new byte[Long.BYTES];
        for (int i = 0; i < max; ++i) {
            int nread = gis.read(buf);
            if (nread != Long.BYTES) {
                throw new RuntimeException("unexpected read: " + nread);
            }
            long value = bytes2long(buf);
            if (value != num + i) {
                throw new RuntimeException("unexpected value: " + value);
            }
        }
        gis.close();
    }

    static byte[] long2bytes(long num) {
        ByteBuffer buf = ByteBuffer.allocate(Long.BYTES);
        buf.putLong(num);
        return buf.array();
    }

    static long bytes2long(byte[] bytes) {
        return ByteBuffer.wrap(bytes).getLong();
    }
}

輸出:

testing with 32
testing with 10240
Exception in thread "main" java.lang.RuntimeException: unexpected read: 6
    at GzipTest.test(GzipTest.java:31)
    at GzipTest.main(GzipTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

如果您進行了彼得·勞瑞(Peter Lawrey)提出的更改,則代碼可以正常工作:

 try (DataInputStream in = new DataInputStream(new GZIPInputStream(new FileInputStream(filename), 4096))) {
        byte[] buf = new byte[Long.BYTES];
        for (int i = 0; i < max; ++i) {
            in.readFully(buf);
            long value = bytes2long(buf);
            if (value != num + i) {
                throw new RuntimeException("unexpected value: " + value);
            }
        }
    }

或者,您可以將所有內容簡化為:

 try (DataInputStream in = new DataInputStream(new GZIPInputStream(new FileInputStream(filename), 4096))) {
        for (int i = 0; i < max; ++i) {
            long value = in.readLong();
            if (value != num + i) {
                throw new RuntimeException("unexpected value: " + value);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM