diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-04-16 22:11:16 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-04-16 22:11:16 +0000 |
commit | 7daeb4278c5ace8d263db087f8014e779140423d (patch) | |
tree | 2f8a086e508c1842c2e2b641f7d89a1eab850e30 /src/testcases | |
parent | cdefe69aab015c8ca06e8212969f81a4150273a2 (diff) | |
download | poi-7daeb4278c5ace8d263db087f8014e779140423d.tar.gz poi-7daeb4278c5ace8d263db087f8014e779140423d.zip |
Replace Allocate+System.arraycopy with Array.copyOf/Range and IOUtils.safelyClone
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876640 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
16 files changed, 45 insertions, 68 deletions
diff --git a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java index 23745f7fce..c0aa3629ea 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestCFRuleRecord.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator; @@ -85,8 +86,7 @@ public final class TestCFRuleRecord { byte[] serializedRecord = record.serialize(); // Strip header - byte[] recordData = new byte[serializedRecord.length - 4]; - System.arraycopy(serializedRecord, 4, recordData, 0, recordData.length); + byte[] recordData = Arrays.copyOfRange(serializedRecord, 4, serializedRecord.length); // Deserialize record = new CFRuleRecord(TestcaseRecordInputStream.create(CFRuleRecord.sid, recordData)); @@ -109,8 +109,7 @@ public final class TestCFRuleRecord { byte[] serializedRecord = record.serialize(); // Strip header - byte[] recordData = new byte[serializedRecord.length - 4]; - System.arraycopy(serializedRecord, 4, recordData, 0, recordData.length); + byte[] recordData = Arrays.copyOfRange(serializedRecord, 4, serializedRecord.length); // Deserialize record = new CFRule12Record(TestcaseRecordInputStream.create(CFRule12Record.sid, recordData)); @@ -140,8 +139,7 @@ public final class TestCFRuleRecord { byte[] serializedRecord = record.serialize(); // Strip header - byte[] recordData = new byte[serializedRecord.length - 4]; - System.arraycopy(serializedRecord, 4, recordData, 0, recordData.length); + byte[] recordData = Arrays.copyOfRange(serializedRecord, 4, serializedRecord.length); // Deserialize record = new CFRule12Record(TestcaseRecordInputStream.create(CFRule12Record.sid, recordData)); diff --git a/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java b/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java index 0d5bcfbae6..269bbd1adb 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertNotNull; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.hpsf.ClassID; import org.apache.poi.hpsf.ClassIDPredefined; @@ -262,7 +263,7 @@ public final class TestHyperlinkRecord { /** * From Bugzilla 47498 */ - private static byte[] data_47498 = { + private static final byte[] data_47498 = { 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xD0, (byte)0xC9, (byte)0xEA, 0x79, (byte)0xF9, (byte)0xBA, (byte)0xCE, 0x11, (byte)0x8C, (byte)0x82, 0x00, (byte)0xAA, 0x00, 0x4B, (byte)0xA9, 0x0B, 0x02, 0x00, @@ -388,8 +389,7 @@ public final class TestHyperlinkRecord { link.setAddress("http://www.lakings.com/"); byte[] tmp = link.serialize(); - byte[] ser = new byte[tmp.length-4]; - System.arraycopy(tmp, 4, ser, 0, ser.length); + byte[] ser = Arrays.copyOfRange(tmp, 4, tmp.length); assertEquals(data1.length, ser.length); assertArrayEquals(data1, ser); } @@ -404,8 +404,7 @@ public final class TestHyperlinkRecord { link.setShortFilename("link1.xls"); byte[] tmp = link.serialize(); - byte[] ser = new byte[tmp.length-4]; - System.arraycopy(tmp, 4, ser, 0, ser.length); + byte[] ser = Arrays.copyOfRange(tmp, 4, tmp.length); assertEquals(data2.length, ser.length); assertArrayEquals(data2, ser); } @@ -420,8 +419,7 @@ public final class TestHyperlinkRecord { link.setTextMark("Sheet1!A1"); byte[] tmp = link.serialize(); - byte[] ser = new byte[tmp.length-4]; - System.arraycopy(tmp, 4, ser, 0, ser.length); + byte[] ser = Arrays.copyOfRange(tmp, 4, tmp.length); assertEquals(data4.length, ser.length); assertArrayEquals(data4, ser); } @@ -436,8 +434,7 @@ public final class TestHyperlinkRecord { link.setAddress("mailto:ebgans@mail.ru?subject=Hello,%20Ebgans!"); byte[] tmp = link.serialize(); - byte[] ser = new byte[tmp.length-4]; - System.arraycopy(tmp, 4, ser, 0, ser.length); + byte[] ser = Arrays.copyOfRange(tmp, 4, tmp.length); assertEquals(data3.length, ser.length); assertArrayEquals(data3, ser); } diff --git a/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java b/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java index 0cda75422c..c6cb9ac26f 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestObjRecord.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.Arrays; import java.util.List; import org.apache.poi.util.HexRead; @@ -77,8 +78,7 @@ public final class TestObjRecord { byte [] recordBytes = record.serialize(); assertEquals(26, recordBytes.length - 4); - byte[] subData = new byte[recdata.length]; - System.arraycopy(recordBytes, 4, subData, 0, subData.length); + byte[] subData = Arrays.copyOfRange(recordBytes, 4, 4+recdata.length); assertArrayEquals(recdata, subData); } @@ -99,8 +99,7 @@ public final class TestObjRecord { //serialize and read again byte [] recordBytes = record.serialize(); //cut off the record header - byte [] bytes = new byte[recordBytes.length-4]; - System.arraycopy(recordBytes, 4, bytes, 0, bytes.length); + byte [] bytes = Arrays.copyOfRange(recordBytes, 4, recordBytes.length); record = new ObjRecord(TestcaseRecordInputStream.create(ObjRecord.sid, bytes)); List<SubRecord> subrecords = record.getSubRecords(); diff --git a/src/testcases/org/apache/poi/hssf/record/TestSubRecord.java b/src/testcases/org/apache/poi/hssf/record/TestSubRecord.java index bbeea09ce0..10377c23f3 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSubRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSubRecord.java @@ -24,6 +24,8 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import java.util.Arrays; + import org.apache.poi.util.HexRead; import org.apache.poi.util.LittleEndian; import org.junit.Test; @@ -130,11 +132,10 @@ public final class TestSubRecord { ); final int LBS_START_POS = 0x002E; final int WRONG_LBS_SIZE = 0x1FEE; - assertEquals(0x0013, LittleEndian.getShort(data, LBS_START_POS+0)); + assertEquals(0x0013, LittleEndian.getShort(data, LBS_START_POS)); assertEquals(WRONG_LBS_SIZE, LittleEndian.getShort(data, LBS_START_POS+2)); int wrongTotalSize = LBS_START_POS + 4 + WRONG_LBS_SIZE; - byte[] wrongData = new byte[wrongTotalSize]; - System.arraycopy(data, 0, wrongData, 0, data.length); + byte[] wrongData = Arrays.copyOf(data, wrongTotalSize); // wrongData has the ObjRecord data as would have been written by v3.1 RecordInputStream in = TestcaseRecordInputStream.create(ObjRecord.sid, wrongData); diff --git a/src/testcases/org/apache/poi/hssf/record/TestcaseRecordInputStream.java b/src/testcases/org/apache/poi/hssf/record/TestcaseRecordInputStream.java index f528ddb1e3..4acc5f71e3 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestcaseRecordInputStream.java +++ b/src/testcases/org/apache/poi/hssf/record/TestcaseRecordInputStream.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianByteArrayInputStream; @@ -95,9 +96,6 @@ public final class TestcaseRecordInputStream { } public static byte[] cut(byte[] data, int fromInclusive, int toExclusive) { - int length = toExclusive - fromInclusive; - byte[] result = new byte[length]; - System.arraycopy( data, fromInclusive, result, 0, length); - return result; + return Arrays.copyOfRange(data, fromInclusive, toExclusive); } } diff --git a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java index d8f4b76b02..9837459393 100644 --- a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.hssf.record.ContinueRecord; import org.apache.poi.hssf.record.RecordInputStream; @@ -258,11 +259,8 @@ public final class TestUnicodeString { // Load in again and re-test - byte[] data = new byte[14]; - System.arraycopy(b, 4, data, 0, data.length); - LittleEndianInputStream inp = new LittleEndianInputStream( - new ByteArrayInputStream(data) - ); + byte[] data = Arrays.copyOfRange(b, 4, 4+14); + LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(data)); ext = new ExtRst(inp, data.length); assertEquals(0, ext.getNumberOfRuns()); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java index ed18bf34ee..95bd508770 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPicture.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import java.io.IOException; +import java.util.Arrays; import java.util.List; import org.apache.poi.POIDataSamples; @@ -279,8 +280,7 @@ public final class TestHSSFPicture extends BaseTestPicture { byte[] pictureDataOut = wb2.getAllPictures().get(0).getData(); assertArrayEquals(pictureDataEmf, pictureDataOut); - byte[] wmfNoHeader = new byte[pictureDataWmf.length - 22]; - System.arraycopy(pictureDataWmf, 22, wmfNoHeader, 0, pictureDataWmf.length - 22); + byte[] wmfNoHeader = Arrays.copyOfRange(pictureDataWmf, 22, pictureDataWmf.length); pictureDataOut = wb2.getAllPictures().get(2).getData(); assertArrayEquals(wmfNoHeader, pictureDataOut); } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java index ba03fcaf69..b6261817a9 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentInputStream.java @@ -56,8 +56,7 @@ public final class TestDocumentInputStream { } // Now create the POIFS Version - byte[] _workbook_data_only = new byte[_workbook_size]; - System.arraycopy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size); + byte[] _workbook_data_only = Arrays.copyOf(_workbook_data, _workbook_size); POIFSFileSystem poifs = new POIFSFileSystem(); // Make it easy when debugging to see what isn't the doc diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java index 9262e804a4..a31a78feab 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestDocumentOutputStream.java @@ -23,6 +23,7 @@ import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.util.IOUtils; import org.junit.Test; @@ -93,8 +94,7 @@ public final class TestDocumentOutputStream { @Test public void testWrite3() throws IOException { byte[] input = data(50); - byte[] expected = new byte[25]; - System.arraycopy(input, 1, expected, 0, 25); + byte[] expected = Arrays.copyOfRange(input, 1, 1+25); POIFSWriterListener l = (event) -> { DocumentOutputStream dstream = event.getStream(); diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java index 3a20b607a3..9c01ca0a52 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSDocumentPath.java @@ -23,6 +23,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import java.util.Arrays; + import org.junit.Test; /** @@ -49,9 +51,7 @@ public final class TestPOIFSDocumentPath { String[] components = {"foo", "bar", "foobar", "fubar"}; for (int j = 0; j < components.length; j++) { - String[] params = new String[ j ]; - - System.arraycopy(components, 0, params, 0, j); + String[] params = Arrays.copyOf(components, j); POIFSDocumentPath path = new POIFSDocumentPath(params); assertEquals(j, path.length()); @@ -91,16 +91,12 @@ public final class TestPOIFSDocumentPath { String[] initialComponents = {"a", "b", "c"}; for (int n = 0; n < initialComponents.length; n++) { - String[] initialParams = new String[ n ]; - - System.arraycopy(initialComponents, 0, initialParams, 0, n); + String[] initialParams = Arrays.copyOf(initialComponents, n); POIFSDocumentPath base = new POIFSDocumentPath(initialParams); String[] components = {"foo", "bar", "foobar", "fubar"}; for (int j = 0; j < components.length; j++) { - String[] params = new String[ j ]; - - System.arraycopy(components, 0, params, 0, j); + String[] params = Arrays.copyOf(components, j); POIFSDocumentPath path = new POIFSDocumentPath(base, params); assertEquals(j + n, path.length()); diff --git a/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java b/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java index c14c5dd17f..0a11d83a57 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestDirectoryProperty.java @@ -282,9 +282,7 @@ public final class TestDirectoryProperty { private static void verifyReadingProperty(int index, byte[] input, int offset, String name) { DirectoryProperty property = new DirectoryProperty(index, input, offset); ByteArrayOutputStream stream = new ByteArrayOutputStream(128); - byte[] expected = new byte[128]; - - System.arraycopy(input, offset, expected, 0, 128); + byte[] expected = Arrays.copyOfRange(input, offset, offset+128); try { property.writeData(stream); } catch (IOException e) { diff --git a/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java b/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java index 95dbf3e44b..2ad3a7714a 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestDocumentProperty.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.SummaryInformation; @@ -82,9 +83,7 @@ public final class TestDocumentProperty { DocumentProperty property = new DocumentProperty(index, input, offset); ByteArrayOutputStream stream = new ByteArrayOutputStream(128); - byte[] expected = new byte[ 128 ]; - - System.arraycopy(input, offset, expected, 0, 128); + byte[] expected = Arrays.copyOfRange(input, offset, offset+128); property.writeData(stream); byte[] output = stream.toByteArray(); diff --git a/src/testcases/org/apache/poi/poifs/property/TestPropertyFactory.java b/src/testcases/org/apache/poi/poifs/property/TestPropertyFactory.java index 9544e0a031..27387ddf40 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestPropertyFactory.java +++ b/src/testcases/org/apache/poi/poifs/property/TestPropertyFactory.java @@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.poi.poifs.storage.RawDataUtil; @@ -68,8 +69,7 @@ public final class TestPropertyFactory { for (int readBytes; (readBytes = stream.read(buf)) != -1; ) { byte[] bbuf = buf; if (readBytes < 512) { - bbuf = new byte[readBytes]; - System.arraycopy(buf, 0, bbuf, 0, readBytes); + bbuf = Arrays.copyOf(buf, readBytes); } PropertyFactory.convertToProperties(bbuf, properties); diff --git a/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java b/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java index 5553bb7b10..9bbb612f3d 100644 --- a/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java +++ b/src/testcases/org/apache/poi/poifs/property/TestRootProperty.java @@ -19,10 +19,10 @@ package org.apache.poi.poifs.property; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.poifs.storage.RawDataUtil; @@ -116,9 +116,7 @@ public final class TestRootProperty { String sClsId) { RootProperty property = new RootProperty(index, input, offset); ByteArrayOutputStream stream = new ByteArrayOutputStream(128); - byte[] expected = new byte[128]; - - System.arraycopy(input, offset, expected, 0, 128); + byte[] expected = Arrays.copyOfRange(input, offset, offset+128); try { property.writeData(stream); } catch (IOException e) { diff --git a/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java b/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java index c0cc2bbfa3..3234676d17 100644 --- a/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java +++ b/src/testcases/org/apache/poi/poifs/storage/TestHeaderBlockReading.java @@ -22,6 +22,7 @@ import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.Arrays; import org.junit.Test; @@ -56,11 +57,9 @@ public final class TestHeaderBlockReading { assertEquals(-2, block.getPropertyStart()); // verify we can't read a short block - byte[] shortblock = new byte[511]; - - System.arraycopy(content, 0, shortblock, 0, 511); + byte[] shortblock = Arrays.copyOf(content, 511); try { - block = new HeaderBlock(new ByteArrayInputStream(shortblock)); + new HeaderBlock(new ByteArrayInputStream(shortblock)); fail("Should have caught IOException reading a short block"); } catch (IOException ignored) { @@ -71,7 +70,7 @@ public final class TestHeaderBlockReading { for (int index = 0; index < 8; index++) { content[index] = (byte) (content[index] - 1); try { - block = new HeaderBlock(new ByteArrayInputStream(content)); + new HeaderBlock(new ByteArrayInputStream(content)); fail("Should have caught IOException corrupting byte " + index); } catch (IOException ignored) { diff --git a/src/testcases/org/apache/poi/util/TestByteField.java b/src/testcases/org/apache/poi/util/TestByteField.java index b35a1d41c7..3c6bea78bf 100644 --- a/src/testcases/org/apache/poi/util/TestByteField.java +++ b/src/testcases/org/apache/poi/util/TestByteField.java @@ -116,12 +116,9 @@ public final class TestByteField { @Test public void testReadFromStream() throws IOException { ByteField field = new ByteField(0); - byte[] buffer = new byte[ _test_array.length ]; + ByteArrayInputStream stream = new ByteArrayInputStream(_test_array.clone()); - System.arraycopy(_test_array, 0, buffer, 0, buffer.length); - ByteArrayInputStream stream = new ByteArrayInputStream(buffer); - - for (int j = 0; j < buffer.length; j++) { + for (int j = 0; j < _test_array.length; j++) { field.readFromStream(stream); assertEquals("Testing " + j, _test_array[ j ], field.get()); } |