1

现在,我正在使用纯 ObjectOutputStream 通过套接字传递对象。有时,这些对象会变得非常大并导致 OutOfMemory 问题。我相信这些对象可以被压缩,因为它们内部通常有很多重复的数据。所以,我试图用以下方式包装我的套接字:

  static class CompressedSocket extends Socket {
    private ZipInputStream zis;
    private ZipOutputStream zos;

    CompressedSocket(InetAddress inetAddress, int port) throws IOException {
      super(inetAddress, port);
      zis = new ZipInputStream(super.getInputStream());
      zos = new ZipOutputStream(super.getOutputStream());
    }

    @Override
    public ZipInputStream getInputStream() throws IOException {
      return zis;
    }

    @Override
    public ZipOutputStream getOutputStream() throws IOException {
      return zos;
    }
  }

但是,在我将套接字包装在 ObjectInput/OutputStreams 中的课堂部分中,出现以下错误。以下是相关行:

ObjectInputStream in = null;
ObjectOutputStream out = null;
Object serverResponse = null;
CompressedSocket socket = null;
try {
    socket = new CompressedSocket(InetAddress.getByName(ipAddress), SixDofServer.PORT);
} catch ( ... ) { ... }

try {
    out = new ObjectOutputStream(socket.getOutputStream());
    in = new ObjectInputStream(socket.getInputStream());
} catch ( ... ) { ... }

出现的错误是:

java.util.zip.ZipException: no current ZIP entry
    at java.util.zip.ZipOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    at java.io.ObjectOutputStream.<init>(Unknown Source)

所以,很明显我做的不对。但我发现谷歌和其他指南帮助不大。

现在,如果事实证明包装套接字流不是一种选择,无论出于何种原因,我仍然想压缩我一开始提到的那个对象。关于如何在本地压缩该对象,然后通过常规 ObjectOutputStream 发送它的任何建议?

4

0 回答 0