GZIPInputStream .read()限制

时间:2013-12-27 20:14:16

标签: java base64 gzip compression

我正在尝试在Java中使用GZIPInputStream来解压缩来自我的.NET应用程序(以GZIP格式压缩)的图像数据。图像数据作为base64字符串传输,因为它作为XML文本同步。我假设流将读入缓冲区传入.read()参数,直到缓冲区饱和。它没有像我预期的那样工作,即.read()只读取,即每次读取操作800个左右的字节。对于小图像而言这不是问题,但对于大图像,例如800 KB,解压缩需要很长时间。我写了一个小测试样本来展示这个:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPtest {

public static void main(String[] args) throws IOException {
    String b64img = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAVaklEQVR4Xt1bCXSdZZl+7rokN7nZ2zRNmnSzK7Rhaym7FKkUWc6Ao44VHc6cg+MoOmd0Rh1HdBAXUI4z1REdoMoIWgbKIshama600NKmaZMmTZp9ubn35m7/vsz7fuc7uTSldIUqb89zvvx37fu8z7t83594XNfFZPN4PDhVS/wOV5o27jRMXG44iOkmwNAIGRXPJ7L4w6e+hwcB6ARn7RfR5Di4U7ewVDXgVTRsHhjDr3/9IjoAWAQXZ9je7rMfZ9DyT+CztosHfWU1KKmaDo/XAztcDNeyYeQVjPYOrfL3jK164tvWPWMZ/NJyEA/4cE9JxTTU1zcgl0ti/4HeFYD2+c+twl0PPo9fAFAIDs6snXkCnGdRZpj4iX/+cgSqa4F0AsgQshlg2mygNIKK8ghmza7BofahkvaO5Feyqo1lV38Osy/6MlC0EHBUrBz9Xzzz2L+VbNrefe+nV2IWEdRoOygxDHh0A1A0PDeuYMP2NhwEYJ+uQrw4Q2ZauNnjRSwwcyGOMDUDpOOQhgB5NG92BVZdVovLL5yD2Zf+QDgPOwvk2wC1E9dfHMWMacDMGcV3LD1/yqrVq2dccsPq2hWXXly2YtpU/93lxWi9ZB5+BKCE4PuzUIDtYGagYgre0XQFiJbh7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UBsEKou92PR8GwZe5k6caYe95Q05cOkfBAIY9gu3z7tPSw4/lUoZdAN0AGufGsZ3aP3uIz34xC0b8foj5LyT74P64L2FgxSYH4gUkFA3f3nB7bM+89+bveXnfv6ljcGmfFxFfiQPcyyRgNayGanv/1buM3rf7Y+q/h+/Yba2aSiBewAAAABJRU5ErkJggg==";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(bos);
    gzos.write(b64img.getBytes());
    gzos.close();

    byte[] compressed = bos.toByteArray();
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gzis = new GZIPInputStream(bis);
    int bufSize = 8192;
    byte[] decompressed = new byte[bufSize];
    int read = gzis.read(decompressed);
    String s = new String(decompressed, 0, read);
    while (read != -1) {
        decompressed = new byte[bufSize];
        read = gzis.read(decompressed);
        String tmp = "";
        if (read != -1) {
            tmp = new String(decompressed, 0, read);
            s += tmp;
        }
    }
    gzis.close();
    if (b64img.equals(s))
        System.out.print("EQUAL");
    System.exit(0);
}

}

b64imgthis image的base64表示形式。这是一个简单的6 KB映像,因此运行此程序不应该花费很长时间。如果您单步执行读取过程,您将看到它不会使decompressed字节缓冲区饱和。

我使用print语句查看压缩/解压缩的字符串是否相等,作为我自己的测试,看看GZIP是否正常工作。

1 个答案:

答案 0 :(得分:0)

srkavin是正确的。您可以使用GZIPInputStream的构造函数设置缓冲区大小。