You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/30 15:32:17 UTC
[commons-compress] 06/07: Refactor magic strings
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 1fef07a5741960a9fbc7cf6384457418635ec665
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Nov 30 10:26:09 2022 -0500
Refactor magic strings
---
.../compress/archivers/tar/TarArchiveEntry.java | 19 ++++++------
.../archivers/tar/TarArchiveInputStream.java | 4 +--
.../commons/compress/archivers/tar/TarFile.java | 4 +--
.../compress/archivers/tar/TarGnuSparseKeys.java | 34 ++++++++++++++++++++++
.../commons/compress/archivers/tar/TarUtils.java | 16 +++++-----
5 files changed, 56 insertions(+), 21 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
index 7efc2fa4..a8a60c67 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveEntry.java
@@ -191,6 +191,7 @@ import org.apache.commons.compress.utils.IOUtils;
* @NotThreadSafe
*/
public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamOffsets {
+
private static final TarArchiveEntry[] EMPTY_TAR_ARCHIVE_ENTRY_ARRAY = new TarArchiveEntry[0];
/**
@@ -759,22 +760,22 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
void fillGNUSparse0xData(final Map<String, String> headers) {
paxGNUSparse = true;
- realSize = Integer.parseInt(headers.get("GNU.sparse.size"));
- if (headers.containsKey("GNU.sparse.name")) {
+ realSize = Integer.parseInt(headers.get(TarGnuSparseKeys.SIZE));
+ if (headers.containsKey(TarGnuSparseKeys.NAME)) {
// version 0.1
- name = headers.get("GNU.sparse.name");
+ name = headers.get(TarGnuSparseKeys.NAME);
}
}
void fillGNUSparse1xData(final Map<String, String> headers) throws IOException {
paxGNUSparse = true;
paxGNU1XSparse = true;
- if (headers.containsKey("GNU.sparse.name")) {
- name = headers.get("GNU.sparse.name");
+ if (headers.containsKey(TarGnuSparseKeys.NAME)) {
+ name = headers.get(TarGnuSparseKeys.NAME);
}
- if (headers.containsKey("GNU.sparse.realsize")) {
+ if (headers.containsKey(TarGnuSparseKeys.REALSIZE)) {
try {
- realSize = Integer.parseInt(headers.get("GNU.sparse.realsize"));
+ realSize = Integer.parseInt(headers.get(TarGnuSparseKeys.REALSIZE));
} catch (NumberFormatException ex) {
throw new IOException("Corrupted TAR archive. GNU.sparse.realsize header for "
+ name + " contains non-numeric value");
@@ -1699,10 +1700,10 @@ public class TarArchiveEntry implements ArchiveEntry, TarConstants, EntryStreamO
}
setDevMajor(devMajor);
break;
- case "GNU.sparse.size":
+ case TarGnuSparseKeys.SIZE:
fillGNUSparse0xData(headers);
break;
- case "GNU.sparse.realsize":
+ case TarGnuSparseKeys.REALSIZE:
fillGNUSparse1xData(headers);
break;
case "SCHILY.filetype":
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index b622af6b..d5432a85 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -605,8 +605,8 @@ public class TarArchiveInputStream extends ArchiveInputStream {
final Map<String, String> headers = TarUtils.parsePaxHeaders(this, sparseHeaders, globalPaxHeaders, entrySize);
// for 0.1 PAX Headers
- if (headers.containsKey("GNU.sparse.map")) {
- sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get("GNU.sparse.map")));
+ if (headers.containsKey(TarGnuSparseKeys.MAP)) {
+ sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get(TarGnuSparseKeys.MAP)));
}
getNextEntry(); // Get the actual file entry
if (currEntry == null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
index ce1ef0c4..d9087b8c 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarFile.java
@@ -430,8 +430,8 @@ public class TarFile implements Closeable {
}
// for 0.1 PAX Headers
- if (headers.containsKey("GNU.sparse.map")) {
- sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get("GNU.sparse.map")));
+ if (headers.containsKey(TarGnuSparseKeys.MAP)) {
+ sparseHeaders = new ArrayList<>(TarUtils.parseFromPAX01SparseHeaders(headers.get(TarGnuSparseKeys.MAP)));
}
getNextTarEntry(); // Get the actual file entry
if (currEntry == null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarGnuSparseKeys.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarGnuSparseKeys.java
new file mode 100644
index 00000000..1eefc0c4
--- /dev/null
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarGnuSparseKeys.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress.archivers.tar;
+
+/**
+ * GNU sparse key names.
+ */
+class TarGnuSparseKeys {
+
+ static final String MAP = "GNU.sparse.map";
+ static final String NAME = "GNU.sparse.name";
+ static final String NUMBYTES = "GNU.sparse.numbytes";
+ static final String OFFSET = "GNU.sparse.offset";
+ static final String REALSIZE = "GNU.sparse.realsize";
+ static final String SIZE = "GNU.sparse.size";
+
+}
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
index b0393639..69922133 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarUtils.java
@@ -774,7 +774,7 @@ public class TarUtils {
headers.put(keyword, value);
// for 0.0 PAX Headers
- if (keyword.equals("GNU.sparse.offset")) {
+ if (keyword.equals(TarGnuSparseKeys.OFFSET)) {
if (offset != null) {
// previous GNU.sparse.offset header but but no numBytes
sparseHeaders.add(new TarArchiveStructSparse(offset, 0));
@@ -783,30 +783,30 @@ public class TarUtils {
offset = Long.valueOf(value);
} catch (NumberFormatException ex) {
throw new IOException("Failed to read Paxheader."
- + "GNU.sparse.offset contains a non-numeric value");
+ + TarGnuSparseKeys.OFFSET + " contains a non-numeric value");
}
if (offset < 0) {
throw new IOException("Failed to read Paxheader."
- + "GNU.sparse.offset contains negative value");
+ + TarGnuSparseKeys.OFFSET + " contains negative value");
}
}
// for 0.0 PAX Headers
- if (keyword.equals("GNU.sparse.numbytes")) {
+ if (keyword.equals(TarGnuSparseKeys.NUMBYTES)) {
if (offset == null) {
- throw new IOException("Failed to read Paxheader." +
- "GNU.sparse.offset is expected before GNU.sparse.numbytes shows up.");
+ throw new IOException("Failed to read Paxheader."
+ + TarGnuSparseKeys.OFFSET + " is expected before GNU.sparse.numbytes shows up.");
}
long numbytes;
try {
numbytes = Long.parseLong(value);
} catch (NumberFormatException ex) {
throw new IOException("Failed to read Paxheader."
- + "GNU.sparse.numbytes contains a non-numeric value.");
+ + TarGnuSparseKeys.NUMBYTES + " contains a non-numeric value.");
}
if (numbytes < 0) {
throw new IOException("Failed to read Paxheader."
- + "GNU.sparse.numbytes contains negative value");
+ + TarGnuSparseKeys.NUMBYTES + " contains negative value");
}
sparseHeaders.add(new TarArchiveStructSparse(offset, numbytes));
offset = null;