Skip to content

Commit

Permalink
#220 use Arrays.copyOf() instead of System.arrayCopy()
Browse files Browse the repository at this point in the history
  • Loading branch information
winfriedgerlach committed Nov 28, 2024
1 parent c72367f commit cfe59fb
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 33 deletions.
5 changes: 1 addition & 4 deletions src/main/java/com/ctc/wstx/api/ReaderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -599,10 +599,7 @@ public ReaderConfig createNonShared(SymbolTable sym)
rc.mMaxDtdDepth = mMaxDtdDepth;
rc.mAllowSurrogatePairEntities = mAllowSurrogatePairEntities;
if (mSpecialProperties != null) {
int len = mSpecialProperties.length;
Object[] specProps = new Object[len];
System.arraycopy(mSpecialProperties, 0, specProps, 0, len);
rc.mSpecialProperties = specProps;
rc.mSpecialProperties = Arrays.copyOf(mSpecialProperties, mSpecialProperties.length);
}
return rc;
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/ctc/wstx/api/WriterConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ctc.wstx.api;

import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.HashMap;

import javax.xml.stream.XMLOutputFactory;
Expand Down Expand Up @@ -332,9 +333,7 @@ public WriterConfig createNonShared()
Object[] specProps;

if (mSpecialProperties != null) {
int len = mSpecialProperties.length;
specProps = new Object[len];
System.arraycopy(mSpecialProperties, 0, specProps, 0, len);
specProps = Arrays.copyOf(mSpecialProperties, mSpecialProperties.length);
} else {
specProps = null;
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/ctc/wstx/sr/AttributeCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,7 @@ public ElemAttrs buildAttrOb()
/* 02-Feb-2009, TSa: Must make a copy of the Map array now,
* otherwise could get overwritten.
*/
int amapLen = mAttrMap.length;
int[] amap = new int[amapLen];
// TODO: JDK 1.6 has Arrays.copyOf(), should use with Woodstox 6
System.arraycopy(mAttrMap, 0, amap, 0, amapLen);
int[] amap = Arrays.copyOf(mAttrMap, mAttrMap.length);
return new ElemAttrs(raw, mNonDefCount,
amap, mAttrHashSize, mAttrSpillEnd);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ctc/wstx/sr/StreamScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -2391,8 +2392,7 @@ protected final char[] getNameBuffer(int minSize)
protected final char[] expandBy50Pct(char[] buf)
{
int len = buf.length;
char[] newBuf = new char[len + (len >> 1)];
System.arraycopy(buf, 0, newBuf, 0, len);
char[] newBuf = Arrays.copyOf(buf, len + (len >> 1));
return newBuf;
}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/com/ctc/wstx/util/StringVector.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ctc.wstx.util;

import java.util.Arrays;

/**
* Data container similar {@link java.util.List} (from storage perspective),
* but that can be used in multiple ways. For some uses it acts more like
Expand Down Expand Up @@ -51,8 +53,7 @@ public String[] getInternalArray() {
}

public String[] asArray() {
String[] strs = new String[mSize];
System.arraycopy(mStrings, 0, strs, 0, mSize);
String[] strs = Arrays.copyOf(mStrings, mSize);
return strs;
}

Expand All @@ -74,20 +75,16 @@ public boolean containsInterned(String value) {

public void addString(String str) {
if (mSize == mStrings.length) {
String[] old = mStrings;
int oldSize = old.length;
mStrings = new String[oldSize + (oldSize << 1)];
System.arraycopy(old, 0, mStrings, 0, oldSize);
int oldSize = mStrings.length;
mStrings = Arrays.copyOf(mStrings, oldSize + (oldSize << 1));
}
mStrings[mSize++] = str;
}

public void addStrings(String str1, String str2) {
if ((mSize + 2) > mStrings.length) {
String[] old = mStrings;
int oldSize = old.length;
mStrings = new String[oldSize + (oldSize << 1)];
System.arraycopy(old, 0, mStrings, 0, oldSize);
int oldSize = mStrings.length;
mStrings = Arrays.copyOf(mStrings, oldSize + (oldSize << 1));
}
mStrings[mSize] = str1;
mStrings[mSize+1] = str2;
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/ctc/wstx/util/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.ctc.wstx.util;

import java.util.Arrays;

/**
* This class is a kind of specialized type-safe Map, from char array to
* String value. Specialization means that in addition to type-safety
Expand Down Expand Up @@ -585,14 +587,8 @@ public static int calcHash(String key) {
* change is made to a derived symbol table.
*/
private void copyArrays() {
String[] oldSyms = mSymbols;
int size = oldSyms.length;
mSymbols = new String[size];
System.arraycopy(oldSyms, 0, mSymbols, 0, size);
Bucket[] oldBuckets = mBuckets;
size = oldBuckets.length;
mBuckets = new Bucket[size];
System.arraycopy(oldBuckets, 0, mBuckets, 0, size);
mSymbols = Arrays.copyOf(mSymbols, mSymbols.length);
mBuckets = Arrays.copyOf(mBuckets, mBuckets.length);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/ctc/wstx/util/WordResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,7 @@ public WordResolver construct()
return null;
}

result = new char[mSize];
System.arraycopy(mData, 0, result, 0, mSize);
result = Arrays.copyOf(mData, mSize);
}

return new WordResolver(mWords, result);
Expand Down

0 comments on commit cfe59fb

Please sign in to comment.