This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 7982818ceb9b70fc6b2a4a3fdcdb6374e4132230 Author: Gary D. Gregory <garydgreg...@gmail.com> AuthorDate: Sat Mar 1 15:12:23 2025 -0500 Better internal API names - Use "All" like in NIO Files APIs - Javadoc --- .../zip/FileRandomAccessOutputStream.java | 2 +- .../SeekableChannelRandomAccessOutputStream.java | 4 +-- .../commons/compress/archivers/zip/ZipIoUtil.java | 32 +++++++++++----------- .../archivers/zip/ZipSplitOutputStream.java | 4 +-- .../compress/archivers/zip/ZipIoUtilTest.java | 20 +++++++------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java index 6a0d69697..556eef821 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java @@ -66,7 +66,7 @@ public synchronized long position() { @Override public synchronized void write(final byte[] b, final int off, final int len) throws IOException { - ZipIoUtil.writeFully(this.channel, ByteBuffer.wrap(b, off, len)); + ZipIoUtil.writeAll(this.channel, ByteBuffer.wrap(b, off, len)); position += len; } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java index 30490ee8c..de8c2eb64 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java @@ -46,7 +46,7 @@ public synchronized long position() throws IOException { @Override public synchronized void write(final byte[] b, final int off, final int len) throws IOException { - ZipIoUtil.writeFully(this.channel, ByteBuffer.wrap(b, off, len)); + ZipIoUtil.writeAll(this.channel, ByteBuffer.wrap(b, off, len)); } @Override @@ -54,7 +54,7 @@ public synchronized void writeFully(final byte[] b, final int off, final int len final long saved = channel.position(); try { channel.position(position); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap(b, off, len)); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap(b, off, len)); } finally { channel.position(saved); } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java index cd2516681..89c525bff 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java @@ -31,38 +31,38 @@ final class ZipIoUtil { /** - * Writes full buffer to channel. + * Writes all bytes in a buffer to a channel at specified position. * - * @param channel channel to write to - * @param buffer buffer to write - * @throws IOException when writing fails or fails to write fully + * @param channel The target channel. + * @param buffer The source bytes. + * @param position The file position at which the transfer is to begin; must be non-negative + * @throws IOException If some I/O error occurs or fails or fails to write all bytes. */ - static void writeFully(final WritableByteChannel channel, final ByteBuffer buffer) throws IOException { - while (buffer.hasRemaining()) { + static void writeAll(final FileChannel channel, final ByteBuffer buffer, final long position) throws IOException { + for (long currentPosition = position; buffer.hasRemaining();) { final int remaining = buffer.remaining(); - final int written = channel.write(buffer); + final int written = channel.write(buffer, currentPosition); if (written <= 0) { throw new IOException("Failed to write all bytes in the buffer for channel=" + channel + ", length=" + remaining + ", written=" + written); } + currentPosition += written; } } /** - * Writes full buffer to channel at specified position. + * Writes all bytes in a buffer to a channel. * - * @param channel channel to write to - * @param buffer buffer to write - * @param position position to write at - * @throws IOException when writing fails or fails to write fully + * @param channel The target channel. + * @param buffer The source bytes. + * @throws IOException If some I/O error occurs or fails or fails to write all bytes. */ - static void writeFullyAt(final FileChannel channel, final ByteBuffer buffer, final long position) throws IOException { - for (long currentPosition = position; buffer.hasRemaining();) { + static void writeAll(final WritableByteChannel channel, final ByteBuffer buffer) throws IOException { + while (buffer.hasRemaining()) { final int remaining = buffer.remaining(); - final int written = channel.write(buffer, currentPosition); + final int written = channel.write(buffer); if (written <= 0) { throw new IOException("Failed to write all bytes in the buffer for channel=" + channel + ", length=" + remaining + ", written=" + written); } - currentPosition += written; } } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java index 21ab2cd2c..6d3b768eb 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java @@ -282,7 +282,7 @@ public void writeFully(final byte[] b, final int off, final int len, final long final Map.Entry<Long, Path> segment = positionToFiles.floorEntry(remainingPosition); final Long segmentEnd = positionToFiles.higherKey(remainingPosition); if (segmentEnd == null) { - ZipIoUtil.writeFullyAt(this.currentChannel, ByteBuffer.wrap(b, remainingOff, remainingLen), remainingPosition - segment.getKey()); + ZipIoUtil.writeAll(this.currentChannel, ByteBuffer.wrap(b, remainingOff, remainingLen), remainingPosition - segment.getKey()); remainingPosition += remainingLen; remainingOff += remainingLen; remainingLen = 0; @@ -309,7 +309,7 @@ private void writeToSegment( final int len ) throws IOException { try (FileChannel channel = FileChannel.open(segment, StandardOpenOption.WRITE)) { - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap(b, off, len), position); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap(b, off, len), position); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java index 038b21772..c485826d3 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java @@ -62,8 +62,8 @@ public void testWriteFully_whenFullAtOnce_thenSucceed() throws IOException { ((ByteBuffer) answer.getArgument(0)).position(6); return 6; }); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); verify(channel, times(2)).write((ByteBuffer) any()); } } @@ -81,8 +81,8 @@ public void testWriteFully_whenFullButPartial_thenSucceed() throws IOException { ((ByteBuffer) answer.getArgument(0)).position(6); return 6; }); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); verify(channel, times(3)).write((ByteBuffer) any()); } } @@ -94,7 +94,7 @@ public void testWriteFully_whenPartial_thenFail() throws IOException { ((ByteBuffer) answer.getArgument(0)).position(3); return 3; }).thenAnswer(answer -> 0); - assertThrows(IOException.class, () -> ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))); + assertThrows(IOException.class, () -> ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))); verify(channel, times(2)).write((ByteBuffer) any()); } } @@ -110,8 +110,8 @@ public void testWriteFullyAt_whenFullAtOnce_thenSucceed() throws IOException { ((ByteBuffer) answer.getArgument(0)).position(6); return 6; }); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); } @@ -132,8 +132,8 @@ public void testWriteFullyAt_whenFullButPartial_thenSucceed() throws IOException ((ByteBuffer) answer.getArgument(0)).position(6); return 6; }); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); + ZipIoUtil.writeAll(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); @@ -148,7 +148,7 @@ public void testWriteFullyAt_whenPartial_thenFail() throws IOException { return 3; }); when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0); - assertThrows(IOException.class, () -> ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); + assertThrows(IOException.class, () -> ZipIoUtil.writeAll(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); verify(channel, times(0)).write((ByteBuffer) any(), eq(25L));