当前位置: 首页 > 知识库问答 >
问题:

GZIPInputStream。阅读()限制

濮翰学
2023-03-14

我试图在Java中使用GZIPInputStream来解压缩来自我的。NET应用程序(它以GZIP格式压缩)的图像数据。图像数据以Base64字符串传输,因为它是作为XML文本同步的。我假设流会读取到传入. read()参数的缓冲区中,直到缓冲区饱和。它没有像我预期的那样工作,也就是说,. read()每次读取操作只能读取800html" target="_blank">字节左右。这对于小图像不是问题,但对于大图像,如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+BKCE4Puz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N/ngYt4QkE4CFRHgMYQUBsEKou92PR8GwZe5k6caYe95Q05cOkfBAIY9gu3z7tPSw4/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);
}

}

b64img是此图像的base64表示形式。这是一个简单的6KB图像,所以运行这个程序不会花费太长时间。如果您逐步完成读取过程,您将看到它不会使解压缩后的字节缓冲区饱和。

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

共有1个答案

罗寒
2023-03-14

斯卡文是对的。可以使用GZIPInputStream的构造函数设置缓冲区大小。

 类似资料:
  • 问题内容: 我有一个.gz格式的文件。读取此文件的Java类是GZIPInputStream。但是,此类不会扩展Java的BufferedReader类。结果,我无法逐行读取文件。我需要这样的东西 尽管我创建了扩展java的Reader或BufferedReader类并使用GZIPInputStream作为其变量之一的类。 但是,这在我使用时不起作用 有人可以建议如何进行.. 问题答案: 装饰器的

  • 介绍 (Introduction) java.util.zip.GZIPInputStream类实现了一个流过滤器,用于读取GZIP文件格式的压缩数据。 类声明 以下是java.util.zip.GZIPInputStream类的声明 - public class GZIPInputStream extends InflaterInputStream 字段 (Fields) 以下是java

  • 问题内容: 为了授予队列管理器的 所有对象 (队列,通道等)的读取权限以监视视角,需要什么命令/权限。我的监视客户端使用Java api。 威尔用于此目的的权限做。可 用于设置此权限,但是此命令具有+ put,+ get等选项,而不是MQZAO_ALL_ADMIN。目前,我正在为每个队列等使用8.0.0.4版。 问题答案: 对于只读用户,您不想使用它,因为这将授予管理权限。在这方面,并提供队列。

  • 我有以下类,它从/到包裹读取和写入对象数组: 在上面的代码中,我在读取< code>readParcelableArray时得到一个< code>ClassCastException: 错误/AndroidRuntime(5880):原因:Java . lang . classcastexception:[land roid . OS . parcelable; 上面的代码有什么错误?在编写对象数

  • 问题内容: 我正在使用新类来处理异步HTTP请求和响应交换,并且我试图找到一种方法使BodySubscriber处理不同的编码类型,例如gzip。 但是,映射a 以便基础流被a包裹(当在响应标头中找到“ Content-Encoding:gzip”时)会导致挂起。没有例外,只是完全停止活动。 映射外观的代码如下所示: 接收到具有“ gzip”编码的HTTP响应,控制台将显示以下内容: 输入了Enc

  • 在问题[1]中,我了解到如果您想在Android下使用NFC标签,则不必采用NDEF格式。我想在Win 8.1 in. Net下执行此操作。我的情况是这样的: 我有一个RFID卡Mifare Classic 1K,其中存储了一个ID。(由制造商记录)该ID由我们的考勤系统通过通常的RFID读取器(例如Gigatek的PROMAG MFR120)读取。我们不在卡上写任何东西,我们只需要读取ID。但是