From c4418bf0e58089e803816cf653e483581b7cc379 Mon Sep 17 00:00:00 2001 From: Martin Mirchev Date: Wed, 18 Nov 2026 09:19:37 +0800 Subject: [PATCH] COMPRESS-734: Write all bytes when a channel writes only part of a buffer SevenZOutputFile ignored the return value of SeekableByteChannel.write and counted every byte as written, so a channel that writes fewer bytes than requested produced a corrupt archive without an error. Loop until the buffer is written, as ZipIoUtil.writeAll does for ZIP. Assisted-By: Claude Code:claude-opus-5-5 --- .../archivers/sevenz/SevenZOutputFile.java | 31 +++++-- .../sevenz/SevenZOutputFileTest.java | 80 +++++++++++++++++++ 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java index 136fa3e3f36..4a622bfc9e9 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java @@ -90,11 +90,11 @@ public void write(final byte[] b) throws IOException { public void write(final byte[] b, final int off, final int len) throws IOException { IOUtils.checkFromIndexSize(b, off, len); if (len > BUF_SIZE) { - channel.write(ByteBuffer.wrap(b, off, len)); + writeFully(ByteBuffer.wrap(b, off, len)); } else { buffer.clear(); buffer.put(b, off, len).flip(); - channel.write(buffer); + writeFully(buffer); } compressedCrc32.update(b, off, len); fileBytesWritten += len; @@ -104,7 +104,7 @@ public void write(final byte[] b, final int off, final int len) throws IOExcepti public void write(final int b) throws IOException { buffer.clear(); buffer.put((byte) b).flip(); - channel.write(buffer); + writeFully(buffer); compressedCrc32.update(b); fileBytesWritten++; } @@ -118,6 +118,27 @@ private static Iterable reverse(final Iterable i) { return l; } + /** + * Writes all remaining bytes of a buffer to the channel, which may write fewer bytes than requested in one call. + * + * @param buf the bytes to write. + * @throws IOException if an I/O error occurs or the channel reports a negative byte count. + */ + private void writeFully(final ByteBuffer buf) throws IOException { + while (buf.hasRemaining()) { + final int remaining = buf.remaining(); + final int written = channel.write(buf); + if (written == 0) { + // A non-blocking channel + Thread.yield(); + continue; + } + if (written < 0) { + throw new ArchiveException("Failed to write all bytes in the buffer for channel = %s, length = %,d, written = %,d", channel, remaining, written); + } + } + } + private final SeekableByteChannel channel; private final List files = new ArrayList<>(); private int numNonEmptyStreams; @@ -306,7 +327,7 @@ public void finish() throws IOException { writeHeader(header); header.flush(); final byte[] headerBytes = headerBaos.toByteArray(); - channel.write(ByteBuffer.wrap(headerBytes)); + writeFully(ByteBuffer.wrap(headerBytes)); final CRC32 crc32 = new CRC32(); crc32.update(headerBytes); @@ -331,7 +352,7 @@ public void finish() throws IOException { crc32.update(bb.array(), SevenZFile.SIGNATURE.length + 6, 20); bb.putInt(SevenZFile.SIGNATURE.length + 2, (int) crc32.getValue()); bb.flip(); - channel.write(bb); + writeFully(bb); } private Iterable getContentMethods(final SevenZArchiveEntry entry) throws ArchiveException { diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java index c16e471fba1..d9471906bde 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java @@ -30,6 +30,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.SeekableByteChannel; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.attribute.FileTime; @@ -46,6 +48,7 @@ import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.utils.SeekableInMemoryByteChannel; import org.apache.commons.compress.utils.TimeUtilsTest; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.channels.ByteArraySeekableByteChannel; import org.apache.commons.lang3.ArrayUtils; import org.junit.jupiter.api.Test; @@ -695,4 +698,81 @@ private Boolean verifyFile(final SevenZFile archive, final int index, final int private Boolean verifyFile(final SevenZFile archive, final int index, final Iterable methods) throws Exception { return verifyFile(archive, index, 1, methods); } + + /** + * Tests that an archive written to a channel that accepts at most 1 KiB per write call reads back intact. + */ + @Test + void testPartialChannelWrites() throws Exception { + final ByteArraySeekableByteChannel output = new ByteArraySeekableByteChannel(); + final SeekableByteChannel partial = new SeekableByteChannel() { + + @Override + public void close() throws IOException { + output.close(); + } + + @Override + public boolean isOpen() { + return output.isOpen(); + } + + @Override + public long position() throws IOException { + return output.position(); + } + + @Override + public SeekableByteChannel position(final long newPosition) throws IOException { + output.position(newPosition); + return this; + } + + @Override + public int read(final ByteBuffer dst) throws IOException { + return output.read(dst); + } + + @Override + public long size() throws IOException { + return output.size(); + } + + @Override + public SeekableByteChannel truncate(final long size) throws IOException { + output.truncate(size); + return this; + } + + @Override + public int write(final ByteBuffer src) throws IOException { + final ByteBuffer slice = src.duplicate(); + slice.limit(slice.position() + Math.min(1024, slice.remaining())); + final int written = output.write(slice); + src.position(src.position() + written); + return written; + } + }; + final byte[] data = new byte[100_000]; + for (int i = 0; i < data.length; i++) { + data[i] = (byte) (i * 31 + i / 256); + } + try (SevenZOutputFile outArchive = new SevenZOutputFile(partial)) { + outArchive.setContentCompression(SevenZMethod.COPY); + final SevenZArchiveEntry entry = new SevenZArchiveEntry(); + entry.setName("data.bin"); + outArchive.putArchiveEntry(entry); + outArchive.write(data); + outArchive.write(data, 0, 100); + outArchive.closeArchiveEntry(); + } + try (SevenZFile archive = SevenZFile.builder().setByteArray(output.toByteArray()).get()) { + final SevenZArchiveEntry entry = archive.getNextEntry(); + assertEquals("data.bin", entry.getName()); + final byte[] read = new byte[data.length + 100]; + assertEquals(read.length, IOUtils.read(archive.getInputStream(entry), read)); + assertArrayEquals(data, Arrays.copyOf(read, data.length)); + assertArrayEquals(Arrays.copyOf(data, 100), Arrays.copyOfRange(read, data.length, read.length)); + } + } }