Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++;
}
Expand All @@ -118,6 +118,27 @@ private static <T> Iterable<T> reverse(final Iterable<T> 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<SevenZArchiveEntry> files = new ArrayList<>();
private int numNonEmptyStreams;
Expand Down Expand Up @@ -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);
Expand All @@ -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<? extends SevenZMethodConfiguration> getContentMethods(final SevenZArchiveEntry entry) throws ArchiveException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<SevenZMethodConfiguration> 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));
}
}
}