COMPRESS-459 use ZipEncoding consistently
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/c8ee9f78 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/c8ee9f78 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/c8ee9f78 Branch: refs/heads/master Commit: c8ee9f781900f874b075433141de779723b3e110 Parents: 72bfc12 Author: Stefan Bodewig <bode...@apache.org> Authored: Wed Jul 11 18:50:01 2018 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Wed Jul 11 18:50:01 2018 +0200 ---------------------------------------------------------------------- .../archivers/cpio/CpioArchiveEntry.java | 22 +++++++++++++------- .../archivers/cpio/CpioArchiveOutputStream.java | 5 ++--- 2 files changed, 17 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/c8ee9f78/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java index 79e7542..6aeb7ce 100644 --- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java +++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveEntry.java @@ -18,11 +18,13 @@ */ package org.apache.commons.compress.archivers.cpio; +import java.nio.ByteBuffer; import java.io.File; -import java.nio.charset.Charset; +import java.io.IOException; import java.util.Date; import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipEncoding; /** * A cpio archive consists of a sequence of files. There are several types of @@ -468,7 +470,7 @@ public class CpioArchiveEntry implements CpioConstants, ArchiveEntry { * Get the number of bytes needed to pad the header to the alignment boundary. * * @deprecated This method doesn't properly work for multi-byte encodings. And - * creates corrupt archives. Use {@link #getHeaderPadCount(Charset)} + * creates corrupt archives. Use {@link #getHeaderPadCount(ZipEncoding)} * or {@link #getHeaderPadCount(long)} in any case. * @return the number of bytes needed to pad the header (0,1,2,3) */ @@ -480,19 +482,25 @@ public class CpioArchiveEntry implements CpioConstants, ArchiveEntry { /** * Get the number of bytes needed to pad the header to the alignment boundary. * - * @param charset - * The character set used to encode the entry name in the stream. + * @param encoding + * The encoding used to encode the entry name in the stream. * @return the number of bytes needed to pad the header (0,1,2,3) * @since 1.18 */ - public int getHeaderPadCount(Charset charset) { + public int getHeaderPadCount(ZipEncoding encoding) { if (name == null) { return 0; } - if (charset == null) { + if (encoding == null) { return getHeaderPadCount(name.length()); } - return getHeaderPadCount(name.getBytes(charset).length); + try { + final ByteBuffer buf = encoding.encode(name); + return getHeaderPadCount(buf.limit() - buf.position()); + } catch (IOException ex) { + // won't happen as the output stream has already encoded the name without error + throw new RuntimeException("cannot encode " + name, ex); + } } /** http://git-wip-us.apache.org/repos/asf/commons-compress/blob/c8ee9f78/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java index afb57a2..73cf714 100644 --- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java @@ -22,7 +22,6 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; @@ -305,7 +304,7 @@ public class CpioArchiveOutputStream extends ArchiveOutputStream implements writeAsciiLong(name.length + 1L, 8, 16); writeAsciiLong(entry.getChksum(), 8, 16); writeCString(name); - pad(entry.getHeaderPadCount(Charset.forName(encoding))); + pad(entry.getHeaderPadCount(zipEncoding)); } private void writeOldAsciiEntry(final CpioArchiveEntry entry) @@ -368,7 +367,7 @@ public class CpioArchiveOutputStream extends ArchiveOutputStream implements writeBinaryLong(name.length + 1L, 2, swapHalfWord); writeBinaryLong(entry.getSize(), 4, swapHalfWord); writeCString(name); - pad(entry.getHeaderPadCount(Charset.forName(encoding))); + pad(entry.getHeaderPadCount(zipEncoding)); } /*(non-Javadoc)