From e2ea3cd6f98f7f7a2dbd851376a91dd95e6cb219 Mon Sep 17 00:00:00 2001 From: dredmond Date: Thu, 1 Oct 2020 11:00:04 -0400 Subject: [PATCH 1/4] Replace StringBuffer usage with StringBuilder for thread safety --- .../java/org/kamranzafar/jtar/TarEntry.java | 6 +-- .../java/org/kamranzafar/jtar/TarHeader.java | 38 +++++++++---------- .../java/org/kamranzafar/jtar/TarUtils.java | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/kamranzafar/jtar/TarEntry.java b/src/main/java/org/kamranzafar/jtar/TarEntry.java index 674ae4e..09f2539 100755 --- a/src/main/java/org/kamranzafar/jtar/TarEntry.java +++ b/src/main/java/org/kamranzafar/jtar/TarEntry.java @@ -87,7 +87,7 @@ public String getName() { } public void setName(String name) { - header.name = new StringBuffer(name); + header.name = new StringBuilder(name); } public int getUserId() { @@ -111,7 +111,7 @@ public String getUserName() { } public void setUserName(String userName) { - header.userName = new StringBuffer(userName); + header.userName = new StringBuilder(userName); } public String getGroupName() { @@ -119,7 +119,7 @@ public String getGroupName() { } public void setGroupName(String groupName) { - header.groupName = new StringBuffer(groupName); + header.groupName = new StringBuilder(groupName); } public void setIds(int userId, int groupId) { diff --git a/src/main/java/org/kamranzafar/jtar/TarHeader.java b/src/main/java/org/kamranzafar/jtar/TarHeader.java index c886085..def6f2f 100755 --- a/src/main/java/org/kamranzafar/jtar/TarHeader.java +++ b/src/main/java/org/kamranzafar/jtar/TarHeader.java @@ -106,7 +106,7 @@ public class TarHeader { public static final int USTAR_FILENAME_PREFIX = 155; // Header values - public StringBuffer name; + public StringBuilder name; public int mode; public int userId; public int groupId; @@ -114,19 +114,19 @@ public class TarHeader { public long modTime; public int checkSum; public byte linkFlag; - public StringBuffer linkName; - public StringBuffer magic; // ustar indicator and version - public StringBuffer userName; - public StringBuffer groupName; + public StringBuilder linkName; + public StringBuilder magic; // ustar indicator and version + public StringBuilder userName; + public StringBuilder groupName; public int devMajor; public int devMinor; - public StringBuffer namePrefix; + public StringBuilder namePrefix; public TarHeader() { - this.magic = new StringBuffer(TarHeader.USTAR_MAGIC); + this.magic = new StringBuilder(TarHeader.USTAR_MAGIC); - this.name = new StringBuffer(); - this.linkName = new StringBuffer(); + this.name = new StringBuilder(); + this.linkName = new StringBuilder(); String user = System.getProperty("user.name", ""); @@ -135,9 +135,9 @@ public TarHeader() { this.userId = 0; this.groupId = 0; - this.userName = new StringBuffer(user); - this.groupName = new StringBuffer(""); - this.namePrefix = new StringBuffer(); + this.userName = new StringBuilder(user); + this.groupName = new StringBuilder(""); + this.namePrefix = new StringBuilder(); } /** @@ -151,8 +151,8 @@ public TarHeader() { * The number of header bytes to parse. * @return The header's entry name. */ - public static StringBuffer parseName(byte[] header, int offset, int length) { - StringBuffer result = new StringBuffer(length); + public static StringBuilder parseName(byte[] header, int offset, int length) { + StringBuilder result = new StringBuilder(length); int end = offset + length; for (int i = offset; i < end; ++i) { @@ -175,7 +175,7 @@ public static StringBuffer parseName(byte[] header, int offset, int length) { * The number of header bytes to parse. * @return The number of bytes in a header's entry name. */ - public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) { + public static int getNameBytes(StringBuilder name, byte[] buf, int offset, int length) { int i; for (i = 0; i < length && i < name.length(); ++i) { @@ -207,14 +207,14 @@ public static TarHeader createHeader(String entryName, long size, long modTime, name = TarUtils.trim(name.replace(File.separatorChar, '/'), '/'); TarHeader header = new TarHeader(); - header.linkName = new StringBuffer(""); + header.linkName = new StringBuilder(""); header.mode = permissions; if (name.length() > 100) { - header.namePrefix = new StringBuffer(name.substring(0, name.lastIndexOf('/'))); - header.name = new StringBuffer(name.substring(name.lastIndexOf('/') + 1)); + header.namePrefix = new StringBuilder(name.substring(0, name.lastIndexOf('/'))); + header.name = new StringBuilder(name.substring(name.lastIndexOf('/') + 1)); } else { - header.name = new StringBuffer(name); + header.name = new StringBuilder(name); } if (dir) { header.linkFlag = TarHeader.LF_DIR; diff --git a/src/main/java/org/kamranzafar/jtar/TarUtils.java b/src/main/java/org/kamranzafar/jtar/TarUtils.java index 21368c0..71b7de2 100755 --- a/src/main/java/org/kamranzafar/jtar/TarUtils.java +++ b/src/main/java/org/kamranzafar/jtar/TarUtils.java @@ -71,7 +71,7 @@ private static long entrySize(long fileSize) { } public static String trim(String s, char c) { - StringBuffer tmp = new StringBuffer(s); + StringBuilder tmp = new StringBuilder(s); for (int i = 0; i < tmp.length(); i++) { if (tmp.charAt(i) != c) { break; From 3518eceb3b3ceac9d3a42d7ef3e5de1957370f4a Mon Sep 17 00:00:00 2001 From: dredmond Date: Thu, 1 Oct 2020 11:01:12 -0400 Subject: [PATCH 2/4] Simplify an if statement, use Java8 for loop simplify for statement to while --- src/main/java/org/kamranzafar/jtar/TarEntry.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/kamranzafar/jtar/TarEntry.java b/src/main/java/org/kamranzafar/jtar/TarEntry.java index 09f2539..a59dd83 100755 --- a/src/main/java/org/kamranzafar/jtar/TarEntry.java +++ b/src/main/java/org/kamranzafar/jtar/TarEntry.java @@ -162,8 +162,7 @@ public boolean isDirectory() { if (header.linkFlag == TarHeader.LF_DIR) return true; - if (header.name.toString().endsWith("/")) - return true; + return header.name.toString().endsWith("/"); } return false; @@ -183,8 +182,8 @@ public void extractTarHeader(String entryName) { public long computeCheckSum(byte[] buf) { long sum = 0; - for (int i = 0; i < buf.length; ++i) { - sum += 255 & buf[i]; + for (byte b : buf) { + sum += 255 & b; } return sum; @@ -220,8 +219,9 @@ public void writeEntryHeader(byte[] outbuf) { offset = Octal.getOctalBytes(header.devMinor, outbuf, offset, TarHeader.USTAR_DEVLEN); offset = TarHeader.getNameBytes(header.namePrefix, outbuf, offset, TarHeader.USTAR_FILENAME_PREFIX); - for (; offset < outbuf.length;) + while (offset < outbuf.length) { outbuf[offset++] = 0; + } long checkSum = this.computeCheckSum(outbuf); From 2cf936a594c3318c9eff86367ca7093aa2a21879 Mon Sep 17 00:00:00 2001 From: dredmond Date: Thu, 1 Oct 2020 11:01:47 -0400 Subject: [PATCH 3/4] Fix dangling Javadoc (should be regular comment) --- .../java/org/kamranzafar/jtar/TarEntry.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/kamranzafar/jtar/TarEntry.java b/src/main/java/org/kamranzafar/jtar/TarEntry.java index a59dd83..8adf639 100755 --- a/src/main/java/org/kamranzafar/jtar/TarEntry.java +++ b/src/main/java/org/kamranzafar/jtar/TarEntry.java @@ -1,18 +1,18 @@ -/** - * Copyright 2012 Kamran Zafar - * - * Licensed 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. - * +/* + * Copyright 2012 Kamran Zafar + * + * Licensed 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.kamranzafar.jtar; From 669845764ae730049a2b0d34de13c8f07c5447af Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 14 Oct 2020 00:36:39 +0000 Subject: [PATCH 4/4] Bump junit from 4.8.1 to 4.13.1 Bumps [junit](https://github.com/junit-team/junit4) from 4.8.1 to 4.13.1. - [Release notes](https://github.com/junit-team/junit4/releases) - [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.13.1.md) - [Commits](https://github.com/junit-team/junit4/compare/r4.8.1...r4.13.1) Signed-off-by: dependabot[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 pom.xml diff --git a/pom.xml b/pom.xml old mode 100755 new mode 100644 index 53fe4d8..cefd9c3 --- a/pom.xml +++ b/pom.xml @@ -99,7 +99,7 @@ junit junit - 4.8.1 + 4.13.1 test