Skip to content

Should I call flush before closing when using ZstdOutputStream? #335

@ConeyLiu

Description

@ConeyLiu

Hopefully, someone could help on whether we should call flush before closing a ZstdOuputStream? Because this could produce different behavior.

    // compress without flush
    byte[] bytes1 = null;
    ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
    try(ByteArrayOutputStream out = outputStream1;
        ZstdOutputStream zstd = new ZstdOutputStream(out, 3)) {
      zstd.setWorkers(0);

      zstd.write(0);
    }

    bytes1 = outputStream1.toByteArray();
    
    // compress with flush
    byte[] bytes2 = null;
    ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
    try(ByteArrayOutputStream out = outputStream2;
        ZstdOutputStream zstd = new ZstdOutputStream(out, 3)) {
      zstd.setWorkers(0);

      zstd.write(0);

      zstd.flush(); // flush before close
    }

    bytes2 = outputStream2.toByteArray();

    Arrays.equals(bytes1, bytes2);

Call flush could produce extra 3 bytes:
企业微信截图_f8d99e89-c545-482b-8539-d4f821ac121c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions