Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StringBuffer usages with StringBuilder #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion pom.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/org/kamranzafar/jtar/TarEntry.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -111,15 +111,15 @@ public String getUserName() {
}

public void setUserName(String userName) {
header.userName = new StringBuffer(userName);
header.userName = new StringBuilder(userName);
}

public String getGroupName() {
return header.groupName.toString();
}

public void setGroupName(String groupName) {
header.groupName = new StringBuffer(groupName);
header.groupName = new StringBuilder(groupName);
}

public void setIds(int userId, int groupId) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
38 changes: 19 additions & 19 deletions src/main/java/org/kamranzafar/jtar/TarHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,27 @@ 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;
public long size;
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", "");

Expand All @@ -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();
}

/**
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kamranzafar/jtar/TarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down