diff --git a/src/java/org/apache/poi/ddf/EscherArrayProperty.java b/src/java/org/apache/poi/ddf/EscherArrayProperty.java index 0e745cc767..c1ac06b7bc 100644 --- a/src/java/org/apache/poi/ddf/EscherArrayProperty.java +++ b/src/java/org/apache/poi/ddf/EscherArrayProperty.java @@ -96,7 +96,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements * @param propertyNumber the property number part of the property id * @param isBlipId {@code true}, if it references a blip * @param complexData the data - * + * * @deprecated use {@link #EscherArrayProperty(EscherPropertyTypes, boolean, int)} and {@link #setComplexData(byte[])} */ @Deprecated @@ -124,7 +124,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements // when called by user code, fix the size to be valid for the header return complexSize == 0 ? 6 : complexSize; } - + public int getNumberOfElementsInArray() { return (emptyComplexPart) ? 0 : LittleEndian.getUShort(getComplexData(), 0); } @@ -171,9 +171,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements public byte[] getElement(int index) { int actualSize = getActualSizeOfElements(getSizeOfElements()); - byte[] result = IOUtils.safelyAllocate(actualSize, MAX_RECORD_LENGTH); - System.arraycopy(getComplexData(), FIXED_SIZE + index * actualSize, result, 0, result.length ); - return result; + return IOUtils.safelyClone(getComplexData(), FIXED_SIZE + index * actualSize, actualSize, MAX_RECORD_LENGTH); } public void setElement(int index, byte[] element) { @@ -252,7 +250,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements public boolean hasNext() { return (idx < getNumberOfElementsInArray()); } - + @Override public byte[] next() { if (!hasNext()) { @@ -260,7 +258,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements } return getElement(idx++); } - + @Override public void remove() { throw new UnsupportedOperationException("not yet implemented"); diff --git a/src/java/org/apache/poi/ddf/EscherBSERecord.java b/src/java/org/apache/poi/ddf/EscherBSERecord.java index f11860a0fb..08f3a0c694 100644 --- a/src/java/org/apache/poi/ddf/EscherBSERecord.java +++ b/src/java/org/apache/poi/ddf/EscherBSERecord.java @@ -103,8 +103,8 @@ public final class EscherBSERecord extends EscherRecord { pos += 36 + bytesRead; bytesRemaining -= bytesRead; - _remainingData = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy( data, pos, _remainingData, 0, bytesRemaining ); + _remainingData = IOUtils.safelyClone(data, pos, bytesRemaining, MAX_RECORD_LENGTH); + return bytesRemaining + 8 + 36 + (field_12_blipRecord == null ? 0 : field_12_blipRecord.getRecordSize()) ; } diff --git a/src/java/org/apache/poi/ddf/EscherBlipRecord.java b/src/java/org/apache/poi/ddf/EscherBlipRecord.java index 23ed091a4a..c9e6d73e5c 100644 --- a/src/java/org/apache/poi/ddf/EscherBlipRecord.java +++ b/src/java/org/apache/poi/ddf/EscherBlipRecord.java @@ -49,9 +49,7 @@ public class EscherBlipRecord extends EscherRecord { int bytesAfterHeader = readHeader( data, offset ); int pos = offset + HEADER_SIZE; - field_pictureData = IOUtils.safelyAllocate(bytesAfterHeader, MAX_RECORD_LENGTH); - System.arraycopy(data, pos, field_pictureData, 0, bytesAfterHeader); - + field_pictureData = IOUtils.safelyClone(data, pos, bytesAfterHeader, MAX_RECORD_LENGTH); return bytesAfterHeader + 8; } @@ -108,8 +106,7 @@ public class EscherBlipRecord extends EscherRecord { if (pictureData == null || offset < 0 || length < 0 || pictureData.length < offset+length) { throw new IllegalArgumentException("picture data can't be null"); } - field_pictureData = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); - System.arraycopy(pictureData, offset, field_pictureData, 0, length); + field_pictureData = IOUtils.safelyClone(pictureData, offset, length, MAX_RECORD_LENGTH); } @Override diff --git a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java index 0d94259554..64d985b9a9 100644 --- a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java @@ -106,8 +106,8 @@ public class EscherClientAnchorRecord extends EscherRecord { } } bytesRemaining -= size; - remainingData = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining ); + remainingData = IOUtils.safelyClone(data, pos + size, bytesRemaining, MAX_RECORD_LENGTH); + return 8 + size + bytesRemaining; } diff --git a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java index ff825158f2..80f4f9e7e4 100644 --- a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java @@ -50,8 +50,9 @@ public class EscherClientDataRecord extends EscherRecord { public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { int bytesRemaining = readHeader( data, offset ); int pos = offset + 8; - remainingData = (bytesRemaining == 0) ? EMPTY : IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy( data, pos, remainingData, 0, bytesRemaining ); + + remainingData = (bytesRemaining == 0) ? EMPTY : IOUtils.safelyClone(data, pos, bytesRemaining, MAX_RECORD_LENGTH); + return 8 + bytesRemaining; } diff --git a/src/java/org/apache/poi/ddf/EscherDgRecord.java b/src/java/org/apache/poi/ddf/EscherDgRecord.java index be13b1d57a..b290b3cf64 100644 --- a/src/java/org/apache/poi/ddf/EscherDgRecord.java +++ b/src/java/org/apache/poi/ddf/EscherDgRecord.java @@ -45,14 +45,11 @@ public class EscherDgRecord extends EscherRecord { @Override public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { - /*int bytesRemaining =*/ readHeader( data, offset ); + readHeader( data, offset ); int pos = offset + 8; int size = 0; field_1_numShapes = LittleEndian.getInt( data, pos + size ); size += 4; field_2_lastMSOSPID = LittleEndian.getInt( data, pos + size ); size += 4; -// bytesRemaining -= size; -// remainingData = new byte[bytesRemaining]; -// System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining ); return getRecordSize(); } @@ -66,8 +63,6 @@ public class EscherDgRecord extends EscherRecord { LittleEndian.putInt( data, offset + 4, 8 ); LittleEndian.putInt( data, offset + 8, field_1_numShapes ); LittleEndian.putInt( data, offset + 12, field_2_lastMSOSPID ); -// System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length ); -// int pos = offset + 8 + 18 + remainingData.length; listener.afterRecordSerialize( offset + 16, getRecordId(), getRecordSize(), this ); return getRecordSize(); diff --git a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java index b8348f2a10..19b4e20af5 100644 --- a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java +++ b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java @@ -106,8 +106,7 @@ public final class EscherMetafileBlip extends EscherBlipRecord { field_6_fCompression = data[pos]; pos++; field_7_fFilter = data[pos]; pos++; - raw_pictureData = IOUtils.safelyAllocate(field_5_cbSave, MAX_RECORD_LENGTH); - System.arraycopy( data, pos, raw_pictureData, 0, field_5_cbSave ); + raw_pictureData = IOUtils.safelyClone(data, pos, field_5_cbSave, MAX_RECORD_LENGTH); pos += field_5_cbSave; // 0 means DEFLATE compression @@ -120,8 +119,7 @@ public final class EscherMetafileBlip extends EscherBlipRecord { int remaining = bytesAfterHeader - pos + offset + HEADER_SIZE; if(remaining > 0) { - remainingData = IOUtils.safelyAllocate(remaining, MAX_RECORD_LENGTH); - System.arraycopy( data, pos, remainingData, 0, remaining ); + remainingData = IOUtils.safelyClone(data, pos, remaining, MAX_RECORD_LENGTH); } return bytesAfterHeader + HEADER_SIZE; } diff --git a/src/java/org/apache/poi/ddf/EscherSpRecord.java b/src/java/org/apache/poi/ddf/EscherSpRecord.java index 165dec01ac..acec08ed32 100644 --- a/src/java/org/apache/poi/ddf/EscherSpRecord.java +++ b/src/java/org/apache/poi/ddf/EscherSpRecord.java @@ -88,14 +88,11 @@ public class EscherSpRecord extends EscherRecord { @Override public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { - /*int bytesRemaining =*/ readHeader( data, offset ); + readHeader( data, offset ); int pos = offset + 8; int size = 0; field_1_shapeId = LittleEndian.getInt( data, pos + size ); size += 4; field_2_flags = LittleEndian.getInt( data, pos + size ); size += 4; -// bytesRemaining -= size; -// remainingData = new byte[bytesRemaining]; -// System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining ); return getRecordSize(); } @@ -119,8 +116,6 @@ public class EscherSpRecord extends EscherRecord { LittleEndian.putInt( data, offset + 4, remainingBytes ); LittleEndian.putInt( data, offset + 8, field_1_shapeId ); LittleEndian.putInt( data, offset + 12, field_2_flags ); -// System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length ); -// int pos = offset + 8 + 18 + remainingData.length; listener.afterRecordSerialize( offset + getRecordSize(), getRecordId(), getRecordSize(), this ); return 8 + 8; } diff --git a/src/java/org/apache/poi/ddf/EscherSpgrRecord.java b/src/java/org/apache/poi/ddf/EscherSpgrRecord.java index 7029163db0..eb98b8004a 100644 --- a/src/java/org/apache/poi/ddf/EscherSpgrRecord.java +++ b/src/java/org/apache/poi/ddf/EscherSpgrRecord.java @@ -59,8 +59,6 @@ public class EscherSpgrRecord extends EscherRecord { if (bytesRemaining != 0) { throw new RecordFormatException("Expected no remaining bytes but got " + bytesRemaining); } -// remainingData = new byte[bytesRemaining]; -// System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining ); return 8 + size + bytesRemaining; } @@ -76,8 +74,6 @@ public class EscherSpgrRecord extends EscherRecord { LittleEndian.putInt( data, offset + 12, field_2_rectY1 ); LittleEndian.putInt( data, offset + 16, field_3_rectX2 ); LittleEndian.putInt( data, offset + 20, field_4_rectY2 ); -// System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length ); -// int pos = offset + 8 + 18 + remainingData.length; listener.afterRecordSerialize( offset + getRecordSize(), getRecordId(), offset + getRecordSize(), this ); return 8 + 16; } diff --git a/src/java/org/apache/poi/ddf/EscherTextboxRecord.java b/src/java/org/apache/poi/ddf/EscherTextboxRecord.java index b42a669977..dced174318 100644 --- a/src/java/org/apache/poi/ddf/EscherTextboxRecord.java +++ b/src/java/org/apache/poi/ddf/EscherTextboxRecord.java @@ -54,10 +54,8 @@ public final class EscherTextboxRecord extends EscherRecord { public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { int bytesRemaining = readHeader( data, offset ); - // Save the data, ready for the calling code to do something - // useful with it - thedata = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy( data, offset + 8, thedata, 0, bytesRemaining ); + // Save the data, ready for the calling code to do something useful with it + thedata = IOUtils.safelyClone(data, offset + 8, bytesRemaining, MAX_RECORD_LENGTH); return bytesRemaining + 8; } @@ -103,10 +101,8 @@ public final class EscherTextboxRecord extends EscherRecord { * @param start the start position in the buffer * @param length the length of the block */ - public void setData(byte[] b, int start, int length) - { - thedata = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); - System.arraycopy(b,start,thedata,0,length); + public void setData(byte[] b, int start, int length) { + thedata = IOUtils.safelyClone(b, start, length, MAX_RECORD_LENGTH); } /** diff --git a/src/java/org/apache/poi/ddf/UnknownEscherRecord.java b/src/java/org/apache/poi/ddf/UnknownEscherRecord.java index 9e0236e7d1..83de70c836 100644 --- a/src/java/org/apache/poi/ddf/UnknownEscherRecord.java +++ b/src/java/org/apache/poi/ddf/UnknownEscherRecord.java @@ -81,8 +81,8 @@ public final class UnknownEscherRecord extends EscherRecord { bytesRemaining = 0; } - thedata = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy( data, offset + 8, thedata, 0, bytesRemaining ); + thedata = IOUtils.safelyClone(data, offset + 8, bytesRemaining, MAX_RECORD_LENGTH); + return bytesRemaining + 8; } diff --git a/src/java/org/apache/poi/hpsf/Thumbnail.java b/src/java/org/apache/poi/hpsf/Thumbnail.java index f6e52c0192..66f5f178fa 100644 --- a/src/java/org/apache/poi/hpsf/Thumbnail.java +++ b/src/java/org/apache/poi/hpsf/Thumbnail.java @@ -17,6 +17,7 @@ package org.apache.poi.hpsf; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; /** *

Class to manipulate data in the Clipboard Variant ({@link @@ -119,6 +120,9 @@ public final class Thumbnail { */ public static final int CF_BITMAP = 2; + //arbitrarily selected; may need to increase + private static final int MAX_RECORD_LENGTH = 1_000_000; + /** *

A byte[] to hold a thumbnail image in ({@link * Variant#VT_CF VT_CF}) format.

@@ -260,13 +264,6 @@ public final class Thumbnail { "be CF_METAFILEPICT."); } byte[] thumbnail = getThumbnail(); - int wmfImageLength = thumbnail.length - OFFSET_WMFDATA; - byte[] wmfImage = new byte[wmfImageLength]; - System.arraycopy(thumbnail, - OFFSET_WMFDATA, - wmfImage, - 0, - wmfImageLength); - return wmfImage; + return IOUtils.safelyClone(thumbnail, OFFSET_WMFDATA, thumbnail.length - OFFSET_WMFDATA, MAX_RECORD_LENGTH); } } diff --git a/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java b/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java index 365284b7f3..7289eda2f1 100644 --- a/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java +++ b/src/java/org/apache/poi/hssf/record/AbstractEscherHolderRecord.java @@ -87,17 +87,17 @@ public abstract class AbstractEscherHolderRecord extends Record { @Override public int serialize(int offset, byte[] data) { - LittleEndian.putShort( data, 0 + offset, getSid() ); + LittleEndian.putShort(data, offset, getSid() ); LittleEndian.putShort( data, 2 + offset, (short) ( getRecordSize() - 4 ) ); byte[] rawData = getRawData(); if ( escherRecords.size() == 0 && rawData != null ) { - LittleEndian.putShort(data, 0 + offset, getSid()); + LittleEndian.putShort(data, offset, getSid()); LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); System.arraycopy( rawData, 0, data, 4 + offset, rawData.length); return rawData.length + 4; } - LittleEndian.putShort(data, 0 + offset, getSid()); + LittleEndian.putShort(data, offset, getSid()); LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); int pos = offset + 4; diff --git a/src/java/org/apache/poi/hssf/record/FormatRecord.java b/src/java/org/apache/poi/hssf/record/FormatRecord.java index 3b53f4957a..92a4898b08 100644 --- a/src/java/org/apache/poi/hssf/record/FormatRecord.java +++ b/src/java/org/apache/poi/hssf/record/FormatRecord.java @@ -17,6 +17,7 @@ package org.apache.poi.hssf.record; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -151,8 +152,7 @@ public final class FormatRecord extends StandardRecord { //there can be a remaining byte (without proper final '00') //that should be read as a byte if (ris.available() == 1) { - char[] tmp = new char[buf.length+1]; - System.arraycopy(buf, 0, tmp, 0, buf.length); + char[] tmp = Arrays.copyOf(buf, buf.length+1); tmp[buf.length] = (char)ris.readUByte(); buf = tmp; } diff --git a/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java b/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java index 8d5cafa5ae..5589cf5369 100644 --- a/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java +++ b/src/java/org/apache/poi/hssf/record/HeaderFooterRecord.java @@ -78,9 +78,7 @@ public final class HeaderFooterRecord extends StandardRecord { * @return the sheet view?s GUID */ public byte[] getGuid(){ - byte[] guid = new byte[16]; - System.arraycopy(_rawData, 12, guid, 0, guid.length); - return guid; + return Arrays.copyOfRange(_rawData, 12, 12+16); } /** diff --git a/src/java/org/apache/poi/hssf/record/UserSViewBegin.java b/src/java/org/apache/poi/hssf/record/UserSViewBegin.java index f9a1455de4..5f34019256 100644 --- a/src/java/org/apache/poi/hssf/record/UserSViewBegin.java +++ b/src/java/org/apache/poi/hssf/record/UserSViewBegin.java @@ -17,6 +17,7 @@ package org.apache.poi.hssf.record; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -74,9 +75,7 @@ public final class UserSViewBegin extends StandardRecord { * @return Globally unique identifier for the custom view */ public byte[] getGuid(){ - byte[] guid = new byte[16]; - System.arraycopy(_rawData, 0, guid, 0, guid.length); - return guid; + return Arrays.copyOf(_rawData, 16); } @Override diff --git a/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java b/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java index 8479bd3d18..0026aabaa8 100644 --- a/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java +++ b/src/java/org/apache/poi/hssf/record/cont/ContinuableRecordInput.java @@ -19,8 +19,8 @@ package org.apache.poi.hssf.record.cont; -import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.ContinueRecord; +import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.util.LittleEndianInput; /** @@ -44,7 +44,7 @@ import org.apache.poi.util.LittleEndianInput; * *

* YK: For now (March 2011) this class is only used to read - * @see org.apache.poi.hssf.record.common.UnicodeString.ExtRst blocks of a UnicodeString. + * @see org.apache.poi.hssf.record.common.ExtRst blocks of a UnicodeString. * *

*/ @@ -78,7 +78,7 @@ public class ContinuableRecordInput implements LittleEndianInput { public int readUShort(){ int ch1 = readUByte(); int ch2 = readUByte(); - return (ch2 << 8) + (ch1 << 0); + return (ch2 << 8) + (ch1); } @Override @@ -87,7 +87,7 @@ public class ContinuableRecordInput implements LittleEndianInput { int ch2 = _in.readUByte(); int ch3 = _in.readUByte(); int ch4 = _in.readUByte(); - return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0); + return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1); } @Override @@ -107,7 +107,7 @@ public class ContinuableRecordInput implements LittleEndianInput { ((long)b3 << 24) + (b2 << 16) + (b1 << 8) + - (b0 << 0)); + (b0)); } @Override @@ -124,7 +124,7 @@ public class ContinuableRecordInput implements LittleEndianInput { public void readFully(byte[] buf, int off, int len){ _in.readFully(buf, off, len); } - + @Override public void readPlain(byte[] buf, int off, int len) { readFully(buf, off, len); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java index a753036bcb..436a7f5d1f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java @@ -96,7 +96,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor * A factory method allowing the creation of conditional formatting * rules using an Icon Set / Multi-State formatting. * The thresholds for it will be created, but will be empty - * and require configuring with + * and require configuring with * {@link HSSFConditionalFormattingRule#getMultiStateFormatting()} * then * {@link HSSFIconMultiStateFormatting#getThresholds()} @@ -109,12 +109,12 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor /** * Create a Databar conditional formatting rule. - *

The thresholds and colour for it will be created, but will be - * empty and require configuring with + *

The thresholds and colour for it will be created, but will be + * empty and require configuring with * {@link HSSFConditionalFormattingRule#getDataBarFormatting()} * then * {@link HSSFDataBarFormatting#getMinThreshold()} - * and + * and * {@link HSSFDataBarFormatting#getMaxThreshold()} */ public HSSFConditionalFormattingRule createConditionalFormattingRule(HSSFExtendedColor color) { @@ -127,8 +127,8 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor /** * Create a Color Scale / Color Gradient conditional formatting rule. - *

The thresholds and colours for it will be created, but will be - * empty and require configuring with + *

The thresholds and colours for it will be created, but will be + * empty and require configuring with * {@link HSSFConditionalFormattingRule#getColorScaleFormatting()} * then * {@link HSSFColorScaleFormatting#getThresholds()} @@ -139,7 +139,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor CFRule12Record rr = CFRule12Record.createColorScale(_sheet); return new HSSFConditionalFormattingRule(_sheet, rr); } - + /** * Adds a copy of HSSFConditionalFormatting object to the sheet *

This method could be used to copy HSSFConditionalFormatting object @@ -196,8 +196,9 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor public int addConditionalFormatting(CellRangeAddress[] regions, ConditionalFormattingRule[] cfRules) { HSSFConditionalFormattingRule[] hfRules; - if(cfRules instanceof HSSFConditionalFormattingRule[]) hfRules = (HSSFConditionalFormattingRule[])cfRules; - else { + if(cfRules instanceof HSSFConditionalFormattingRule[]) { + hfRules = (HSSFConditionalFormattingRule[])cfRules; + } else { hfRules = new HSSFConditionalFormattingRule[cfRules.length]; System.arraycopy(cfRules, 0, hfRules, 0, hfRules.length); } @@ -206,7 +207,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor public int addConditionalFormatting(CellRangeAddress[] regions, HSSFConditionalFormattingRule rule1) { - return addConditionalFormatting(regions, rule1 == null ? + return addConditionalFormatting(regions, rule1 == null ? null : new HSSFConditionalFormattingRule[] { rule1 } ); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 44a1615f63..16d990f348 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -23,7 +23,6 @@ import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAME import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -93,10 +92,11 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentNode; import org.apache.poi.poifs.filesystem.EntryUtils; +import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.poifs.filesystem.FilteringDirectoryNode; +import org.apache.poi.poifs.filesystem.Ole10Native; import org.apache.poi.poifs.filesystem.POIFSDocument; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.apache.poi.poifs.filesystem.Ole10Native; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.formula.FormulaType; @@ -133,6 +133,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss //arbitrarily selected; may need to increase private static final int MAX_RECORD_LENGTH = 100_000; + private static final int MAX_IMAGE_LENGTH = 50_000_000; private static final Pattern COMMA_PATTERN = Pattern.compile(","); @@ -504,13 +505,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss workbook.updateNamesAfterCellShift(shifter); updateNamedRangesAfterSheetReorder(oldSheetIndex, pos); - + updateActiveSheetAfterSheetReorder(oldSheetIndex, pos); } - + /** * copy-pasted from XSSFWorkbook#updateNamedRangesAfterSheetReorder(int, int) - * + * * update sheet-scoped named ranges in this workbook after changing the sheet order * of a sheet at oldIndex to newIndex. * Sheets between these indices will move left or right by 1. @@ -540,7 +541,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } } - + private void updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex) { // adjust active sheet if necessary int active = getActiveSheetIndex(); @@ -600,7 +601,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } setSelectedTabs(list); } - + /** * Selects multiple sheets as a group. This is distinct from * the 'active' sheet (which is the sheet with focus). @@ -624,9 +625,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss short nSelected = (short) set.size(); workbook.getWindowOne().setNumSelectedTabs(nSelected); } - + /** - * Gets the selected sheets (if more than one, Excel calls these a [Group]). + * Gets the selected sheets (if more than one, Excel calls these a [Group]). * * @return indices of selected sheets */ @@ -641,7 +642,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } return Collections.unmodifiableCollection(indexes); } - + /** * Convenience method to set the active sheet. The active sheet is is the sheet * which is currently displayed when the workbook is viewed in Excel. @@ -743,7 +744,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss validateSheetIndex(sheetIx); return workbook.isSheetVeryHidden(sheetIx); } - + @Override public SheetVisibility getSheetVisibility(int sheetIx) { return workbook.getSheetVisibility(sheetIx); @@ -945,7 +946,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss public Iterator iterator() { return sheetIterator(); } - + private final class SheetIterator implements Iterator { final private Iterator it; private T cursor; @@ -1294,7 +1295,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * the Workbook was read, if any. * *

Once this has been called, no further - * operations, updates or reads should be performed on the + * operations, updates or reads should be performed on the * Workbook. */ @Override @@ -1304,42 +1305,42 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Write out this workbook to the currently open {@link File} via the - * writeable {@link POIFSFileSystem} it was opened as. - * + * writeable {@link POIFSFileSystem} it was opened as. + * *

This will fail (with an {@link IllegalStateException} if the * Workbook was opened read-only, opened from an {@link InputStream} - * instead of a File, or if this is not the root document. For those cases, - * you must use {@link #write(OutputStream)} or {@link #write(File)} to + * instead of a File, or if this is not the root document. For those cases, + * you must use {@link #write(OutputStream)} or {@link #write(File)} to * write to a brand new document. */ @Override public void write() throws IOException { validateInPlaceWritePossible(); final DirectoryNode dir = getDirectory(); - + // Update the Workbook stream in the file DocumentNode workbookNode = (DocumentNode)dir.getEntry( getWorkbookDirEntryName(dir)); POIFSDocument workbookDoc = new POIFSDocument(workbookNode); workbookDoc.replaceContents(new ByteArrayInputStream(getBytes())); - + // Update the properties streams in the file writeProperties(); - + // Sync with the File on disk dir.getFileSystem().writeFilesystem(); } - + /** * Method write - write out this workbook to a new {@link File}. Constructs * a new POI POIFSFileSystem, passes in the workbook binary representation and * writes it out. If the file exists, it will be replaced, otherwise a new one * will be created. - * + * * Note that you cannot write to the currently open File using this method. * If you opened your Workbook from a File, you must use the {@link #write()} * method instead! - * + * * @param newFile The new File you wish to write the XLS to * * @exception IOException if anything can't be written. @@ -1352,12 +1353,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss fs.writeFilesystem(); } } - + /** * Method write - write out this workbook to an {@link OutputStream}. Constructs * a new POI POIFSFileSystem, passes in the workbook binary representation and * writes it out. - * + * * If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive * or has a high cost/latency associated with each written byte, * consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream} @@ -1375,7 +1376,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss fs.writeFilesystem(stream); } } - + /** Writes the workbook out to a brand new, empty POIFS */ private void write(POIFSFileSystem fs) throws IOException { // For tracking what we've written out, used if we're @@ -1387,7 +1388,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // Write out our HPFS properties, if we have them writeProperties(fs, excepts); - + if (preserveNodes) { // Don't write out the old Workbook, we'll be doing our new one // If the file had an "incorrect" name for the workbook stream, @@ -1457,7 +1458,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss if (log.check( POILogger.DEBUG )) { log.log(DEBUG, "HSSFWorkbook.getBytes()"); } - + HSSFSheet[] sheets = getSheets(); int nSheets = sheets.length; @@ -1502,7 +1503,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } encryptBytes(retval); - + return retval; } @@ -1555,7 +1556,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss throw new EncryptedDocumentException(e); } } - + /*package*/ InternalWorkbook getWorkbook() { return workbook; } @@ -1873,12 +1874,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss short escherTag; switch (format) { case PICTURE_TYPE_WMF: - // remove first 22 bytes if file starts with magic bytes D7-CD-C6-9A - // see also http://de.wikipedia.org/wiki/Windows_Metafile#Hinweise_zur_WMF-Spezifikation - if (LittleEndian.getInt(pictureData) == 0x9AC6CDD7) { - byte[] picDataNoHeader = new byte[pictureData.length - 22]; - System.arraycopy(pictureData, 22, picDataNoHeader, 0, pictureData.length-22); - pictureData = picDataNoHeader; + // remove first 22 bytes if file starts with the WMF placeable header + if (FileMagic.valueOf(pictureData) == FileMagic.WMF) { + pictureData = IOUtils.safelyClone(pictureData, 22, pictureData.length - 22, MAX_IMAGE_LENGTH); } // fall through case PICTURE_TYPE_EMF: @@ -2047,7 +2045,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } while (oleDir == null); Ole10Native.createOleMarkerEntry(oleDir); - + Ole10Native oleNative = new Ole10Native(label, fileName, command, oleData); ByteArrayOutputStream bos = new ByteArrayOutputStream(); oleNative.writeOut(bos); @@ -2213,15 +2211,15 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss public boolean changeExternalReference(String oldUrl, String newUrl) { return workbook.changeExternalReference(oldUrl, newUrl); } - + @Internal public InternalWorkbook getInternalWorkbook() { return workbook; } - + /** * Returns the spreadsheet version (EXCLE97) of this workbook - * + * * @return EXCEL97 SpreadsheetVersion enum * @since 3.14 beta 2 */ diff --git a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java index 368d2dbb31..3aec22e55f 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java @@ -135,8 +135,7 @@ public class BinaryRC4Decryptor extends Decryptor { hashAlg.update(salt); } - hash = new byte[5]; - System.arraycopy(hashAlg.digest(), 0, hash, 0, 5); + hash = Arrays.copyOf(hashAlg.digest(), 5); return new SecretKeySpec(hash, ver.getCipherAlgorithm().jceId); } diff --git a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java index 81481d3f65..7fc7731f7a 100644 --- a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java +++ b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java @@ -43,12 +43,12 @@ public class Ole10Native { /** * Default content of the \u0001Ole entry */ - private static final byte[] OLE_MARKER_BYTES = + private static final byte[] OLE_MARKER_BYTES = { 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; private static final String OLE_MARKER_NAME = "\u0001Ole"; - - + + // (the fields as they appear in the raw record:) private int totalSize; // 4 bytes, total size of record not including this field private short flags1 = 2; // 2 bytes, unknown, mostly [02 00] @@ -59,10 +59,10 @@ public class Ole10Native { private String command; // ASCIIZ, stored in this field without the terminating zero private byte[] dataBuffer; // varying size, the actual native data private short flags3; // some final flags? or zero terminators?, sometimes not there - + /** * the field encoding mode - merely a try-and-error guess ... - **/ + **/ private enum EncodingMode { /** * the data is stored in parsed format - including label, command, etc. @@ -77,11 +77,11 @@ public class Ole10Native { */ compact } - + private EncodingMode mode; - - + + /** * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected * to include a stream "{01}Ole10Native" which contains the actual @@ -95,7 +95,7 @@ public class Ole10Native { public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException { return createFromEmbeddedOleObject(poifs.getRoot()); } - + /** * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected * to include a stream "{01}Ole10Native" which contains the actual @@ -113,7 +113,7 @@ public class Ole10Native { return new Ole10Native(data, 0); } } - + /** * Creates an instance and fills the fields based on ... the fields */ @@ -124,7 +124,7 @@ public class Ole10Native { setDataBuffer(data); mode = EncodingMode.parsed; } - + /** * Creates an instance and fills the fields based on the data in the given buffer. * @@ -134,14 +134,14 @@ public class Ole10Native { */ public Ole10Native(byte[] data, int offset) throws Ole10NativeException { int ofs = offset; // current offset, initialized to start - + if (data.length < offset + 2) { throw new Ole10NativeException("data is too small"); } - + totalSize = LittleEndian.getInt(data, ofs); ofs += LittleEndianConsts.INT_SIZE; - + mode = EncodingMode.unparsed; if (LittleEndian.getShort(data, ofs) == 2) { // some files like equations don't have a valid filename, @@ -157,36 +157,36 @@ public class Ole10Native { switch (mode) { case parsed: { flags1 = LittleEndian.getShort(data, ofs); - + // structured format ofs += LittleEndianConsts.SHORT_SIZE; - + int len = getStringLength(data, ofs); label = StringUtil.getFromCompressedUnicode(data, ofs, len - 1); ofs += len; - + len = getStringLength(data, ofs); fileName = StringUtil.getFromCompressedUnicode(data, ofs, len - 1); ofs += len; - + flags2 = LittleEndian.getShort(data, ofs); ofs += LittleEndianConsts.SHORT_SIZE; - + unknown1 = LittleEndian.getShort(data, ofs); ofs += LittleEndianConsts.SHORT_SIZE; - + len = LittleEndian.getInt(data, ofs); ofs += LittleEndianConsts.INT_SIZE; command = StringUtil.getFromCompressedUnicode(data, ofs, len - 1); ofs += len; - + if (totalSize < ofs) { throw new Ole10NativeException("Invalid Ole10Native"); } - + dataSize = LittleEndian.getInt(data, ofs); ofs += LittleEndianConsts.INT_SIZE; - + if (dataSize < 0 || totalSize - (ofs - LittleEndianConsts.INT_SIZE) < dataSize) { throw new Ole10NativeException("Invalid Ole10Native"); } @@ -206,14 +206,12 @@ public class Ole10Native { if ((long)dataSize + (long)ofs > (long)data.length) { //cast to avoid overflow throw new Ole10NativeException("Invalid Ole10Native: declared data length > available data"); } - dataBuffer = IOUtils.safelyAllocate(dataSize, MAX_RECORD_LENGTH); - System.arraycopy(data, ofs, dataBuffer, 0, dataSize); - ofs += dataSize; + dataBuffer = IOUtils.safelyClone(data, ofs, dataSize, MAX_RECORD_LENGTH); } /** * Add the \1OLE marker entry, which is not the Ole10Native entry. - * Beside this "\u0001Ole" record there were several other records, e.g. CompObj, + * Beside this "\u0001Ole" record there were several other records, e.g. CompObj, * OlePresXXX, but it seems, that they aren't necessary */ public static void createOleMarkerEntry(final DirectoryEntry parent) throws IOException { @@ -224,14 +222,14 @@ public class Ole10Native { /** * Add the \1OLE marker entry, which is not the Ole10Native entry. - * Beside this "\u0001Ole" record there were several other records, e.g. CompObj, + * Beside this "\u0001Ole" record there were several other records, e.g. CompObj, * OlePresXXX, but it seems, that they aren't necessary */ public static void createOleMarkerEntry(final POIFSFileSystem poifs) throws IOException { createOleMarkerEntry(poifs.getRoot()); } - - + + /* * Helper - determine length of zero terminated string (ASCIIZ). */ @@ -247,7 +245,7 @@ public class Ole10Native { /** * Returns the value of the totalSize field - the total length of the * structure is totalSize + 4 (value of this field + size of this field). - * + * * @return the totalSize */ public int getTotalSize() { @@ -256,7 +254,7 @@ public class Ole10Native { /** * Returns flags1 - currently unknown - usually 0x0002. - * + * * @return the flags1 */ public short getFlags1() { @@ -267,7 +265,7 @@ public class Ole10Native { * Returns the label field - usually the name of the file (without * directory) but probably may be any name specified during * packaging/embedding the data. - * + * * @return the label */ public String getLabel() { @@ -277,7 +275,7 @@ public class Ole10Native { /** * Returns the fileName field - usually the name of the file being embedded * including the full path. - * + * * @return the fileName */ public String getFileName() { @@ -286,7 +284,7 @@ public class Ole10Native { /** * Returns flags2 - currently unknown - mostly 0x0000. - * + * * @return the flags2 */ public short getFlags2() { @@ -295,7 +293,7 @@ public class Ole10Native { /** * Returns unknown1 field - currently unknown. - * + * * @return the unknown1 */ public short getUnknown1() { @@ -306,7 +304,7 @@ public class Ole10Native { * Returns the command field - usually the name of the file being embedded * including the full path, may be a command specified during embedding the * file. - * + * * @return the command */ public String getCommand() { @@ -317,7 +315,7 @@ public class Ole10Native { * Returns the size of the embedded file. If the size is 0 (zero), no data * has been embedded. To be sure, that no data has been embedded, check * whether {@link #getDataBuffer()} returns null. - * + * * @return the dataSize */ public int getDataSize() { @@ -330,7 +328,7 @@ public class Ole10Native { * provide information about the data, but the actual data is not included. * (So label, filename etc. are available, but this method returns * null.) - * + * * @return the dataBuffer */ public byte[] getDataBuffer() { @@ -339,7 +337,7 @@ public class Ole10Native { /** * Returns the flags3 - currently unknown. - * + * * @return the flags3 */ public short getFlags3() { @@ -358,7 +356,7 @@ public class Ole10Native { @SuppressWarnings("resource") LittleEndianOutputStream leosOut = new LittleEndianOutputStream(out); - + switch (mode) { case parsed: { ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -379,7 +377,7 @@ public class Ole10Native { leos.write(getDataBuffer()); leos.writeShort(getFlags3()); leos.close(); // satisfy compiler ... - + leosOut.writeInt(bos.size()); // total size bos.writeTo(out); break; diff --git a/src/java/org/apache/poi/poifs/property/Property.java b/src/java/org/apache/poi/poifs/property/Property.java index a994ff133b..6ffff00424 100644 --- a/src/java/org/apache/poi/poifs/property/Property.java +++ b/src/java/org/apache/poi/poifs/property/Property.java @@ -124,19 +124,13 @@ public abstract class Property implements Child, POIFSViewable { */ protected Property(int index, byte [] array, int offset) { - _raw_data = new byte[ POIFSConstants.PROPERTY_SIZE ]; - System.arraycopy(array, offset, _raw_data, 0, - POIFSConstants.PROPERTY_SIZE); + _raw_data = Arrays.copyOfRange(array, offset, offset + POIFSConstants.PROPERTY_SIZE); _name_size = new ShortField(_name_size_offset, _raw_data); - _property_type = - new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET, _raw_data); + _property_type = new ByteField(PropertyConstants.PROPERTY_TYPE_OFFSET, _raw_data); _node_color = new ByteField(_node_color_offset, _raw_data); - _previous_property = new IntegerField(_previous_property_offset, - _raw_data); - _next_property = new IntegerField(_next_property_offset, - _raw_data); - _child_property = new IntegerField(_child_property_offset, - _raw_data); + _previous_property = new IntegerField(_previous_property_offset, _raw_data); + _next_property = new IntegerField(_next_property_offset, _raw_data); + _child_property = new IntegerField(_child_property_offset, _raw_data); _storage_clsid = new ClassID(_raw_data,_storage_clsid_offset); _user_flags = new IntegerField(_user_flags_offset, 0, _raw_data); _seconds_1 = new IntegerField(_seconds_1_offset, _raw_data); @@ -146,8 +140,7 @@ public abstract class Property implements Child, POIFSViewable { _start_block = new IntegerField(_start_block_offset, _raw_data); _size = new IntegerField(_size_offset, _raw_data); _index = index; - int name_length = (_name_size.get() / LittleEndianConsts.SHORT_SIZE) - - 1; + int name_length = (_name_size.get() / LittleEndianConsts.SHORT_SIZE) - 1; if (name_length < 1) { diff --git a/src/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/src/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java index 66d39ad702..153733f5a1 100644 --- a/src/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ b/src/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java @@ -245,8 +245,7 @@ public class EmbeddedExtractor implements Iterable { } int pictureBytesLen = idxEnd-idxStart+6; - byte[] pdfBytes = IOUtils.safelyAllocate(pictureBytesLen, MAX_RECORD_LENGTH); - System.arraycopy(pictureBytes, idxStart, pdfBytes, 0, pictureBytesLen); + byte[] pdfBytes = IOUtils.safelyClone(pictureBytes, idxStart, pictureBytesLen, MAX_RECORD_LENGTH); String filename = source.getShapeName().trim(); if (!endsWithIgnoreCase(filename, ".pdf")) { filename += ".pdf"; diff --git a/src/java/org/apache/poi/ss/formula/functions/Mirr.java b/src/java/org/apache/poi/ss/formula/functions/Mirr.java index 52d00b36f8..f7f9961a96 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Mirr.java +++ b/src/java/org/apache/poi/ss/formula/functions/Mirr.java @@ -18,6 +18,8 @@ package org.apache.poi.ss.formula.functions; +import java.util.Arrays; + import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; @@ -60,8 +62,7 @@ public class Mirr extends MultiOperandNumericFunction { double financeRate = values[values.length-1]; double reinvestRate = values[values.length-2]; - double[] mirrValues = new double[values.length - 2]; - System.arraycopy(values, 0, mirrValues, 0, mirrValues.length); + double[] mirrValues = Arrays.copyOf(values, values.length - 2); boolean mirrValuesAreAllNegatives = true; for (double mirrValue : mirrValues) { diff --git a/src/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java b/src/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java index 89e103ba43..5e3670c784 100644 --- a/src/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java +++ b/src/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.formula.functions; +import java.util.Arrays; + import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.formula.ThreeDEval; import org.apache.poi.ss.formula.TwoDEval; @@ -67,20 +69,13 @@ public abstract class MultiOperandNumericFunction implements Function { } public double[] toArray() { - if (_count < 1) { - return EMPTY_DOUBLE_ARRAY; - } - double[] result = new double[_count]; - System.arraycopy(_array, 0, result, 0, _count); - return result; + return _count < 1 ? EMPTY_DOUBLE_ARRAY : Arrays.copyOf(_array, _count); } private void ensureCapacity(int reqSize) { if (reqSize > _array.length) { int newSize = reqSize * 3 / 2; // grow with 50% extra - double[] newArr = new double[newSize]; - System.arraycopy(_array, 0, newArr, 0, _count); - _array = newArr; + _array = Arrays.copyOf(_array, newSize); } } @@ -159,7 +154,7 @@ public abstract class MultiOperandNumericFunction implements Function { public boolean isHiddenRowCounted() { return true; } - + /** * Collects values from a single argument */ diff --git a/src/java/org/apache/poi/ss/formula/functions/Npv.java b/src/java/org/apache/poi/ss/formula/functions/Npv.java index 760120f49b..1c19b84c1e 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Npv.java +++ b/src/java/org/apache/poi/ss/formula/functions/Npv.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.formula.functions; +import java.util.Arrays; + import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.NumberEval; @@ -43,8 +45,7 @@ public final class Npv implements Function { try { double rate = NumericFunction.singleOperandEvaluate(args[0], srcRowIndex, srcColumnIndex); // convert tail arguments into an array of doubles - ValueEval[] vargs = new ValueEval[args.length-1]; - System.arraycopy(args, 1, vargs, 0, vargs.length); + ValueEval[] vargs = Arrays.copyOfRange(args, 1, args.length, ValueEval[].class); double[] values = AggregateFunction.ValueCollector.collectValues(vargs); double result = FinanceLib.npv(rate, values); diff --git a/src/java/org/apache/poi/ss/formula/functions/Sumproduct.java b/src/java/org/apache/poi/ss/formula/functions/Sumproduct.java index 16a02af800..aea1fdb8ad 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Sumproduct.java +++ b/src/java/org/apache/poi/ss/formula/functions/Sumproduct.java @@ -17,6 +17,8 @@ package org.apache.poi.ss.formula.functions; +import java.util.Arrays; + import org.apache.poi.ss.formula.TwoDEval; import org.apache.poi.ss.formula.eval.AreaEval; import org.apache.poi.ss.formula.eval.BlankEval; @@ -124,9 +126,9 @@ public final class Sumproduct implements Function { private static ValueEval evaluateAreaSumProduct(ValueEval[] evalArgs) throws EvaluationException { int maxN = evalArgs.length; - TwoDEval[] args = new TwoDEval[maxN]; + TwoDEval[] args; try { - System.arraycopy(evalArgs, 0, args, 0, maxN); + args = Arrays.copyOf(evalArgs, maxN, TwoDEval[].class); } catch (ArrayStoreException e) { // one of the other args was not an AreaRef return ErrorEval.VALUE_INVALID; diff --git a/src/java/org/apache/poi/util/ArrayUtil.java b/src/java/org/apache/poi/util/ArrayUtil.java index 63e9f92883..1ebaae0215 100644 --- a/src/java/org/apache/poi/util/ArrayUtil.java +++ b/src/java/org/apache/poi/util/ArrayUtil.java @@ -18,6 +18,8 @@ package org.apache.poi.util; +import java.util.Arrays; + /** * Utility classes for dealing with arrays. */ @@ -54,8 +56,7 @@ public final class ArrayUtil { } // Grab the bit to move - Object[] toMove = new Object[numToMove]; - System.arraycopy(array, moveFrom, toMove, 0, numToMove); + Object[] toMove = Arrays.copyOfRange(array, moveFrom, moveFrom+numToMove); // Grab the bit to be shifted Object[] toShift; @@ -63,14 +64,12 @@ public final class ArrayUtil { if(moveFrom > moveTo) { // Moving to an earlier point in the array // Grab everything between the two points - toShift = new Object[(moveFrom-moveTo)]; - System.arraycopy(array, moveTo, toShift, 0, toShift.length); + toShift = Arrays.copyOfRange(array, moveTo, moveFrom); shiftTo = moveTo + numToMove; } else { // Moving to a later point in the array // Grab everything from after the toMove block, to the new point - toShift = new Object[(moveTo-moveFrom)]; - System.arraycopy(array, moveFrom+numToMove, toShift, 0, toShift.length); + toShift = Arrays.copyOfRange(array, moveFrom+numToMove, moveTo+numToMove); shiftTo = moveFrom; } diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 1f5214589e..26f461f12d 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -28,6 +28,7 @@ import java.io.OutputStream; import java.io.PushbackInputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; +import java.util.Arrays; import java.util.zip.CRC32; import java.util.zip.Checksum; @@ -35,6 +36,8 @@ import org.apache.poi.EmptyFileException; import org.apache.poi.POIDocument; import org.apache.poi.ss.usermodel.Workbook; + +@Internal public final class IOUtils { private static final POILogger logger = POILogFactory.getLogger( IOUtils.class ); @@ -610,6 +613,18 @@ public final class IOUtils { checkLength(length, maxLength); } + public static byte[] safelyClone(byte[] src, int offset, int length, int maxLength) { + if (src == null) { + return null; + } + assert(offset >= 0 && length >= 0 && maxLength >= 0); + safelyAllocateCheck(Math.min(src.length-offset,length), maxLength); + return Arrays.copyOfRange(src, offset, offset+length); + } + + + + /** * Simple utility function to check that you haven't hit EOF * when reading a byte. diff --git a/src/java/org/apache/poi/util/LittleEndian.java b/src/java/org/apache/poi/util/LittleEndian.java index 7aa95a5eb6..0ef687298f 100644 --- a/src/java/org/apache/poi/util/LittleEndian.java +++ b/src/java/org/apache/poi/util/LittleEndian.java @@ -26,93 +26,38 @@ import java.io.Serializable; * a utility class for handling little-endian numbers, which the 80x86 world is * replete with. The methods are all static, and input/output is from/to byte * arrays, or from InputStreams. - * - * @author Marc Johnson (mjohnson at apache dot org) - * @author Andrew Oliver (acoliver at apache dot org) */ -public class LittleEndian implements LittleEndianConsts -{ +@Internal +public final class LittleEndian implements LittleEndianConsts { /** * Exception to handle buffer underruns - * + * * @author Marc Johnson (mjohnson at apache dot org) */ - public static final class BufferUnderrunException extends IOException - { + public static final class BufferUnderrunException extends IOException { /** * Serial version UID - * + * * @see Serializable */ private static final long serialVersionUID = 8736973884877006145L; - BufferUnderrunException() - { + BufferUnderrunException() { super( "buffer underrun" ); } } - /** - * Copy a portion of a byte array - * - * @param data - * the original byte array - * @param offset - * Where to start copying from. - * @param size - * Number of bytes to copy. - * @return The byteArray value - * - * @see #getByteArray(byte[], int, int, int) if size is not a constant - * - * @throws IndexOutOfBoundsException - * - if copying would cause access of data outside array bounds. - */ - public static byte[] getByteArray( byte[] data, int offset, int size ) - { - byte[] copy = new byte[size]; - System.arraycopy( data, offset, copy, 0, size ); - - return copy; - } - - /** - * Copy a portion of a byte array - * - * @param data - * the original byte array - * @param offset - * Where to start copying from. - * @param size - * Number of bytes to copy. - * @param maxSize - * Size must be <= maxSize or an exception is thrown. - * Use this to avoid potential OOMs on corrupt data. - * @return The byteArray value - * @throws IndexOutOfBoundsException - * - if copying would cause access of data outside array bounds. - */ - public static byte[] getByteArray( byte[] data, int offset, int size, int maxSize) - { - byte[] copy = IOUtils.safelyAllocate(size, maxSize); - System.arraycopy( data, offset, copy, 0, size ); - - return copy; - } - - /** * get a double value from a byte array, reads it in little endian format * then converts the resulting revolting IEEE 754 (curse them) floating * point number to a happy java double - * + * * @param data * the byte array * @return the double (64-bit) value */ - public static double getDouble( byte[] data ) - { + public static double getDouble( byte[] data ) { return Double.longBitsToDouble( getLong( data, 0 ) ); } @@ -120,15 +65,14 @@ public class LittleEndian implements LittleEndianConsts * get a double value from a byte array, reads it in little endian format * then converts the resulting revolting IEEE 754 (curse them) floating * point number to a happy java double - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the double (64-bit) value */ - public static double getDouble( byte[] data, int offset ) - { + public static double getDouble( byte[] data, int offset ) { return Double.longBitsToDouble( getLong( data, offset ) ); } @@ -136,13 +80,12 @@ public class LittleEndian implements LittleEndianConsts * get a float value from a byte array, reads it in little endian format * then converts the resulting revolting IEEE 754 (curse them) floating * point number to a happy java float - * + * * @param data * the byte array * @return the double (64-bit) value */ - public static float getFloat( byte[] data ) - { + public static float getFloat( byte[] data ) { return getFloat( data, 0 ); } @@ -150,72 +93,67 @@ public class LittleEndian implements LittleEndianConsts * get a float value from a byte array, reads it in little endian format * then converts the resulting revolting IEEE 754 (curse them) floating * point number to a happy java float - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the double (64-bit) value */ - public static float getFloat( byte[] data, int offset ) - { + public static float getFloat( byte[] data, int offset ) { return Float.intBitsToFloat( getInt( data, offset ) ); } /** * get an int value from the beginning of a byte array - * + * * @param data * the byte array * @return the int (32-bit) value */ - public static int getInt( byte[] data ) - { + public static int getInt( byte[] data ) { return getInt( data, 0 ); } /** * get an int value from a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the int (32-bit) value */ - public static int getInt( byte[] data, int offset ) - { + public static int getInt( byte[] data, int offset ) { int i = offset; int b0 = data[i++] & 0xFF; int b1 = data[i++] & 0xFF; int b2 = data[i++] & 0xFF; - int b3 = data[i++] & 0xFF; - return ( b3 << 24 ) + ( b2 << 16 ) + ( b1 << 8 ) + ( b0 << 0 ); + int b3 = data[i ] & 0xFF; + return ( b3 << 24 ) + ( b2 << 16 ) + ( b1 << 8 ) + (b0); } /** * get a long value from a byte array - * + * * @param data * the byte array * @return the long (64-bit) value */ - public static long getLong( byte[] data ) - { + public static long getLong( byte[] data ) { return getLong( data, 0 ); } /** * get a long value from a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the long (64-bit) value */ - public static long getLong( byte[] data, int offset ) - { + public static long getLong( byte[] data, int offset ) { long result = 0xff & data[offset + 7]; for ( int j = offset + LONG_SIZE - 1; j >= offset; j-- ) @@ -228,35 +166,33 @@ public class LittleEndian implements LittleEndianConsts /** * get a short value from the beginning of a byte array - * + * * @param data * the byte array * @return the short (16-bit) value */ - public static short getShort( byte[] data ) - { + public static short getShort( byte[] data ) { return getShort( data, 0 ); } /** * get a short value from a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the short (16-bit) value */ - public static short getShort( byte[] data, int offset ) - { + public static short getShort( byte[] data, int offset ) { int b0 = data[offset] & 0xFF; int b1 = data[offset + 1] & 0xFF; - return (short) ( ( b1 << 8 ) + ( b0 << 0 ) ); + return (short) ( ( b1 << 8 ) + (b0) ); } /** * Read short array - * + * * @param data * the original byte array * @param offset @@ -266,8 +202,7 @@ public class LittleEndian implements LittleEndianConsts * @throws IndexOutOfBoundsException * - if read would cause access of data outside array bounds. */ - public static short[] getShortArray( byte[] data, int offset, int size ) - { + public static short[] getShortArray( byte[] data, int offset, int size ) { short[] result = new short[size / SHORT_SIZE]; for ( int i = 0; i < result.length; i++ ) { @@ -278,83 +213,77 @@ public class LittleEndian implements LittleEndianConsts /** * get the unsigned value of a byte. - * + * * @param data * the byte array. * @return the unsigned value of the byte as a 16 bit short */ - public static short getUByte( byte[] data ) - { + public static short getUByte( byte[] data ) { return (short) ( data[0] & 0xFF ); } /** * get the unsigned value of a byte. - * + * * @param data * the byte array. * @param offset * a starting offset into the byte array. * @return the unsigned value of the byte as a 16 bit short */ - public static short getUByte( byte[] data, int offset ) - { + public static short getUByte( byte[] data, int offset ) { return (short) ( data[offset] & 0xFF ); } /** * get an unsigned int value from a byte array - * + * * @param data * the byte array * @return the unsigned int (32-bit) value in a long */ - public static long getUInt( byte[] data ) - { + public static long getUInt( byte[] data ) { return getUInt( data, 0 ); } /** * get an unsigned int value from a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the unsigned int (32-bit) value in a long */ - public static long getUInt( byte[] data, int offset ) - { + public static long getUInt( byte[] data, int offset ) { long retNum = getInt( data, offset ); - return retNum & 0x00FFFFFFFFl; + return retNum & 0x00FFFFFFFFL; } /** * get an unsigned short value from the beginning of a byte array - * + * * @param data * the byte array * @return the unsigned short (16-bit) value in an int */ - public static int getUShort( byte[] data ) - { + public static int getUShort( byte[] data ) { return getUShort( data, 0 ); } /** * get an unsigned short value from a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @return the unsigned short (16-bit) value in an integer */ - public static int getUShort( byte[] data, int offset ) - { + public static int getUShort( byte[] data, int offset ) { int b0 = data[offset] & 0xFF; int b1 = data[offset + 1] & 0xFF; - return ( b1 << 8 ) + ( b0 << 0 ); + return ( b1 << 8 ) + (b0); } /** @@ -366,14 +295,13 @@ public class LittleEndian implements LittleEndianConsts *

* Added for consistency with other put~() methods */ - public static void putByte( byte[] data, int offset, int value ) - { + public static void putByte( byte[] data, int offset, int value ) { data[offset] = (byte) value; } /** * put a double value into a byte array - * + * * @param data * the byte array * @param offset @@ -381,14 +309,13 @@ public class LittleEndian implements LittleEndianConsts * @param value * the double (64-bit) value */ - public static void putDouble( byte[] data, int offset, double value ) - { + public static void putDouble( byte[] data, int offset, double value ) { putLong( data, offset, Double.doubleToLongBits( value ) ); } /** * put a double value into a byte array - * + * * @param value * the double (64-bit) value * @param outputStream @@ -396,15 +323,13 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putDouble( double value, OutputStream outputStream ) - throws IOException - { + public static void putDouble( double value, OutputStream outputStream ) throws IOException { putLong( Double.doubleToLongBits( value ), outputStream ); } /** * put a float value into a byte array - * + * * @param data * the byte array * @param offset @@ -412,14 +337,13 @@ public class LittleEndian implements LittleEndianConsts * @param value * the float (32-bit) value */ - public static void putFloat( byte[] data, int offset, float value ) - { + public static void putFloat( byte[] data, int offset, float value ) { putInt( data, offset, Float.floatToIntBits( value ) ); } /** * put a float value into a byte array - * + * * @param value * the float (32-bit) value * @param outputStream @@ -427,15 +351,14 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putFloat( float value, OutputStream outputStream ) - throws IOException - { + @SuppressWarnings("unused") + public static void putFloat(float value, OutputStream outputStream ) throws IOException { putInt( Float.floatToIntBits( value ), outputStream ); } /** * put an int value into a byte array - * + * * @param data * the byte array * @param offset @@ -443,18 +366,17 @@ public class LittleEndian implements LittleEndianConsts * @param value * the int (32-bit) value */ - public static void putInt( byte[] data, int offset, int value ) - { + public static void putInt( byte[] data, int offset, int value ) { int i = offset; - data[i++] = (byte) ( ( value >>> 0 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); + data[i++] = (byte) ( ( value ) & 0xFF ); + data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); data[i++] = (byte) ( ( value >>> 16 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 24 ) & 0xFF ); + data[i] = (byte) ( ( value >>> 24 ) & 0xFF ); } /** * Put int into output stream - * + * * @param value * the int (32-bit) value * @param outputStream @@ -462,18 +384,16 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putInt( int value, OutputStream outputStream ) - throws IOException - { - outputStream.write( (byte) ( ( value >>> 0 ) & 0xFF ) ); - outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); + public static void putInt( int value, OutputStream outputStream ) throws IOException { + outputStream.write( (byte) ( ( value ) & 0xFF ) ); + outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 16 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 24 ) & 0xFF ) ); } /** * put a long value into a byte array - * + * * @param data * the byte array * @param offset @@ -481,10 +401,9 @@ public class LittleEndian implements LittleEndianConsts * @param value * the long (64-bit) value */ - public static void putLong( byte[] data, int offset, long value ) - { - data[offset + 0] = (byte) ( ( value >>> 0 ) & 0xFF ); - data[offset + 1] = (byte) ( ( value >>> 8 ) & 0xFF ); + public static void putLong( byte[] data, int offset, long value ) { + data[offset ] = (byte) ( ( value ) & 0xFF ); + data[offset + 1] = (byte) ( ( value >>> 8 ) & 0xFF ); data[offset + 2] = (byte) ( ( value >>> 16 ) & 0xFF ); data[offset + 3] = (byte) ( ( value >>> 24 ) & 0xFF ); data[offset + 4] = (byte) ( ( value >>> 32 ) & 0xFF ); @@ -495,7 +414,7 @@ public class LittleEndian implements LittleEndianConsts /** * Put long into output stream - * + * * @param value * the long (64-bit) value * @param outputStream @@ -503,11 +422,9 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putLong( long value, OutputStream outputStream ) - throws IOException - { - outputStream.write( (byte) ( ( value >>> 0 ) & 0xFF ) ); - outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); + public static void putLong( long value, OutputStream outputStream ) throws IOException { + outputStream.write( (byte) ( ( value ) & 0xFF ) ); + outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 16 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 24 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 32 ) & 0xFF ) ); @@ -518,7 +435,7 @@ public class LittleEndian implements LittleEndianConsts /** * put a short value into a byte array - * + * * @param data * the byte array * @param offset @@ -526,16 +443,15 @@ public class LittleEndian implements LittleEndianConsts * @param value * the short (16-bit) value */ - public static void putShort( byte[] data, int offset, short value ) - { + public static void putShort( byte[] data, int offset, short value ) { int i = offset; - data[i++] = (byte) ( ( value >>> 0 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); + data[i++] = (byte) ( ( value ) & 0xFF ); + data[i ] = (byte) ( ( value >>> 8 ) & 0xFF ); } /** * Put signed short into output stream - * + * * @param value * the short (16-bit) value * @param outputStream @@ -543,16 +459,14 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putShort( OutputStream outputStream, short value ) - throws IOException - { - outputStream.write( (byte) ( ( value >>> 0 ) & 0xFF ) ); + public static void putShort( OutputStream outputStream, short value ) throws IOException { + outputStream.write( (byte) ( ( value ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); } /** * Stores short array in buffer - * + * * @param data * the byte array * @param startOffset @@ -560,9 +474,7 @@ public class LittleEndian implements LittleEndianConsts * @param value * the short (16-bit) values */ - public static void putShortArray( byte[] data, int startOffset, - short[] value ) - { + public static void putShortArray( byte[] data, int startOffset, short[] value ) { int offset = startOffset; for ( short s : value ) { @@ -573,47 +485,45 @@ public class LittleEndian implements LittleEndianConsts /** * put an unsigned byte value into a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @param value * the short (16-bit) value - * + * * @exception ArrayIndexOutOfBoundsException * may be thrown */ - public static void putUByte( byte[] data, int offset, short value ) - { + public static void putUByte( byte[] data, int offset, short value ) { data[offset] = (byte) ( value & 0xFF ); } /** * put an unsigned int value into a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @param value * the int (32-bit) value - * + * * @exception ArrayIndexOutOfBoundsException * may be thrown */ - public static void putUInt( byte[] data, int offset, long value ) - { + public static void putUInt( byte[] data, int offset, long value ) { int i = offset; - data[i++] = (byte) ( ( value >>> 0 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); + data[i++] = (byte) ( ( value ) & 0xFF ); + data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); data[i++] = (byte) ( ( value >>> 16 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 24 ) & 0xFF ); + data[i ] = (byte) ( ( value >>> 24 ) & 0xFF ); } /** * Put unsigned int into output stream - * + * * @param value * the int (32-bit) value * @param outputStream @@ -621,38 +531,35 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putUInt( long value, OutputStream outputStream ) - throws IOException - { - outputStream.write( (byte) ( ( value >>> 0 ) & 0xFF ) ); - outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); + public static void putUInt( long value, OutputStream outputStream ) throws IOException { + outputStream.write( (byte) ( ( value ) & 0xFF ) ); + outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 16 ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 24 ) & 0xFF ) ); } /** * put an unsigned short value into a byte array - * + * * @param data * the byte array * @param offset * a starting offset into the byte array * @param value * the short (16-bit) value - * + * * @exception ArrayIndexOutOfBoundsException * may be thrown */ - public static void putUShort( byte[] data, int offset, int value ) - { + public static void putUShort( byte[] data, int offset, int value ) { int i = offset; - data[i++] = (byte) ( ( value >>> 0 ) & 0xFF ); - data[i++] = (byte) ( ( value >>> 8 ) & 0xFF ); + data[i++] = (byte) ( ( value ) & 0xFF ); + data[i ] = (byte) ( ( value >>> 8 ) & 0xFF ); } /** * Put unsigned short into output stream - * + * * @param value * the unsigned short (16-bit) value * @param outputStream @@ -660,16 +567,14 @@ public class LittleEndian implements LittleEndianConsts * @throws IOException * if an I/O error occurs */ - public static void putUShort( int value, OutputStream outputStream ) - throws IOException - { - outputStream.write( (byte) ( ( value >>> 0 ) & 0xFF ) ); + public static void putUShort( int value, OutputStream outputStream ) throws IOException { + outputStream.write( (byte) ( ( value ) & 0xFF ) ); outputStream.write( (byte) ( ( value >>> 8 ) & 0xFF ) ); } /** * get an int value from an InputStream - * + * * @param stream * the InputStream from which the int is to be read * @return the int (32-bit) value @@ -678,9 +583,7 @@ public class LittleEndian implements LittleEndianConsts * @exception BufferUnderrunException * if the stream cannot provide enough bytes */ - public static int readInt( InputStream stream ) throws IOException, - BufferUnderrunException - { + public static int readInt( InputStream stream ) throws IOException { int ch1 = stream.read(); int ch2 = stream.read(); int ch3 = stream.read(); @@ -689,12 +592,12 @@ public class LittleEndian implements LittleEndianConsts { throw new BufferUnderrunException(); } - return ( ch4 << 24 ) + ( ch3 << 16 ) + ( ch2 << 8 ) + ( ch1 << 0 ); + return ( ch4 << 24 ) + ( ch3 << 16 ) + ( ch2 << 8 ) + ( ch1 ); } - + /** * get an unsigned int value from an InputStream - * + * * @param stream * the InputStream from which the int is to be read * @return the unsigned int (32-bit) value @@ -703,16 +606,14 @@ public class LittleEndian implements LittleEndianConsts * @exception BufferUnderrunException * if the stream cannot provide enough bytes */ - public static long readUInt( InputStream stream ) throws IOException, - BufferUnderrunException - { + public static long readUInt( InputStream stream ) throws IOException { long retNum = readInt(stream); - return retNum & 0x00FFFFFFFFl; + return retNum & 0x00FFFFFFFFL; } /** * get a long value from an InputStream - * + * * @param stream * the InputStream from which the long is to be read * @return the long (64-bit) value @@ -721,9 +622,7 @@ public class LittleEndian implements LittleEndianConsts * @exception BufferUnderrunException * if the stream cannot provide enough bytes */ - public static long readLong( InputStream stream ) throws IOException, - BufferUnderrunException - { + public static long readLong( InputStream stream ) throws IOException { int ch1 = stream.read(); int ch2 = stream.read(); int ch3 = stream.read(); @@ -732,8 +631,7 @@ public class LittleEndian implements LittleEndianConsts int ch6 = stream.read(); int ch7 = stream.read(); int ch8 = stream.read(); - if ( ( ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8 ) < 0 ) - { + if ( ( ch1 | ch2 | ch3 | ch4 | ch5 | ch6 | ch7 | ch8 ) < 0 ) { throw new BufferUnderrunException(); } @@ -741,12 +639,12 @@ public class LittleEndian implements LittleEndianConsts + ( (long) ch6 << 40 ) + ( (long) ch5 << 32 ) + ( (long) ch4 << 24 ) + // cast to long to preserve bit 31 // (sign bit for ints) - ( ch3 << 16 ) + ( ch2 << 8 ) + ( ch1 << 0 ); + ( ch3 << 16 ) + ( ch2 << 8 ) + ( ch1 ); } /** * get a short value from an InputStream - * + * * @param stream * the InputStream from which the short is to be read * @return the short (16-bit) value @@ -755,39 +653,32 @@ public class LittleEndian implements LittleEndianConsts * @exception BufferUnderrunException * if the stream cannot provide enough bytes */ - public static short readShort( InputStream stream ) throws IOException, - BufferUnderrunException - { + public static short readShort( InputStream stream ) throws IOException { return (short) readUShort( stream ); } - public static int readUShort( InputStream stream ) throws IOException, - BufferUnderrunException - { + public static int readUShort( InputStream stream ) throws IOException { int ch1 = stream.read(); int ch2 = stream.read(); - if ( ( ch1 | ch2 ) < 0 ) - { + if ( ( ch1 | ch2 ) < 0 ) { throw new BufferUnderrunException(); } - return ( ch2 << 8 ) + ( ch1 << 0 ); + return ( ch2 << 8 ) + ( ch1 ); } /** * Convert an 'unsigned' byte to an integer. ie, don't carry across the * sign. - * + * * @param b * Description of the Parameter * @return Description of the Return Value */ - public static int ubyteToInt( byte b ) - { + public static int ubyteToInt( byte b ) { return b & 0xFF; } - private LittleEndian() - { + private LittleEndian() { // no instances of this class } } diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index 2bf29a55a0..a0778a3efa 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -65,6 +65,9 @@ public final class StringUtil { final int offset, final int len) throws ArrayIndexOutOfBoundsException, IllegalArgumentException { + if (len == 0) { + return ""; + } if ((offset < 0) || (offset >= string.length)) { throw new ArrayIndexOutOfBoundsException("Illegal offset " + offset + " (String data is of length " + string.length + ")"); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java index 504a302955..a8507cd92e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFColor.java @@ -41,7 +41,7 @@ public class XSSFColor extends ExtendedColor { //noinspection deprecation return color == null ? null : new XSSFColor(color, map); } - + /** * Create an instance of XSSFColor from the supplied XML bean, with default color indexes * @param color The {@link CTColor} to use as color-value. @@ -52,7 +52,7 @@ public class XSSFColor extends ExtendedColor { public XSSFColor(CTColor color) { this(color, new DefaultIndexedColorMap()); } - + /** * Create an instance of XSSFColor from the supplied XML bean, with the given color indexes * @param color The {@link CTColor} to use as color-value. @@ -119,7 +119,7 @@ public class XSSFColor extends ExtendedColor { this(CTColor.Factory.newInstance(), colorMap); ctColor.setRgb(rgb); } - + /** * @param indexedColor color index (Enum named for default colors) * @param colorMap The IndexedColorMap to use instead of the default one @@ -167,7 +167,7 @@ public class XSSFColor extends ExtendedColor { public boolean isThemed() { return ctColor.isSetTheme(); } - + /** * @return true if the ctColor has a alpha */ @@ -215,14 +215,8 @@ public class XSSFColor extends ExtendedColor { return null; } - if(rgb.length == 4) { - // Need to trim off the alpha - byte[] tmp = new byte[3]; - System.arraycopy(rgb, 1, tmp, 0, 3); - return tmp; - } else { - return rgb; - } + // Need to trim off the alpha + return rgb.length == 4 ? Arrays.copyOfRange(rgb, 1, 4) : rgb; } /** @@ -258,7 +252,7 @@ public class XSSFColor extends ExtendedColor { } return null; } - + /** * Standard Alpha Red Green Blue ctColor value (ARGB). */ @@ -403,7 +397,7 @@ public class XSSFColor extends ExtendedColor { } return (XSSFColor)color; } - + @Override public int hashCode(){ return ctColor.toString().hashCode(); @@ -437,7 +431,7 @@ public class XSSFColor extends ExtendedColor { private boolean sameAuto(XSSFColor other) { return isAuto() == other.isAuto(); } - + @Override public boolean equals(Object o){ if(!(o instanceof XSSFColor)) { @@ -445,7 +439,7 @@ public class XSSFColor extends ExtendedColor { } XSSFColor other = (XSSFColor)o; - + // Compare each field in ctColor. // Cannot compare ctColor's XML string representation because equivalent // colors may have different relation namespace URI's diff --git a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java index ca78a48edd..5ebda3841d 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkFactory.java @@ -188,8 +188,7 @@ public final class ChunkFactory { } // Now, create the chunk - byte[] contents = IOUtils.safelyAllocate(header.getLength(), MAX_RECORD_LENGTH); - System.arraycopy(data, offset+header.getSizeInBytes(), contents, 0, contents.length); + byte[] contents = IOUtils.safelyClone(data, offset+header.getSizeInBytes(), header.getLength(), MAX_RECORD_LENGTH); Chunk chunk = new Chunk(header, trailer, separator, contents); // Feed in the stuff from chunks_parse_cmds.tbl diff --git a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkSeparator.java b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkSeparator.java index bf49ceac48..ee009db15e 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkSeparator.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkSeparator.java @@ -17,16 +17,17 @@ package org.apache.poi.hdgf.chunks; +import java.util.Arrays; + /** * A separator between the trailer of one chunk, and the * header of the next one */ public final class ChunkSeparator { - protected byte[] separatorData; + final byte[] separatorData; public ChunkSeparator(byte[] data, int offset) { - separatorData = new byte[4]; - System.arraycopy(data, offset, separatorData, 0, 4); + separatorData = Arrays.copyOfRange(data, offset, offset+4); } public String toString() { diff --git a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkTrailer.java b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkTrailer.java index a4c9e178c2..92160abb8e 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkTrailer.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/chunks/ChunkTrailer.java @@ -17,15 +17,16 @@ package org.apache.poi.hdgf.chunks; +import java.util.Arrays; + /** * A trailer that follows a chunk */ public final class ChunkTrailer { - private byte[] trailerData; + private final byte[] trailerData; public ChunkTrailer(byte[] data, int offset) { - trailerData = new byte[8]; - System.arraycopy(data, offset, trailerData, 0, 8); + trailerData = Arrays.copyOfRange(data, offset, offset+8); } public String toString() { diff --git a/src/scratchpad/src/org/apache/poi/hdgf/streams/CompressedStreamStore.java b/src/scratchpad/src/org/apache/poi/hdgf/streams/CompressedStreamStore.java index 0ed5540b5e..a8bea5ccd8 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/streams/CompressedStreamStore.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/streams/CompressedStreamStore.java @@ -38,21 +38,19 @@ public final class CompressedStreamStore extends StreamStore { * We're not sure what this is, but it comes before the * real contents in the de-compressed data */ - private byte[] blockHeader = new byte[4]; + private final byte[] blockHeader; private boolean blockHeaderInContents; - protected byte[] _getCompressedContents() { return compressedContents; } - protected byte[] _getBlockHeader() { return blockHeader; } + byte[] _getCompressedContents() { return compressedContents; } + byte[] _getBlockHeader() { return blockHeader; } /** * Creates a new compressed StreamStore, which will handle * the decompression. */ - protected CompressedStreamStore(byte[] data, int offset, int length) throws IOException { + CompressedStreamStore(byte[] data, int offset, int length) throws IOException { this(decompress(data,offset,length)); - - compressedContents = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); - System.arraycopy(data, offset, compressedContents, 0, length); + compressedContents = IOUtils.safelyClone(data, offset, length, MAX_RECORD_LENGTH); } /** * Handles passing the de-compressed data onto our superclass. diff --git a/src/scratchpad/src/org/apache/poi/hdgf/streams/StreamStore.java b/src/scratchpad/src/org/apache/poi/hdgf/streams/StreamStore.java index 30dead90a3..ad8e13cd56 100644 --- a/src/scratchpad/src/org/apache/poi/hdgf/streams/StreamStore.java +++ b/src/scratchpad/src/org/apache/poi/hdgf/streams/StreamStore.java @@ -34,8 +34,7 @@ public class StreamStore { // TODO - instantiable superclass * Creates a new, non compressed Stream Store */ protected StreamStore(byte[] data, int offset, int length) { - contents = IOUtils.safelyAllocate(length, MAX_RECORD_LENGTH); - System.arraycopy(data, offset, contents, 0, length); + contents = IOUtils.safelyClone(data, offset, length, MAX_RECORD_LENGTH); } protected void prependContentsWith(byte[] b) { diff --git a/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java b/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java index b9f4cc4ee6..c3e2ab0c38 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java +++ b/src/scratchpad/src/org/apache/poi/hmef/CompressedRTF.java @@ -20,7 +20,7 @@ package org.apache.poi.hmef; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LZWDecompresser; @@ -43,18 +43,18 @@ public final class CompressedRTF extends LZWDecompresser { LittleEndian.getInt(COMPRESSED_SIGNATURE); public static final int UNCOMPRESSED_SIGNATURE_INT = LittleEndian.getInt(UNCOMPRESSED_SIGNATURE); - + // The 4096 byte LZW dictionary is pre-loaded with some common // RTF fragments. These come from RTFLIB32.LIB, which ships // with older versions of Visual Studio or the EDK - public static final String LZW_RTF_PRELOAD = + public static final String LZW_RTF_PRELOAD = "{\\rtf1\\ansi\\mac\\deff0\\deftab720{\\fonttbl;}{\\f0\\fnil \\froman \\fswiss " + "\\fmodern \\fscript \\fdecor MS Sans SerifSymbolArialTimes New RomanCourier" + "{\\colortbl\\red0\\green0\\blue0\n\r\\par \\pard\\plain\\f0\\fs20\\b\\i\\u\\tab\\tx"; private int compressedSize; private int decompressedSize; - + public CompressedRTF() { // Out flag has the normal meaning // Length wise, we're 2 longer than we say, so the max len is 18 @@ -76,9 +76,9 @@ public final class CompressedRTF extends LZWDecompresser { decompressedSize = LittleEndian.readInt(src); int compressionType = LittleEndian.readInt(src); /* int dataCRC = */ LittleEndian.readInt(src); - + // TODO - Handle CRC checking on the output side - + // Do we need to do anything? if(compressionType == UNCOMPRESSED_SIGNATURE_INT) { // Nope, nothing fancy to do @@ -92,7 +92,7 @@ public final class CompressedRTF extends LZWDecompresser { // Have it processed super.decompress(src, res); } - + /** * Returns how big the compressed version was. */ @@ -100,7 +100,7 @@ public final class CompressedRTF extends LZWDecompresser { // Return the size less the header return compressedSize - 12; } - + /** * Returns how big the decompressed version was. */ @@ -119,10 +119,10 @@ public final class CompressedRTF extends LZWDecompresser { @Override protected int populateDictionary(byte[] dict) { - // Copy in the RTF constants - byte[] preload = LZW_RTF_PRELOAD.getBytes(Charset.forName("US-ASCII")); + // Copy in the RTF constants + byte[] preload = LZW_RTF_PRELOAD.getBytes(StandardCharsets.US_ASCII); System.arraycopy(preload, 0, dict, 0, preload.length); - + // Start adding new codes after the constants return preload.length; } diff --git a/src/scratchpad/src/org/apache/poi/hmef/attribute/MAPIAttribute.java b/src/scratchpad/src/org/apache/poi/hmef/attribute/MAPIAttribute.java index 159328fe70..695822909a 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/attribute/MAPIAttribute.java +++ b/src/scratchpad/src/org/apache/poi/hmef/attribute/MAPIAttribute.java @@ -21,6 +21,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.poi.hmef.Attachment; @@ -45,7 +46,7 @@ public class MAPIAttribute { private final MAPIProperty property; private final int type; private final byte[] data; - + /** * Constructs a single new attribute from * the contents of the stream @@ -67,21 +68,20 @@ public class MAPIAttribute { public byte[] getData() { return data; } - + public String toString() { String hex; if(data.length <= 16) { hex = HexDump.toHex(data); } else { - byte[] d = new byte[16]; - System.arraycopy(data, 0, d, 0, 16); + byte[] d = Arrays.copyOf(data, 16); hex = HexDump.toHex(d); hex = hex.substring(0, hex.length()-1) + ", ....]"; } - + return property + " " + hex; } - + /** * Parses a MAPI Properties TNEF Attribute, and returns * the list of MAPI Attributes contained within it @@ -101,16 +101,16 @@ public class MAPIAttribute { ); } ByteArrayInputStream inp = new ByteArrayInputStream(parent.getData()); - + // First up, get the number of attributes int count = LittleEndian.readInt(inp); List attrs = new ArrayList<>(); - + // Now, read each one in in turn for(int i=0; i rtf.getDeCompressedSize()) { - this.decompressed = IOUtils.safelyAllocate(rtf.getDeCompressedSize(), MAX_RECORD_LENGTH); - System.arraycopy(tmp, 0, decompressed, 0, decompressed.length); + this.decompressed = IOUtils.safelyClone(tmp, 0, rtf.getDeCompressedSize(), MAX_RECORD_LENGTH); } else { this.decompressed = tmp; } - + // Turn the RTF data into a more useful string this.data = StringUtil.getFromCompressedUnicode(decompressed, 0, decompressed.length); } - + /** * Returns the original, compressed RTF */ public byte[] getRawData() { return super.getData(); } - + /** * Returns the raw uncompressed RTF data */ public byte[] getData() { return decompressed; } - + /** * Returns the uncompressed RTF as a string */ public String getDataString() { return data; } - + public String toString() { return getProperty() + " " + data; } diff --git a/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java b/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java index e969952648..d0019cebf4 100644 --- a/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java +++ b/src/scratchpad/src/org/apache/poi/hmef/dev/HMEFDumper.java @@ -44,7 +44,7 @@ public final class HMEFDumper { if(args.length < 1) { throw new IllegalArgumentException("Filename must be given"); } - + boolean truncatePropData = true; for (String arg : args) { if (arg.equalsIgnoreCase("--full")) { @@ -62,77 +62,77 @@ public final class HMEFDumper { private InputStream inp; private boolean truncatePropertyData; - + public HMEFDumper(InputStream inp) throws IOException { this.inp = inp; - + // Check the signature matches int sig = LittleEndian.readInt(inp); if(sig != HMEFMessage.HEADER_SIGNATURE) { throw new IllegalArgumentException( "TNEF signature not detected in file, " + - "expected " + HMEFMessage.HEADER_SIGNATURE + + "expected " + HMEFMessage.HEADER_SIGNATURE + " but got " + sig ); } - + // Skip over the File ID LittleEndian.readUShort(inp); } - + public void setTruncatePropertyData(boolean truncate) { truncatePropertyData = truncate; } - + private void dump() throws IOException { int level; int attachments = 0; - + while(true) { // Fetch the level level = inp.read(); if(level == TNEFProperty.LEVEL_END_OF_FILE) { break; } - + // Build the attribute TNEFAttribute attr = TNEFAttribute.create(inp); - + // Is it a new attachment? - if(level == TNEFProperty.LEVEL_ATTACHMENT && + if(level == TNEFProperty.LEVEL_ATTACHMENT && attr.getProperty() == TNEFProperty.ID_ATTACHRENDERDATA) { attachments++; System.out.println(); System.out.println("Attachment # " + attachments); System.out.println(); } - + // Print the attribute into System.out.println( "Level " + level + " : Type " + attr.getType() + " : ID " + attr.getProperty() ); - + // Print the contents String indent = " "; - + if(attr instanceof TNEFStringAttribute) { System.out.println(indent + indent + indent + ((TNEFStringAttribute)attr).getString()); } if(attr instanceof TNEFDateAttribute) { System.out.println(indent + indent + indent + ((TNEFDateAttribute)attr).getDate()); } - + System.out.println(indent + "Data of length " + attr.getData().length); if(attr.getData().length > 0) { int len = attr.getData().length; if(truncatePropertyData) { len = Math.min( attr.getData().length, 48 ); } - + int loops = len/16; if(loops == 0) loops = 1; - + for(int i=0; i attrs = MAPIAttribute.create(attr); diff --git a/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java b/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java index 8db0e5b7d2..ab5b5d09e9 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/model/QuillContents.java @@ -69,8 +69,7 @@ public final class QuillContents extends HPBFPart { int from = (int)LittleEndian.getUInt(data, offset+16); int len = (int)LittleEndian.getUInt(data, offset+20); - byte[] bitData = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); - System.arraycopy(data, from, bitData, 0, len); + byte[] bitData = IOUtils.safelyClone(data, from, len, MAX_RECORD_LENGTH); // Create if(bitType.equals("TEXT")) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/blip/Bitmap.java b/src/scratchpad/src/org/apache/poi/hslf/blip/Bitmap.java index 1e944d48ed..afc8cd5481 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/blip/Bitmap.java +++ b/src/scratchpad/src/org/apache/poi/hslf/blip/Bitmap.java @@ -39,9 +39,7 @@ public abstract class Bitmap extends HSLFPictureData { public byte[] getData(){ byte[] rawdata = getRawData(); int prefixLen = 16*getUIDInstanceCount()+1; - byte[] imgdata = IOUtils.safelyAllocate(rawdata.length-prefixLen, rawdata.length); - System.arraycopy(rawdata, prefixLen, imgdata, 0, imgdata.length); - return imgdata; + return IOUtils.safelyClone(rawdata, prefixLen, rawdata.length-prefixLen, rawdata.length); } @Override diff --git a/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java b/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java index 5699bbfeff..1c2d3dc07f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java +++ b/src/scratchpad/src/org/apache/poi/hslf/blip/DIB.java @@ -62,9 +62,9 @@ public final class DIB extends Bitmap { break; default: throw new IllegalArgumentException(signature+" is not a valid instance/signature value for DIB"); - } - } - + } + } + @Override public byte[] getData(){ return addBMPHeader ( super.getData() ); @@ -83,14 +83,14 @@ public final class DIB extends Bitmap { int imageSize = LittleEndian.getInt(data, 0x22 - HEADER_SIZE); int fileSize = data.length + HEADER_SIZE; int offset = fileSize - imageSize; - + // specifies the size, in bytes, of the bitmap file - must add the length of the header - LittleEndian.putInt(header, 2, fileSize); + LittleEndian.putInt(header, 2, fileSize); // Reserved; set to zero LittleEndian.putInt(header, 6, 0); // the offset, i.e. starting address, of the byte where the bitmap data can be found LittleEndian.putInt(header, 10, offset); - + //DIB data is the header + dib bytes byte[] dib = IOUtils.safelyAllocate(header.length + data.length, MAX_RECORD_LENGTH); System.arraycopy(header, 0, dib, 0, header.length); @@ -102,8 +102,7 @@ public final class DIB extends Bitmap { @Override public void setData(byte[] data) throws IOException { //cut off the bitmap file-header - byte[] dib = IOUtils.safelyAllocate(data.length-HEADER_SIZE, data.length); - System.arraycopy(data, HEADER_SIZE, dib, 0, dib.length); + byte[] dib = IOUtils.safelyClone(data, HEADER_SIZE, data.length-HEADER_SIZE, data.length); super.setData(dib); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java b/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java index e4fedb4d98..201f98e603 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java +++ b/src/scratchpad/src/org/apache/poi/hslf/blip/PICT.java @@ -36,8 +36,8 @@ import org.apache.poi.util.Units; */ public final class PICT extends Metafile { private static final POILogger LOG = POILogFactory.getLogger(PICT.class); - - + + @Override public byte[] getData(){ byte[] rawdata = getRawData(); @@ -95,7 +95,7 @@ public final class PICT extends Metafile { // skip the first 512 bytes - they are MAC specific crap final int nOffset = ImageHeaderPICT.PICT_HEADER_OFFSET; ImageHeaderPICT nHeader = new ImageHeaderPICT(data, nOffset); - + Header header = new Header(); int wmfSize = data.length - nOffset; header.setWmfSize(wmfSize); @@ -144,12 +144,12 @@ public final class PICT extends Metafile { break; default: throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT"); - } + } } - - + + /* - * initialize a smaller piece of the array and use the System.arraycopy + * initialize a smaller piece of the array and use the System.arraycopy * call to fill in the rest of the array in an expanding binary fashion */ private static void bytefill(byte[] array, byte value) { @@ -161,7 +161,7 @@ public final class PICT extends Metafile { } for (int i = 1; i < len; i += i) { - System.arraycopy(array, 0, array, i, ((len - i) < i) ? (len - i) : i); + System.arraycopy(array, 0, array, i, Math.min(len - i, i)); } } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java index fc0afe7ece..6b13a07920 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/PPTXMLDump.java @@ -26,6 +26,7 @@ import java.io.OutputStreamWriter; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.util.Arrays; import org.apache.poi.hslf.record.RecordTypes; import org.apache.poi.hslf.usermodel.HSLFSlideShow; @@ -161,20 +162,18 @@ public final class PPTXMLDump { public void dumpPictures(byte[] data, int padding) throws IOException { int pos = 0; while (pos < data.length) { - byte[] header = new byte[PICT_HEADER_SIZE]; - - if(data.length - pos < header.length) { + if(data.length - pos < PICT_HEADER_SIZE) { // corrupt file, cannot read header return; } - System.arraycopy(data, pos, header, 0, header.length); + byte[] header = Arrays.copyOfRange(data, pos, pos + PICT_HEADER_SIZE); int size = LittleEndian.getInt(header, 4) - 17; if(size < 0) { // corrupt file, negative image size return; } - byte[] pictdata = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH); - System.arraycopy(data, pos + PICT_HEADER_SIZE, pictdata, 0, pictdata.length); + + byte[] pictdata = IOUtils.safelyClone(data, pos + PICT_HEADER_SIZE, size, MAX_RECORD_LENGTH); pos += PICT_HEADER_SIZE + size; padding++; diff --git a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java index 7fbed19e0e..230892e61f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java +++ b/src/scratchpad/src/org/apache/poi/hslf/dev/SlideShowDumper.java @@ -58,7 +58,7 @@ public final class SlideShowDumper { private boolean ddfEscher; /** Do we use our own built-in basic escher groker to understand the escher objects? */ private boolean basicEscher; - + private PrintStream out; /** @@ -213,8 +213,7 @@ public void walkTree(int depth, int startPos, int maxLen) throws IOException { final String ind = (indent == 0) ? "%1$s" : "%1$"+indent+"s"; - byte[] contents = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); - System.arraycopy(docstream,pos,contents,0,len); + byte[] contents = IOUtils.safelyClone(docstream, pos, len, MAX_RECORD_LENGTH); DefaultEscherRecordFactory erf = new HSLFEscherRecordFactory(); EscherRecord record = erf.createRecord(contents,0); @@ -229,8 +228,8 @@ public void walkTree(int depth, int startPos, int maxLen) throws IOException { String fmt = ind+"At position %2$d (%2$04x): type is %3$d (%3$04x), len is %4$d (%4$04x) (%5$d) - record claims %6$d"; out.println(String.format(Locale.ROOT, fmt, "", pos, atomType, atomLen, atomLen+8, recordLen)); - - + + // Check for corrupt / lying ones if(recordLen != 8 && (recordLen != (atomLen+8))) { out.println(String.format(Locale.ROOT, ind+"** Atom length of $2d ($3d) doesn't match record length of %4d", "", atomLen, atomLen+8, recordLen)); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java b/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java index 01f641d303..baa17a08ea 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfo.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -39,8 +40,7 @@ public final class AnimationInfo extends RecordContainer { */ protected AnimationInfo(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java index 2f1306a3f8..f89e188f14 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/AnimationInfoAtom.java @@ -21,6 +21,7 @@ import static org.apache.poi.util.GenericRecordUtil.getBitsAsString; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -101,7 +102,7 @@ public final class AnimationInfoAtom extends RecordAtom { /** * record data */ - private byte[] _recdata; + private final byte[] _recdata; /** * Constructs a brand new link related atom record. @@ -125,12 +126,10 @@ public final class AnimationInfoAtom extends RecordAtom { */ protected AnimationInfoAtom(byte[] source, int start, int len) { // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the record data - _recdata = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_recdata,0,len-8); + _recdata = IOUtils.safelyClone(source,start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/BinaryTagDataBlob.java b/src/scratchpad/src/org/apache/poi/hslf/record/BinaryTagDataBlob.java index 160e2bdbdf..79d8d9da7d 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/BinaryTagDataBlob.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/BinaryTagDataBlob.java @@ -17,9 +17,11 @@ package org.apache.poi.hslf.record; -import org.apache.poi.util.LittleEndian; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; + +import org.apache.poi.util.LittleEndian; /** * If we come across a record we know has children of (potential) @@ -44,8 +46,7 @@ public final class BinaryTagDataBlob extends PositionDependentRecordContainer */ protected BinaryTagDataBlob(byte[] source, int start, int len) { // Just grab the header, not the whole contents - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); _type = LittleEndian.getUShort(_header,2); // Find our children diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/CString.java b/src/scratchpad/src/org/apache/poi/hslf/record/CString.java index 5f98cc8a8c..329915f93e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/CString.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/CString.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -86,12 +87,10 @@ public final class CString extends RecordAtom { if(len < 8) { len = 8; } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the text - _text = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_text,0,len-8); + _text = IOUtils.safelyClone(source,start+8, len-8, MAX_RECORD_LENGTH); } /** * Create an empty CString diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ColorSchemeAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ColorSchemeAtom.java index 9bd51ed8ad..8625245844 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ColorSchemeAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ColorSchemeAtom.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -108,11 +109,10 @@ public final class ColorSchemeAtom extends RecordAtom { } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the rgb values - backgroundColourRGB = LittleEndian.getInt(source,start+8+0); + backgroundColourRGB = LittleEndian.getInt(source, start+8); textAndLinesColourRGB = LittleEndian.getInt(source,start+8+4); shadowsColourRGB = LittleEndian.getInt(source,start+8+8); titleTextColourRGB = LittleEndian.getInt(source,start+8+12); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000.java b/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000.java index ed8237b38f..fbf0d3086a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -101,8 +102,7 @@ public final class Comment2000 extends RecordContainer { */ protected Comment2000(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = org.apache.poi.hslf.record.Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000Atom.java b/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000Atom.java index de01e4f9aa..00c3cdef01 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000Atom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Comment2000Atom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Date; import java.util.Map; import java.util.function.Supplier; @@ -72,12 +73,10 @@ public final class Comment2000Atom extends RecordAtom */ protected Comment2000Atom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java index 99b991fa11..680d97c242 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java @@ -192,17 +192,12 @@ public class CurrentUserAtom // Grab the unicode username, if stored int start = 28+(int)usernameLen+4; - int len = 2*(int)usernameLen; - if(_contents.length >= start+len) { - byte[] textBytes = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); - System.arraycopy(_contents,start,textBytes,0,len); - lastEditUser = StringUtil.getFromUnicodeLE(textBytes); + if(_contents.length >= start+2*usernameLen) { + lastEditUser = StringUtil.getFromUnicodeLE(_contents, start, (int)usernameLen); } else { // Fake from the 8 bit version - byte[] textBytes = IOUtils.safelyAllocate(usernameLen, MAX_RECORD_LENGTH); - System.arraycopy(_contents,28,textBytes,0,(int)usernameLen); - lastEditUser = StringUtil.getFromCompressedUnicode(textBytes,0,(int)usernameLen); + lastEditUser = StringUtil.getFromCompressedUnicode(_contents, 28, (int)usernameLen); } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DocInfoListContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/DocInfoListContainer.java index e10e864724..a43e475034 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DocInfoListContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DocInfoListContainer.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -38,8 +39,7 @@ public final class DocInfoListContainer extends RecordContainer { */ protected DocInfoListContainer(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source,start,start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Document.java b/src/scratchpad/src/org/apache/poi/hslf/record/Document.java index 4fd37a9b35..114f5b3bb6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Document.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Document.java @@ -17,11 +17,12 @@ package org.apache.poi.hslf.record; -import org.apache.poi.util.POILogger; - import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; + +import org.apache.poi.util.POILogger; /** * Master container for Document. There is one of these for every @@ -46,24 +47,24 @@ public final class Document extends PositionDependentRecordContainer * Returns the DocumentAtom of this Document */ public DocumentAtom getDocumentAtom() { return documentAtom; } - + /** * Returns the Environment of this Notes, which lots of * settings for the document in it */ public Environment getEnvironment() { return environment; } - + /** * Returns the PPDrawingGroup, which holds an Escher Structure * that contains information on pictures in the slides. */ public PPDrawingGroup getPPDrawingGroup() { return ppDrawing; } - + /** * Returns the ExObjList, which holds the references to * external objects used in the slides. This may be null, if * there are no external references. - * + * * @param create if true, create an ExObjList if it doesn't exist */ public ExObjList getExObjList(boolean create) { @@ -126,8 +127,7 @@ public final class Document extends PositionDependentRecordContainer */ /* package */ Document(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DocumentEncryptionAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/DocumentEncryptionAtom.java index abcf03628e..bb498f2088 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DocumentEncryptionAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DocumentEncryptionAtom.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -51,8 +52,7 @@ public final class DocumentEncryptionAtom extends PositionDependentRecordAtom { */ protected DocumentEncryptionAtom(byte[] source, int start, int len) { // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source,start,start+8); ByteArrayInputStream bis = new ByteArrayInputStream(source, start+8, len-8); try (LittleEndianInputStream leis = new LittleEndianInputStream(bis)) { @@ -67,10 +67,10 @@ public final class DocumentEncryptionAtom extends PositionDependentRecordAtom { LittleEndian.putShort(_header, 0, (short)0x000F); LittleEndian.putShort(_header, 2, (short)_type); // record length not yet known ... - + ei = new EncryptionInfo(EncryptionMode.cryptoAPI); } - + /** * Initializes the encryption settings * @@ -79,7 +79,7 @@ public final class DocumentEncryptionAtom extends PositionDependentRecordAtom { public void initializeEncryptionInfo(int keyBits) { ei = new EncryptionInfo(EncryptionMode.cryptoAPI, CipherAlgorithm.rc4, HashAlgorithm.sha1, keyBits, -1, null); } - + /** * Return the length of the encryption key, in bits */ @@ -100,8 +100,8 @@ public final class DocumentEncryptionAtom extends PositionDependentRecordAtom { public EncryptionInfo getEncryptionInfo() { return ei; } - - + + /** * We are of type 12052 */ @@ -119,10 +119,10 @@ public final class DocumentEncryptionAtom extends PositionDependentRecordAtom { bos.writeShort(ei.getVersionMajor()); bos.writeShort(ei.getVersionMinor()); bos.writeInt(ei.getEncryptionFlags()); - + ((CryptoAPIEncryptionHeader)ei.getHeader()).write(bos); ((CryptoAPIEncryptionVerifier)ei.getVerifier()).write(bos); - + // Header LittleEndian.putInt(_header, 4, bos.getWriteIndex()); out.write(_header); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java b/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java index 285f824b43..db7a0cb943 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java @@ -17,9 +17,11 @@ package org.apache.poi.hslf.record; -import org.apache.poi.util.LittleEndian; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; + +import org.apache.poi.util.LittleEndian; /** * If we come across a record we know has children of (potential) @@ -44,8 +46,7 @@ public final class DummyPositionSensitiveRecordWithChildren extends PositionDepe */ protected DummyPositionSensitiveRecordWithChildren(byte[] source, int start, int len) { // Just grab the header, not the whole contents - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source,start,start+8); _type = LittleEndian.getUShort(_header,2); // Find our children diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java b/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java index 18dea58923..cc99aa2f98 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java @@ -17,9 +17,11 @@ package org.apache.poi.hslf.record; -import org.apache.poi.util.LittleEndian; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; + +import org.apache.poi.util.LittleEndian; /** * If we come across a record we know has children of (potential) @@ -39,8 +41,7 @@ public final class DummyRecordWithChildren extends RecordContainer */ protected DummyRecordWithChildren(byte[] source, int start, int len) { // Just grab the header, not the whole contents - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); _type = LittleEndian.getUShort(_header,2); // Find our children diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Environment.java b/src/scratchpad/src/org/apache/poi/hslf/record/Environment.java index eb66f2a288..a8bebeaeaa 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Environment.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Environment.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; /** * Environment, which contains lots of settings for the document. @@ -47,8 +48,7 @@ public final class Environment extends PositionDependentRecordContainer */ protected Environment(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExControlAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExControlAtom.java index 93942674af..58d27ef340 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExControlAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExControlAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -63,8 +64,7 @@ public final class ExControlAtom extends RecordAtom { */ protected ExControlAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source, start, _header, 0, 8); + _header = Arrays.copyOfRange(source, start, start+8); _id = LittleEndian.getInt(source, start + 8); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbed.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbed.java index 099aeb854b..486a9bf235 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbed.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbed.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -49,8 +50,7 @@ public class ExEmbed extends RecordContainer { */ protected ExEmbed(final byte[] source, final int start, final int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -66,9 +66,9 @@ public class ExEmbed extends RecordContainer { this(); _children[0] = this.embedAtom = embedAtom; } - - - + + + /** * Create a new ExEmbed, with blank fields */ @@ -163,7 +163,7 @@ public class ExEmbed extends RecordContainer { /** * Gets the OLE Programmatic Identifier. - * + * * @return the OLE Programmatic Identifier. */ public String getProgId() { @@ -175,8 +175,8 @@ public class ExEmbed extends RecordContainer { this.progId.setText(progId); } - - + + /** * Gets the name that appears in the paste special dialog. * @@ -190,7 +190,7 @@ public class ExEmbed extends RecordContainer { this.clipboardName = safeCString(this.clipboardName, 0x3); this.clipboardName.setText(clipboardName); } - + /** * Returns the type (held as a little endian in bytes 3 and 4) * that this class handles. @@ -213,7 +213,7 @@ public class ExEmbed extends RecordContainer { public void writeOut(final OutputStream out) throws IOException { writeOut(_header[0],_header[1],getRecordType(),_children,out); } - + private CString safeCString(CString oldStr, int optionsId) { CString newStr = oldStr; if (newStr == null) { @@ -233,7 +233,7 @@ public class ExEmbed extends RecordContainer { if (!found) { appendChildRecord(newStr); } - + return newStr; } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbedAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbedAtom.java index e72f052347..f3c3a48f43 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbedAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExEmbedAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -94,12 +95,10 @@ public class ExEmbedAtom extends RecordAtom { */ protected ExEmbedAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source,start+8,len-8, MAX_RECORD_LENGTH); // Must be at least 8 bytes long if(_data.length < 8) { @@ -130,7 +129,7 @@ public class ExEmbedAtom extends RecordAtom { public void setCantLockServerB(boolean cantBeLocked) { _data[4] = (byte)(cantBeLocked ? 1 : 0); } - + /** * Gets whether it is not required to send the dimensions to the embedded object. * diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlink.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlink.java index 37c47824e4..4dabe4cc82 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlink.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlink.java @@ -18,12 +18,13 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; /** - * This class represents the data of a link in the document. + * This class represents the data of a link in the document. * @author Nick Burch */ public class ExHyperlink extends RecordContainer { @@ -35,12 +36,12 @@ public class ExHyperlink extends RecordContainer { private ExHyperlinkAtom linkAtom; private CString linkDetailsA; private CString linkDetailsB; - - /** + + /** * Returns the ExHyperlinkAtom of this link - */ + */ public ExHyperlinkAtom getExHyperlinkAtom() { return linkAtom; } - + /** * Returns the URL of the link. * @@ -68,7 +69,7 @@ public class ExHyperlink extends RecordContainer { linkDetailsB.setText(url); } } - + public void setLinkOptions(int options) { if(linkDetailsB != null) { linkDetailsB.setOptions(options); @@ -80,7 +81,7 @@ public class ExHyperlink extends RecordContainer { linkDetailsA.setText(title); } } - + /** * Get the link details (field A) */ @@ -94,13 +95,12 @@ public class ExHyperlink extends RecordContainer { return linkDetailsB == null ? null : linkDetailsB.getText(); } - /** + /** * Set things up, and find our more interesting children */ protected ExHyperlink(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -111,7 +111,7 @@ public class ExHyperlink extends RecordContainer { * Go through our child records, picking out the ones that are * interesting, and saving those for use by the easy helper * methods. - */ + */ private void findInterestingChildren() { // First child should be the ExHyperlinkAtom @@ -138,11 +138,11 @@ public class ExHyperlink extends RecordContainer { public ExHyperlink() { _header = new byte[8]; _children = new org.apache.poi.hslf.record.Record[3]; - + // Setup our header block _header[0] = 0x0f; // We are a container record LittleEndian.putShort(_header, 2, (short)_type); - + // Setup our child records CString csa = new CString(); CString csb = new CString(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlinkAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlinkAtom.java index bc99430998..ba63ac5da6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlinkAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExHyperlinkAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -70,12 +71,10 @@ public final class ExHyperlinkAtom extends RecordAtom { */ protected ExHyperlinkAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); // Must be at least 4 bytes long if(_data.length < 4) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java index b83e0f8184..37b60ca9e2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExMCIMovie.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -39,8 +40,7 @@ public class ExMCIMovie extends RecordContainer { // TODO - instantiable supercl */ protected ExMCIMovie(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source, start, _header, 0, 8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source, start + 8, len - 8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java index b8db11ad78..486b976f66 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExMediaAtom.java @@ -21,6 +21,7 @@ import static org.apache.poi.util.GenericRecordUtil.getBitsAsString; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -89,12 +90,10 @@ public final class ExMediaAtom extends RecordAtom */ protected ExMediaAtom(byte[] source, int start, int len) { // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the record data - _recdata = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_recdata,0,len-8); + _recdata = IOUtils.safelyClone(source,start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjList.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjList.java index f57461cf18..bee803a21b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjList.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjList.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; @@ -30,15 +31,15 @@ import org.apache.poi.util.LittleEndian; public class ExObjList extends RecordContainer { private byte[] _header; private static final long _type = RecordTypes.ExObjList.typeID; - + // Links to our more interesting children - private ExObjListAtom exObjListAtom; - - /** + private ExObjListAtom exObjListAtom; + + /** * Returns the ExObjListAtom of this list - */ + */ public ExObjListAtom getExObjListAtom() { return exObjListAtom; } - + /** * Returns all the ExHyperlinks */ @@ -53,13 +54,12 @@ public class ExObjList extends RecordContainer { return links.toArray(new ExHyperlink[0]); } - /** + /** * Set things up, and find our more interesting children */ protected ExObjList(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -70,7 +70,7 @@ public class ExObjList extends RecordContainer { * Go through our child records, picking out the ones that are * interesting, and saving those for use by the easy helper * methods. - */ + */ private void findInterestingChildren() { // First child should be the atom if(_children[0] instanceof ExObjListAtom) { @@ -79,18 +79,18 @@ public class ExObjList extends RecordContainer { throw new IllegalStateException("First child record wasn't a ExObjListAtom, was of type " + _children[0].getRecordType()); } } - + /** * Create a new ExObjList, with blank fields */ public ExObjList() { _header = new byte[8]; _children = new org.apache.poi.hslf.record.Record[1]; - + // Setup our header block _header[0] = 0x0f; // We are a container record LittleEndian.putShort(_header, 2, (short)_type); - + // Setup our child records _children[0] = new ExObjListAtom(); findInterestingChildren(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjListAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjListAtom.java index 0c345a286b..5fab662c42 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjListAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjListAtom.java @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -58,7 +59,7 @@ public class ExObjListAtom extends RecordAtom LittleEndian.putShort(_header, 2, (short)getRecordType()); LittleEndian.putInt(_header, 4, _data.length); - + // It is fine for the other values to be zero } @@ -72,13 +73,11 @@ public class ExObjListAtom extends RecordAtom */ protected ExObjListAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); - + _header = Arrays.copyOfRange(source, start, start+8); + // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); - + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); + // Must be at least 4 bytes long if(_data.length < 4) { throw new IllegalArgumentException("The length of the data for a ExObjListAtom must be at least 4 bytes, but was only " + _data.length); @@ -101,7 +100,7 @@ public class ExObjListAtom extends RecordAtom public void setObjectIDSeed(int seed) { LittleEndian.putInt(_data,0,seed); } - + /** * Gets the record type. * @return the record type. diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjRefAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjRefAtom.java index 930f54c380..a1e3d168c8 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExObjRefAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExObjRefAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -55,17 +56,14 @@ public final class ExObjRefAtom extends RecordAtom { /** * Build an instance of ExObjRefAtom from on-disk data - * + * * @param source the source data as a byte array. * @param start the start offset into the byte array. * @param len the length of the slice in the byte array. */ protected ExObjRefAtom(byte[] source, int start, int len) { - _header = new byte[8]; - int offset = start; - System.arraycopy(source,start,_header,0,8); - offset += _header.length; - exObjIdRef = (int)LittleEndian.getUInt(source, offset); + _header = Arrays.copyOfRange(source, start, start+8); + exObjIdRef = (int)LittleEndian.getUInt(source, start+8); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjAtom.java index b8c77a747c..321f09cb41 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjAtom.java @@ -22,6 +22,7 @@ import static org.apache.poi.util.GenericRecordUtil.safeEnum; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -187,12 +188,10 @@ public class ExOleObjAtom extends RecordAtom { */ protected ExOleObjAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); // Must be at least 24 bytes long if(_data.length < 24) { @@ -258,7 +257,7 @@ public class ExOleObjAtom extends RecordAtom { /** * Gets the type of OLE object. - * + * * @return the sub-type, one of the {@code SUBTYPE_*} constants. */ public int getSubType() { @@ -303,7 +302,7 @@ public class ExOleObjAtom extends RecordAtom { // Even though this is a mere boolean, KOffice's code says it's an int. return LittleEndian.getInt(_data, 20) != 0; } - + /** * Gets misc options (the last four bytes in the atom). * diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjStg.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjStg.java index 3fdfa04ecd..4fc8a838e0 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjStg.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExOleObjStg.java @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; import java.util.zip.DeflaterOutputStream; @@ -51,7 +52,7 @@ public class ExOleObjStg extends PositionDependentRecordAtom implements PersistR * Record data. */ private byte[] _data; - + /** * Constructs a new empty storage container. */ @@ -74,12 +75,10 @@ public class ExOleObjStg extends PositionDependentRecordAtom implements PersistR */ protected ExOleObjStg(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } public boolean isCompressed() { @@ -155,7 +154,7 @@ public class ExOleObjStg extends PositionDependentRecordAtom implements PersistR public int getRecordInstance() { return (LittleEndian.getUShort(_header, 0) >>> 4); } - + /** * Write the contents of the record back, so it can be written * to disk. diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java index 1e9031cba2..bebb2aecfe 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/ExVideoContainer.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -41,8 +42,7 @@ public final class ExVideoContainer extends RecordContainer { */ protected ExVideoContainer(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java index 4bd0b0547c..aef98a81c1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -47,8 +48,7 @@ public final class FontCollection extends RecordContainer { private byte[] _header; /* package */ FontCollection(byte[] source, int start, int len) { - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); _children = Record.findChildRecords(source,start+8,len-8); @@ -86,10 +86,10 @@ public final class FontCollection extends RecordContainer { /** * Add font with the given FontInfo configuration to the font collection. - * The returned FontInfo contains the HSLF specific details and the collection + * The returned FontInfo contains the HSLF specific details and the collection * uniquely contains fonts based on their typeface, i.e. calling the method with FontInfo * objects having the same name results in the same HSLFFontInfo reference. - * + * * @param fontInfo the FontInfo configuration, can be a instance of {@link HSLFFontInfo}, * {@link HSLFFontInfoPredefined} or a custom implementation * @return the register HSLFFontInfo object @@ -168,9 +168,9 @@ public final class FontCollection extends RecordContainer { /** * Lookup a FontInfo object by its typeface - * + * * @param typeface the full font name - * + * * @return the HSLFFontInfo for the given name or {@code null} if not found */ public HSLFFontInfo getFontInfo(String typeface) { @@ -195,9 +195,9 @@ public final class FontCollection extends RecordContainer { /** * Lookup a FontInfo object by its internal font index - * + * * @param index the internal font index - * + * * @return the HSLFFontInfo for the given index or {@code null} if not found */ public HSLFFontInfo getFontInfo(int index) { @@ -208,7 +208,7 @@ public final class FontCollection extends RecordContainer { } return null; } - + /** * @return the number of registered fonts */ diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontEmbeddedData.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontEmbeddedData.java index f28bb3f990..b9acb2e3bf 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontEmbeddedData.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontEmbeddedData.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -63,12 +64,10 @@ public class FontEmbeddedData extends RecordAtom implements FontFacet { */ /* package */ FontEmbeddedData(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); // Must be at least 4 bytes long if(_data.length < 4) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontEntityAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontEntityAtom.java index 0e9f732833..0d576f6a6a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontEntityAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontEntityAtom.java @@ -62,7 +62,7 @@ public final class FontEntityAtom extends RecordAtom { /** * record header */ - private final byte[] _header = new byte[8]; + private final byte[] _header; /** * record data @@ -74,11 +74,10 @@ public final class FontEntityAtom extends RecordAtom { */ /* package */ FontEntityAtom(byte[] source, int start, int len) { // Get the header - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the record data - _recdata = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_recdata,0,len-8); + _recdata = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** @@ -86,6 +85,7 @@ public final class FontEntityAtom extends RecordAtom { */ public FontEntityAtom() { _recdata = new byte[68]; + _header = new byte[8]; LittleEndian.putShort(_header, 2, (short)getRecordType()); LittleEndian.putInt(_header, 4, _recdata.length); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/HSLFEscherClientDataRecord.java b/src/scratchpad/src/org/apache/poi/hslf/record/HSLFEscherClientDataRecord.java index 3f1b0fbe3d..af0e0a5ec5 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/HSLFEscherClientDataRecord.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/HSLFEscherClientDataRecord.java @@ -68,8 +68,7 @@ public class HSLFEscherClientDataRecord extends EscherClientDataRecord { @Override public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { int bytesRemaining = readHeader( data, offset ); - byte[] remainingData = IOUtils.safelyAllocate(bytesRemaining, MAX_RECORD_LENGTH); - System.arraycopy(data, offset+8, remainingData, 0, bytesRemaining); + byte[] remainingData = IOUtils.safelyClone(data, offset+8, bytesRemaining, MAX_RECORD_LENGTH); setRemainingData(remainingData); return bytesRemaining + 8; } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersAtom.java index f175848ff1..42f8d017ca 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersAtom.java @@ -22,6 +22,7 @@ import static org.apache.poi.util.GenericRecordUtil.safeEnum; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -142,12 +143,10 @@ public final class HeadersFootersAtom extends RecordAtom { */ protected HeadersFootersAtom(byte[] source, int start, int len) { // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the record data - _recdata = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_recdata,0,len-8); + _recdata = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersContainer.java index b974bb35bb..492933be48 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/HeadersFootersContainer.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -59,8 +60,7 @@ public final class HeadersFootersContainer extends RecordContainer { protected HeadersFootersContainer(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); _children = Record.findChildRecords(source,start+8,len-8); findInterestingChildren(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfo.java b/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfo.java index 440005b479..76b84c554a 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfo.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfo.java @@ -18,6 +18,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogger; @@ -31,22 +32,21 @@ import org.apache.poi.util.POILogger; public class InteractiveInfo extends RecordContainer { private byte[] _header; private static final long _type = RecordTypes.InteractiveInfo.typeID; - + // Links to our more interesting children private InteractiveInfoAtom infoAtom; - - /** + + /** * Returns the InteractiveInfoAtom of this InteractiveInfo - */ + */ public InteractiveInfoAtom getInteractiveInfoAtom() { return infoAtom; } - - /** + + /** * Set things up, and find our more interesting children */ protected InteractiveInfo(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -57,7 +57,7 @@ public class InteractiveInfo extends RecordContainer { * Go through our child records, picking out the ones that are * interesting, and saving those for use by the easy helper * methods. - */ + */ private void findInterestingChildren() { // First child should be the InteractiveInfoAtom if (_children == null || _children.length == 0 || !(_children[0] instanceof InteractiveInfoAtom)) { @@ -67,18 +67,18 @@ public class InteractiveInfo extends RecordContainer { infoAtom = (InteractiveInfoAtom)_children[0]; } - + /** * Create a new InteractiveInfo, with blank fields */ public InteractiveInfo() { _header = new byte[8]; _children = new org.apache.poi.hslf.record.Record[1]; - + // Setup our header block _header[0] = 0x0f; // We are a container record LittleEndian.putShort(_header, 2, (short)_type); - + // Setup our child records infoAtom = new InteractiveInfoAtom(); _children[0] = infoAtom; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfoAtom.java index 71a5adc123..3543010612 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/InteractiveInfoAtom.java @@ -22,6 +22,7 @@ import static org.apache.poi.util.GenericRecordUtil.safeEnum; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -153,12 +154,10 @@ public class InteractiveInfoAtom extends RecordAtom { */ protected InteractiveInfoAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); // Must be at least 16 bytes long if(_data.length < 16) { diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/MainMaster.java b/src/scratchpad/src/org/apache/poi/hslf/record/MainMaster.java index 8451193c33..e771fcf57d 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/MainMaster.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/MainMaster.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; /** * Master slide @@ -57,8 +58,7 @@ public final class MainMaster extends SheetContainer { */ protected MainMaster(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -66,19 +66,19 @@ public final class MainMaster extends SheetContainer { ArrayList tx = new ArrayList<>(); ArrayList clr = new ArrayList<>(); // Find the interesting ones in there - for(int i=0; i<_children.length; i++) { - if(_children[i] instanceof SlideAtom) { - slideAtom = (SlideAtom)_children[i]; - } else if(_children[i] instanceof PPDrawing) { - ppDrawing = (PPDrawing)_children[i]; - } else if(_children[i] instanceof TxMasterStyleAtom) { - tx.add( (TxMasterStyleAtom)_children[i] ); - } else if(_children[i] instanceof ColorSchemeAtom) { - clr.add( (ColorSchemeAtom)_children[i] ); + for (Record child : _children) { + if (child instanceof SlideAtom) { + slideAtom = (SlideAtom) child; + } else if (child instanceof PPDrawing) { + ppDrawing = (PPDrawing) child; + } else if (child instanceof TxMasterStyleAtom) { + tx.add((TxMasterStyleAtom) child); + } else if (child instanceof ColorSchemeAtom) { + clr.add((ColorSchemeAtom) child); } - if(ppDrawing != null && _children[i] instanceof ColorSchemeAtom) { - _colorScheme = (ColorSchemeAtom)_children[i]; + if (ppDrawing != null && child instanceof ColorSchemeAtom) { + _colorScheme = (ColorSchemeAtom) child; } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java index 0e1b4dcab1..8da3d439b6 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -76,12 +77,10 @@ public final class MasterTextPropAtom extends RecordAtom { */ protected MasterTextPropAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); try { read(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java b/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java index 8695b25f0b..4854426283 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; /** * Master container for Notes. There is one of these for every page of @@ -53,23 +54,22 @@ public final class Notes extends SheetContainer */ protected Notes(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); // Find the interesting ones in there - for(int i=0; i<_children.length; i++) { - if(_children[i] instanceof NotesAtom) { - notesAtom = (NotesAtom)_children[i]; + for (Record child : _children) { + if (child instanceof NotesAtom) { + notesAtom = (NotesAtom) child; } - if(_children[i] instanceof PPDrawing) { - ppDrawing = (PPDrawing)_children[i]; + if (child instanceof PPDrawing) { + ppDrawing = (PPDrawing) child; + } + if (ppDrawing != null && child instanceof ColorSchemeAtom) { + _colorScheme = (ColorSchemeAtom) child; } - if(ppDrawing != null && _children[i] instanceof ColorSchemeAtom) { - _colorScheme = (ColorSchemeAtom)_children[i]; - } } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/NotesAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/NotesAtom.java index 40b4554955..655898adb8 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/NotesAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/NotesAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -70,33 +71,19 @@ public final class NotesAtom extends RecordAtom if(len < 8) { len = 8; } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the slide ID slideID = LittleEndian.getInt(source,start+8); // Grok the flags, stored as bits int flags = LittleEndian.getUShort(source,start+12); - if((flags&4) == 4) { - followMasterBackground = true; - } else { - followMasterBackground = false; - } - if((flags&2) == 2) { - followMasterScheme = true; - } else { - followMasterScheme = false; - } - if((flags&1) == 1) { - followMasterObjects = true; - } else { - followMasterObjects = false; - } + followMasterBackground = (flags & 4) == 4; + followMasterScheme = (flags & 2) == 2; + followMasterObjects = (flags & 1) == 1; // There might be 2 more bytes, which are a reserved field - reserved = IOUtils.safelyAllocate(len-14, MAX_RECORD_LENGTH); - System.arraycopy(source,start+14,reserved,0,reserved.length); + reserved = IOUtils.safelyClone(source, start+14, len-14, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/OEPlaceholderAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/OEPlaceholderAtom.java index f25164d2a2..af3595fecb 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/OEPlaceholderAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/OEPlaceholderAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -28,7 +29,7 @@ import org.apache.poi.util.LittleEndian; /** * OEPlaceholderAtom (3011).

- * + * * An atom record that specifies whether a shape is a placeholder shape. * * @see Placeholder @@ -77,10 +78,8 @@ public final class OEPlaceholderAtom extends RecordAtom{ * Build an instance of {@code OEPlaceholderAtom} from on-disk data */ protected OEPlaceholderAtom(byte[] source, int start, int len) { - _header = new byte[8]; - int offset = start; - System.arraycopy(source,start,_header,0,8); - offset += _header.length; + _header = Arrays.copyOfRange(source, start, start+8); + int offset = start+8; placementId = LittleEndian.getInt(source, offset); offset += 4; placeholderId = LittleEndian.getUByte(source, offset); offset++; @@ -96,7 +95,7 @@ public final class OEPlaceholderAtom extends RecordAtom{ /** * Returns the placement Id.

- * + * * The placement Id is a number assigned to the placeholder. It goes from -1 to the number of placeholders. * It SHOULD be unique among all PlacholderAtom records contained in the corresponding slide. * The value 0xFFFFFFFF specifies that the corresponding shape is not a placeholder shape. diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/OutlineTextRefAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/OutlineTextRefAtom.java index e0c368825b..e521c74383 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/OutlineTextRefAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/OutlineTextRefAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -55,8 +56,7 @@ public final class OutlineTextRefAtom extends RecordAtom { */ protected OutlineTextRefAtom(byte[] source, int start, int len) { // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the record data _index = LittleEndian.getInt(source, start+8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/PPDrawing.java b/src/scratchpad/src/org/apache/poi/hslf/record/PPDrawing.java index 2bb40a812a..77af5d467f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/PPDrawing.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/PPDrawing.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -114,15 +115,13 @@ public final class PPDrawing extends RecordAtom implements Iterable getSlideLocationsLookup() { return Collections.unmodifiableMap(_slideLocations); } - + /** * Create a new holder for a PersistPtr record */ @@ -103,8 +104,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom if(len < 8) { len = 8; } // Treat as an atom, grab and hold everything - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); _type = LittleEndian.getUShort(_header,2); // Try to make sense of the data part: @@ -115,8 +115,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom // count * 32 bit offsets // Repeat as many times as you have data _slideLocations = new HashMap<>(); - _ptrData = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_ptrData,0,_ptrData.length); + _ptrData = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); int pos = 0; while(pos < _ptrData.length) { @@ -127,7 +126,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom // Remaining 12 bits = offset count int offset_no = persistIdFld.getValue(info); int offset_count = cntPersistFld.getValue(info); - + // Wind on by the 4 byte info header pos += 4; @@ -145,13 +144,13 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom /** * remove all slide references - * + * * Convenience method provided, for easier reviewing of invocations */ public void clear() { _slideLocations.clear(); } - + /** * Adds a new slide, notes or similar, to be looked up by this. */ @@ -187,7 +186,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom private void normalizePersistDirectory() { TreeMap orderedSlideLocations = new TreeMap<>(_slideLocations); - + @SuppressWarnings("resource") BufAccessBAOS bos = new BufAccessBAOS(); // NOSONAR byte[] intbuf = new byte[4]; @@ -200,7 +199,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom // Building the info block // First 20 bits = offset number = slide ID (persistIdFld, i.e. first slide ID of a continuous group) // Remaining 12 bits = offset count = 1 (cntPersistFld, i.e. continuous entries in a group) - + if (lastSlideId+1 == nextSlideId) { // use existing PersistDirectoryEntry, need to increase entry count assert(lastPersistEntry != -1); @@ -225,14 +224,14 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom throw new HSLFException(e); } } - + // Save the new ptr data _ptrData = bos.toByteArray(); // Update the atom header LittleEndian.putInt(_header,4,bos.size()); } - + /** * Write the contents of the record back, so it can be written * to disk @@ -243,7 +242,7 @@ public final class PersistPtrHolder extends PositionDependentRecordAtom out.write(_header); out.write(_ptrData); } - + private static class BufAccessBAOS extends ByteArrayOutputStream { public byte[] getBuf() { return buf; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java index dafc3972fe..4e738a7601 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -76,8 +77,7 @@ public abstract class RecordContainer extends Record */ private int appendChild(Record newChild) { // Copy over, and pop the child in at the end - Record[] nc = new org.apache.poi.hslf.record.Record[(_children.length + 1)]; - System.arraycopy(_children, 0, nc, 0, _children.length); + Record[] nc = Arrays.copyOf(_children, _children.length+1, org.apache.poi.hslf.record.Record[].class); // Switch the arrays nc[_children.length] = newChild; _children = nc; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/RoundTripHFPlaceholder12.java b/src/scratchpad/src/org/apache/poi/hslf/record/RoundTripHFPlaceholder12.java index f2c8f03b82..283c65ba43 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/RoundTripHFPlaceholder12.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/RoundTripHFPlaceholder12.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -55,7 +56,7 @@ public final class RoundTripHFPlaceholder12 extends RecordAtom { LittleEndian.putInt(_header, 4, 8); _placeholderId = 0; } - + /** * Constructs the comment atom record from its source data. * @@ -65,8 +66,7 @@ public final class RoundTripHFPlaceholder12 extends RecordAtom { */ protected RoundTripHFPlaceholder12(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. _placeholderId = source[start+8]; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SSSlideInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/SSSlideInfoAtom.java index e0d28e5dfa..7bd098f92b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SSSlideInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SSSlideInfoAtom.java @@ -21,6 +21,7 @@ import static org.apache.poi.util.GenericRecordUtil.getBitsAsString; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -31,7 +32,7 @@ import org.apache.poi.util.LittleEndianConsts; /** * A SlideShowSlideInfo Atom (type 1017).
*
- * + * * An atom record that specifies which transition effects to perform * during a slide show, and how to advance to the next presentation slide.
*
@@ -70,30 +71,30 @@ public class SSSlideInfoAtom extends RecordAtom { * manually advanced by the user during the slide show. */ public static final int MANUAL_ADVANCE_BIT = 1 << 0; - + /** - * A bit that specifies whether the corresponding slide is + * A bit that specifies whether the corresponding slide is * hidden and is not displayed during the slide show. */ public static final int HIDDEN_BIT = 1 << 2; - + /** * A bit that specifies whether to play the sound specified by soundIfRef. */ public static final int SOUND_BIT = 1 << 4; - + /** * A bit that specifies whether the sound specified by soundIdRef is * looped continuously when playing until the next sound plays. */ public static final int LOOP_SOUND_BIT = 1 << 6; - + /** - * A bit that specifies whether to stop any currently playing + * A bit that specifies whether to stop any currently playing * sound when the transition starts. */ public static final int STOP_SOUND_BIT = 1 << 8; - + /** * A bit that specifies whether the slide will automatically * advance after slideTime milliseconds during the slide show. @@ -102,10 +103,10 @@ public class SSSlideInfoAtom extends RecordAtom { /** * A bit that specifies whether to display the cursor during - * the slide show. + * the slide show. */ public static final int CURSOR_VISIBLE_BIT = 1 << 12; - + // public static int RESERVED1_BIT = 1 << 1; // public static int RESERVED2_BIT = 1 << 3; // public static int RESERVED3_BIT = 1 << 5; @@ -144,9 +145,9 @@ public class SSSlideInfoAtom extends RecordAtom { * less than or equal to 86399000. It MUST be ignored unless AUTO_ADVANCE_BIT is TRUE. */ private int _slideTime; - + /** - * A SoundIdRef that specifies which sound to play when the transition starts. + * A SoundIdRef that specifies which sound to play when the transition starts. */ private int _soundIdRef; @@ -155,23 +156,23 @@ public class SSSlideInfoAtom extends RecordAtom { * there are further restriction and specification of this field. */ private short _effectDirection; // byte - + /** * A byte that specifies which transition is used when transitioning to the - * next presentation slide during a slide show. Exact rendering of any transition is + * next presentation slide during a slide show. Exact rendering of any transition is * determined by the rendering application. As such, the same transition can have * many variations depending on the implementation. */ private short _effectType; // byte - + /** * Various flags - see bitmask for more details */ private short _effectTransitionFlags; - + /** * A byte value that specifies how long the transition takes to run. - * (0x00 = 0.75 seconds, 0x01 = 0.5 seconds, 0x02 = 0.25 seconds) + * (0x00 = 0.75 seconds, 0x01 = 0.5 seconds, 0x02 = 0.25 seconds) */ private short _speed; // byte private byte[] _unused; // 3-byte @@ -184,18 +185,18 @@ public class SSSlideInfoAtom extends RecordAtom { LittleEndian.putShort(_header, 6, (short)0); _unused = new byte[3]; } - + public SSSlideInfoAtom(byte[] source, int offset, int len) { int ofs = offset; // Sanity Checking if(len != 24) len = 24; assert(source.length >= offset+len); - + // Get the header - _header = LittleEndian.getByteArray(source,ofs,8); + _header = Arrays.copyOfRange(source, ofs, ofs+8); ofs += _header.length; - + assert(LittleEndian.getShort(_header, 0) == 0); assert(LittleEndian.getShort(_header, 2) == RecordTypes.SSSlideInfoAtom.typeID); assert(LittleEndian.getShort(_header, 4) == 0x10); @@ -214,7 +215,7 @@ public class SSSlideInfoAtom extends RecordAtom { ofs += LittleEndianConsts.SHORT_SIZE; _speed = LittleEndian.getUByte(source, ofs); ofs += LittleEndianConsts.BYTE_SIZE; - _unused = LittleEndian.getByteArray(source,ofs,3); + _unused = Arrays.copyOfRange(source,ofs,ofs+3); } /** @@ -232,7 +233,7 @@ public class SSSlideInfoAtom extends RecordAtom { out.write(byteBuf); LittleEndian.putUByte(byteBuf, 0, _effectType); out.write(byteBuf); - + writeLittleEndian(_effectTransitionFlags, out); LittleEndian.putUByte(byteBuf, 0, _speed); out.write(byteBuf); @@ -240,7 +241,7 @@ public class SSSlideInfoAtom extends RecordAtom { assert(_unused.length == 3); out.write(_unused); } - + /** * We are of type 1017 */ @@ -303,7 +304,7 @@ public class SSSlideInfoAtom extends RecordAtom { public boolean getEffectTransitionFlagByBit(int bitmask) { return ((_effectTransitionFlags & bitmask) != 0); } - + public short getSpeed() { return _speed; } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java b/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java index 4534de034a..cf8d2466c0 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; @@ -56,24 +57,22 @@ public final class Slide extends SheetContainer */ protected Slide(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); // Find the interesting ones in there - for(int i=0; i<_children.length; i++) { - if(_children[i] instanceof SlideAtom) { - slideAtom = (SlideAtom)_children[i]; - } - else if(_children[i] instanceof PPDrawing) { - ppDrawing = (PPDrawing)_children[i]; + for (Record child : _children) { + if (child instanceof SlideAtom) { + slideAtom = (SlideAtom) child; + } else if (child instanceof PPDrawing) { + ppDrawing = (PPDrawing) child; } - if(ppDrawing != null && _children[i] instanceof ColorSchemeAtom) { - _colorScheme = (ColorSchemeAtom)_children[i]; - } + if (ppDrawing != null && child instanceof ColorSchemeAtom) { + _colorScheme = (ColorSchemeAtom) child; + } } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtom.java index e5b85a7e00..5728bacf4c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -83,12 +84,10 @@ public final class SlideAtom extends RecordAtom { if(len < 30) { len = 30; } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the 12 bytes that is "SSlideLayoutAtom" - byte[] SSlideLayoutAtomData = new byte[12]; - System.arraycopy(source,start+8,SSlideLayoutAtomData,0,12); + byte[] SSlideLayoutAtomData = Arrays.copyOfRange(source,start+8, start+12+8); // Use them to build up the SSlideLayoutAtom layoutAtom = new SlideAtomLayout(SSlideLayoutAtomData); @@ -98,26 +97,13 @@ public final class SlideAtom extends RecordAtom { // Grok the flags, stored as bits int flags = LittleEndian.getUShort(source,start+20+8); - if((flags&4) == 4) { - followMasterBackground = true; - } else { - followMasterBackground = false; - } - if((flags&2) == 2) { - followMasterScheme = true; - } else { - followMasterScheme = false; - } - if((flags&1) == 1) { - followMasterObjects = true; - } else { - followMasterObjects = false; - } + followMasterBackground = (flags & 4) == 4; + followMasterScheme = (flags & 2) == 2; + followMasterObjects = (flags & 1) == 1; // If there's any other bits of data, keep them about // 8 bytes header + 20 bytes to flags + 2 bytes flags = 30 bytes - reserved = IOUtils.safelyAllocate(len-30, MAX_RECORD_LENGTH); - System.arraycopy(source,start+30,reserved,0,reserved.length); + reserved = IOUtils.safelyClone(source,start+30, len-30, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtomLayout.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtomLayout.java index 9b27fe2282..e91bc7ddb4 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtomLayout.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlideAtomLayout.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -32,7 +33,7 @@ import org.apache.poi.util.LittleEndian; * Holds the geometry of the Slide, and the ID of the placeholders on the slide. * Embedded inside a SlideAtom is a SlideAtomLayout, without the usual record header. * Since it's a fixed size and tied to the SlideAtom, we'll hold it here.

- * + * * This might eventually merged with the XSLF counterpart */ @Internal @@ -82,7 +83,7 @@ public class SlideAtomLayout implements GenericRecord { SlideLayoutType(int nativeId) { this.nativeId = nativeId; } - + public int getNativeId() { return nativeId; } @@ -117,8 +118,7 @@ public class SlideAtomLayout implements GenericRecord { // Grab out our data geometry = SlideLayoutType.forNativeID(LittleEndian.getInt(data,0)); - placeholderIDs = new byte[8]; - System.arraycopy(data,4,placeholderIDs,0,8); + placeholderIDs = Arrays.copyOfRange(data,4, 4+8); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java index 516bbda7b0..d0298a5be2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java @@ -20,6 +20,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.apache.poi.util.LittleEndian; @@ -74,8 +75,7 @@ public final class SlideListWithText extends RecordContainer { */ protected SlideListWithText(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); @@ -96,8 +96,7 @@ public final class SlideListWithText extends RecordContainer { // Create a SlideAtomsSets, not caring if they're empty //if(emptySet) { continue; } - org.apache.poi.hslf.record.Record[] spaChildren = new org.apache.poi.hslf.record.Record[clen]; - System.arraycopy(_children,i+1,spaChildren,0,clen); + org.apache.poi.hslf.record.Record[] spaChildren = Arrays.copyOfRange(_children,i+1, i+1+clen, org.apache.poi.hslf.record.Record[].class); SlideAtomsSet set = new SlideAtomsSet((SlidePersistAtom)_children[i],spaChildren); sets.add(set); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlidePersistAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlidePersistAtom.java index 34a5560253..d52552ab7b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlidePersistAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlidePersistAtom.java @@ -21,6 +21,7 @@ import static org.apache.poi.util.GenericRecordUtil.getBitsAsString; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -44,7 +45,7 @@ public final class SlidePersistAtom extends RecordAtom { private static final String[] FLAGS_NAMES = { "HAS_SHAPES_OTHER_THAN_PLACEHOLDERS" }; - private final byte[] _header = new byte[8]; + private final byte[] _header; /** Slide reference ID. Should correspond to the PersistPtr "sheet ID" of the matching slide/notes record */ private int refID; @@ -91,7 +92,7 @@ public final class SlidePersistAtom extends RecordAtom { if(len < 8) { len = 8; } // Get the header - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the reference ID refID = LittleEndian.getInt(source,start+8); @@ -107,14 +108,14 @@ public final class SlidePersistAtom extends RecordAtom { // Finally you have typically 4 or 8 bytes of reserved fields, // all zero running from 24 bytes in to the end - reservedFields = IOUtils.safelyAllocate(len-24, MAX_RECORD_LENGTH); - System.arraycopy(source,start+24,reservedFields,0,reservedFields.length); + reservedFields = IOUtils.safelyClone(source,start+24, len-24, MAX_RECORD_LENGTH); } /** * Create a new SlidePersistAtom, for use with a new Slide */ public SlidePersistAtom() { + _header = new byte[8]; LittleEndian.putUShort(_header, 0, 0); LittleEndian.putUShort(_header, 2, (int)_type); LittleEndian.putInt(_header, 4, 20); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Sound.java b/src/scratchpad/src/org/apache/poi/hslf/record/Sound.java index f454e4d208..27a2c1b8cf 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Sound.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Sound.java @@ -17,10 +17,11 @@ package org.apache.poi.hslf.record; -import org.apache.poi.util.POILogger; - -import java.io.OutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; + +import org.apache.poi.util.POILogger; /** * A container holding information about a sound. It contains: @@ -55,8 +56,7 @@ public final class Sound extends RecordContainer { */ protected Sound(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SoundCollection.java b/src/scratchpad/src/org/apache/poi/hslf/record/SoundCollection.java index abea3887f0..3fda587c6e 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SoundCollection.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SoundCollection.java @@ -17,8 +17,9 @@ package org.apache.poi.hslf.record; -import java.io.OutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.util.Arrays; /** * Is a container for all sound related atoms and containers. It contains: @@ -42,8 +43,7 @@ public final class SoundCollection extends RecordContainer { */ protected SoundCollection(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source,start+8,len-8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SoundData.java b/src/scratchpad/src/org/apache/poi/hslf/record/SoundData.java index 09f9d5c1a7..0989052c56 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SoundData.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SoundData.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -68,12 +69,10 @@ public final class SoundData extends RecordAtom { */ protected SoundData(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextProp9Atom.java b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextProp9Atom.java index cedea7f646..20317e3074 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextProp9Atom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextProp9Atom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -45,7 +46,7 @@ public final class StyleTextProp9Atom extends RecordAtom { private short version; private short recordId; private int length; - + /** * Constructs the link related atom record from its * source data. @@ -57,35 +58,33 @@ public final class StyleTextProp9Atom extends RecordAtom { protected StyleTextProp9Atom(byte[] source, int start, int len) { // Get the header. final List schemes = new LinkedList<>(); - header = new byte[8]; - System.arraycopy(source,start, header,0,8); + header = Arrays.copyOfRange(source, start, start+8); this.version = LittleEndian.getShort(header, 0); this.recordId = LittleEndian.getShort(header, 2); this.length = LittleEndian.getInt(header, 4); - + // Get the record data. - data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source, start+8, data, 0, len-8); + data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); for (int i = 0; i < data.length; ) { final TextPFException9 item = new TextPFException9(data, i); schemes.add(item); i += item.getRecordLength(); - + if (i+4 >= data.length) { break; } int textCfException9 = LittleEndian.getInt(data, i ); i += 4; //TODO analyze textCfException when have some test data - + if (i+4 >= data.length) { break; } int textSiException = LittleEndian.getInt(data, i ); i += 4;//TextCFException9 + SIException - - if (0 != (textSiException & 0x40)) { - i += 2; //skip fBidi + + if (0 != (textSiException & 0x40)) { + i += 2; //skip fBidi } if (i+4 >= data.length) { break; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java index 0d2a23ba60..2e0084e323 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/StyleTextPropAtom.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Supplier; @@ -130,13 +131,11 @@ public final class StyleTextPropAtom extends RecordAtom { } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Save the contents of the atom, until we're asked to go and // decode them (via a call to setParentTextSize(int) - rawContents = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,rawContents,0,rawContents.length); + rawContents = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); reserved = new byte[0]; // Set empty lists, ready for when they call setParentTextSize @@ -289,8 +288,7 @@ public final class StyleTextPropAtom extends RecordAtom { // Handle anything left over if(pos < rawContents.length) { - reserved = IOUtils.safelyAllocate(rawContents.length-pos, rawContents.length); - System.arraycopy(rawContents,pos,reserved,0,reserved.length); + reserved = IOUtils.safelyClone(rawContents, pos, rawContents.length-pos, rawContents.length); } initialised = true; diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextBytesAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextBytesAtom.java index cd84b7b8da..81a503b95c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextBytesAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextBytesAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -69,12 +70,10 @@ public final class TextBytesAtom extends RecordAtom { if(len < 8) { len = 8; } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the text - _text = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_text,0,len-8); + _text = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java index 078f1d9ef7..768aaba6ef 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -68,12 +69,10 @@ public final class TextCharsAtom extends RecordAtom { if(len < 8) { len = 8; } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the text - _text = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_text,0,len-8); + _text = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** * Create an empty TextCharsAtom diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextHeaderAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextHeaderAtom.java index 3a2d01adfb..99ac049994 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextHeaderAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextHeaderAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -84,8 +85,7 @@ public final class TextHeaderAtom extends RecordAtom implements ParentAwareRecor } // Get the header - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Grab the type textType = LittleEndian.getInt(source,start+8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoAtom.java index d572290657..b26e9cb04b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoAtom.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Supplier; @@ -43,7 +44,7 @@ public final class TextSpecInfoAtom extends RecordAtom { private static final int MAX_RECORD_LENGTH = 100_000; private static final long _type = RecordTypes.TextSpecInfoAtom.typeID; - + /** * Record header. */ @@ -55,14 +56,14 @@ public final class TextSpecInfoAtom extends RecordAtom { private byte[] _data; /** - * Constructs an empty atom, with a default run of size 1 + * Constructs an empty atom, with a default run of size 1 */ public TextSpecInfoAtom() { _header = new byte[8]; LittleEndian.putUInt(_header, 4, _type); reset(1); } - + /** * Constructs the link related atom record from its * source data. @@ -73,13 +74,10 @@ public final class TextSpecInfoAtom extends RecordAtom { */ public TextSpecInfoAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); - + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** * Gets the record type. @@ -157,7 +155,7 @@ public final class TextSpecInfoAtom extends RecordAtom { // Update the size (header bytes 5-8) LittleEndian.putInt(_header, 4, _data.length); } - + /** * Get the number of characters covered by this records * diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TxInteractiveInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TxInteractiveInfoAtom.java index 469de32a56..f5896aa817 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TxInteractiveInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TxInteractiveInfoAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -67,13 +68,10 @@ public final class TxInteractiveInfoAtom extends RecordAtom { */ protected TxInteractiveInfoAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,len-8); - + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); } /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TxMasterStyleAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TxMasterStyleAtom.java index 2597ccbe67..40a85fab1c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TxMasterStyleAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TxMasterStyleAtom.java @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.function.Supplier; @@ -70,11 +71,9 @@ public final class TxMasterStyleAtom extends RecordAtom { private List charStyles; protected TxMasterStyleAtom(byte[] source, int start, int len) { - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); - _data = IOUtils.safelyAllocate(len-8, MAX_RECORD_LENGTH); - System.arraycopy(source,start+8,_data,0,_data.length); + _data = IOUtils.safelyClone(source, start+8, len-8, MAX_RECORD_LENGTH); //read available styles try { diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/UnknownRecordPlaceholder.java b/src/scratchpad/src/org/apache/poi/hslf/record/UnknownRecordPlaceholder.java index e483ce7424..886a0dd20f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/UnknownRecordPlaceholder.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/UnknownRecordPlaceholder.java @@ -53,8 +53,7 @@ public final class UnknownRecordPlaceholder extends RecordAtom if(len < 0) { len = 0; } // Treat as an atom, grab and hold everything - _contents = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); - System.arraycopy(source,start,_contents,0,len); + _contents = IOUtils.safelyClone(source, start, len, MAX_RECORD_LENGTH); _type = LittleEndian.getUShort(_contents,2); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/UserEditAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/UserEditAtom.java index 287da27798..b89da4a98c 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/UserEditAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/UserEditAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -89,8 +90,7 @@ public final class UserEditAtom extends PositionDependentRecordAtom int offset = start; // Get the header - _header = new byte[8]; - System.arraycopy(source,offset,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); offset += 8; // Get the last viewed slide ID @@ -125,7 +125,7 @@ public final class UserEditAtom extends PositionDependentRecordAtom // Last view type lastViewType = LittleEndian.getShort(source,offset); offset += LittleEndianConsts.SHORT_SIZE; - + // unused unused = LittleEndian.getShort(source,offset); offset += LittleEndianConsts.SHORT_SIZE; @@ -135,7 +135,7 @@ public final class UserEditAtom extends PositionDependentRecordAtom encryptSessionPersistIdRef = LittleEndian.getInt(source,offset); offset += LittleEndianConsts.INT_SIZE; } - + assert(offset-start == len); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoAtom.java index eb4d337383..4607bb6141 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoAtom.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import java.util.Map; import java.util.function.Supplier; @@ -30,7 +31,7 @@ import org.apache.poi.util.LittleEndian; */ public final class VBAInfoAtom extends RecordAtom { private static final long _type = RecordTypes.VBAInfoAtom.typeID; - + /** * Record header. */ @@ -44,7 +45,7 @@ public final class VBAInfoAtom extends RecordAtom { private long version; /** - * Constructs an empty atom - not yet supported + * Constructs an empty atom - not yet supported */ private VBAInfoAtom() { _header = new byte[8]; @@ -54,7 +55,7 @@ public final class VBAInfoAtom extends RecordAtom { hasMacros = true; version = 2; } - + /** * Constructs the vba atom record from its source data. * @@ -64,8 +65,7 @@ public final class VBAInfoAtom extends RecordAtom { */ public VBAInfoAtom(byte[] source, int start, int len) { // Get the header. - _header = new byte[8]; - System.arraycopy(source,start,_header,0,8); + _header = Arrays.copyOfRange(source, start, start+8); // Get the record data. persistIdRef = LittleEndian.getUInt(source, start+8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoContainer.java index cf9f6159cc..043cea2286 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/VBAInfoContainer.java @@ -19,6 +19,7 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; @@ -36,8 +37,7 @@ public final class VBAInfoContainer extends RecordContainer { */ protected VBAInfoContainer(byte[] source, int start, int len) { // Grab the header - _header = new byte[8]; - System.arraycopy(source, start, _header, 0, 8); + _header = Arrays.copyOfRange(source, start, start+8); // Find our children _children = Record.findChildRecords(source, start + 8, len - 8); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureData.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureData.java index e7f92a4a9f..f5b2eaa1ce 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureData.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFPictureData.java @@ -21,6 +21,7 @@ import java.awt.Dimension; import java.io.IOException; import java.io.OutputStream; import java.security.MessageDigest; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -129,9 +130,7 @@ public abstract class HSLFPictureData implements PictureData, GenericRecord { * Returns 16-byte checksum of this picture */ public byte[] getUID(){ - byte[] uid = new byte[16]; - System.arraycopy(rawdata, 0, uid, 0, uid.length); - return uid; + return Arrays.copyOf(rawdata, 16); } @Override diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java index 9ad2c7ccde..6f1c633ea3 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowImpl.java @@ -301,7 +301,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { // check for corrupted user edit atom and try to repair it // if the next user edit atom offset is already known, we would go into an endless loop if (usrOffset > 0 && recordMap.containsKey(usrOffset)) { - // a user edit atom is usually located 36 byte before the smallest known record offset + // a user edit atom is usually located 36 byte before the smallest known record offset usrOffset = recordMap.firstKey() - 36; // check that we really are located on a user edit atom int ver_inst = LittleEndian.getUShort(docstream, usrOffset); @@ -415,8 +415,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { pict.setSignature(signature); // Copy the data, ready to pass to PictureData - byte[] imgdata = IOUtils.safelyAllocate(imgsize, MAX_RECORD_LENGTH); - System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length); + byte[] imgdata = IOUtils.safelyClone(pictstream, pos, imgsize, MAX_RECORD_LENGTH); pict.setRawData(imgdata); pict.setOffset(offset); @@ -563,7 +562,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { validateInPlaceWritePossible(); // Write the PowerPoint streams to the current FileSystem - // No need to do anything to other streams, already there! + // No need to do anything to other streams, already there! write(getDirectory().getFileSystem(), false); // Sync with the File on disk @@ -649,7 +648,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { } private void write(POIFSFileSystem outFS, boolean copyAllOtherNodes) throws IOException { - // read properties and pictures, with old encryption settings where appropriate + // read properties and pictures, with old encryption settings where appropriate if (_pictures == null) { readPictures(); } @@ -673,8 +672,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { updateAndWriteDependantRecords(baos, null); // Update our cached copy of the bytes that make up the PPT stream - _docstream = new byte[baos.size()]; - System.arraycopy(baos.getBuf(), 0, _docstream, 0, baos.size()); + _docstream = baos.toByteArray(); baos.close(); // Write the PPT stream into the POIFS layer @@ -701,7 +699,7 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { } } - + // If requested, copy over any other streams we spot, eg Macros if (copyAllOtherNodes) { EntryUtils.copyNodes(getDirectory().getFileSystem(), outFS, writtenEntries); @@ -715,9 +713,9 @@ public final class HSLFSlideShowImpl extends POIDocument implements Closeable { return (dea != null) ? dea.getEncryptionInfo() : null; } - - - + + + /* ******************* adding methods follow ********************* */ /** diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java index ba9d1495bc..6e1b7611d1 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java @@ -22,6 +22,7 @@ import static org.apache.poi.hslf.record.RecordTypes.OutlineTextRefAtom; import java.awt.Color; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; @@ -333,8 +334,7 @@ public final class HSLFTextParagraph implements TextParagraph * NOTE: This source is automatically generated please do not modify this file. Either subclass or * remove the record in src/types/definitions. *

- * This class is internal. It content or properties may change without notice + * This class is internal. It content or properties may change without notice * due to changes in our knowledge of internal Microsoft Word binary structures. * @author Sergey Vladimirov; according to Microsoft Office Word 97-2007 Binary File Format Specification [*.doc] and [MS-DOC] - v20110608 Word (.doc) Binary File Format - + */ @Internal public abstract class PICFAbstractType @@ -103,10 +104,10 @@ public abstract class PICFAbstractType field_18_dyaReserved2 = LittleEndian.getShort( data, 0x2a + offset ); field_19_fReserved = data[ 0x2c + offset ]; field_20_bpp = data[ 0x2d + offset ]; - field_21_brcTop80 = LittleEndian.getByteArray( data, 0x2e + offset,4 ); - field_22_brcLeft80 = LittleEndian.getByteArray( data, 0x32 + offset,4 ); - field_23_brcBottom80 = LittleEndian.getByteArray( data, 0x36 + offset,4 ); - field_24_brcRight80 = LittleEndian.getByteArray( data, 0x3a + offset,4 ); + field_21_brcTop80 = Arrays.copyOfRange( data, 0x2e + offset, 0x2e + offset + 4 ); + field_22_brcLeft80 = Arrays.copyOfRange( data, 0x32 + offset, 0x32 + offset + 4 ); + field_23_brcBottom80 = Arrays.copyOfRange( data, 0x36 + offset, 0x36 + offset + 4 ); + field_24_brcRight80 = Arrays.copyOfRange( data, 0x3a + offset, 0x3a + offset + 4 ); field_25_dxaReserved3 = LittleEndian.getShort( data, 0x3e + offset ); field_26_dyaReserved3 = LittleEndian.getShort( data, 0x40 + offset ); field_27_cProps = LittleEndian.getShort( data, 0x42 + offset ); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/ParagraphSprmUncompressor.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/ParagraphSprmUncompressor.java index ddd7b52aa2..c89661cd99 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/ParagraphSprmUncompressor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/ParagraphSprmUncompressor.java @@ -18,6 +18,7 @@ package org.apache.poi.hwpf.sprm; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -314,11 +315,9 @@ public final class ParagraphSprmUncompressor case 0x3b: //obsolete break; - case 0x3e: - { - byte[] buf = new byte[sprm.size() - 3]; - System.arraycopy(buf, 0, sprm.getGrpprl(), sprm.getGrpprlOffset(), - buf.length); + case 0x3e: { + // TODO: REMOVEME + byte[] buf = Arrays.copyOfRange(sprm.getGrpprl(), sprm.getGrpprlOffset(), sprm.getGrpprlOffset() + (sprm.size() - 3)); newPAP.setAnld(buf); break; } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SectionSprmUncompressor.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SectionSprmUncompressor.java index 7f64b01047..11072bc334 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SectionSprmUncompressor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SectionSprmUncompressor.java @@ -17,6 +17,8 @@ package org.apache.poi.hwpf.sprm; +import java.util.Arrays; + import org.apache.poi.hwpf.usermodel.BorderCode; import org.apache.poi.hwpf.usermodel.SectionProperties; import org.apache.poi.util.HexDump; @@ -69,8 +71,7 @@ public final class SectionSprmUncompressor extends SprmUncompressor newSEP.setIHeadingPgn ((byte) sprm.getOperand()); break; case 0x2: - byte[] buf = new byte[sprm.size() - 3]; - System.arraycopy(sprm.getGrpprl(), sprm.getGrpprlOffset(), buf, 0, buf.length); + byte[] buf = Arrays.copyOfRange(sprm.getGrpprl(), sprm.getGrpprlOffset(), sprm.getGrpprlOffset() + (sprm.size() - 3)); newSEP.setOlstAnm (buf); break; case 0x3: @@ -216,11 +217,11 @@ public final class SectionSprmUncompressor extends SprmUncompressor newSEP.setWTextFlow ((short) sprm.getOperand()); break; case 0x3C: - // [MS-DOC], v20140721, 2.6.4, sprmSRncFtn + // [MS-DOC], v20140721, 2.6.4, sprmSRncFtn newSEP.setRncFtn((short) sprm.getOperand()); break; case 0x3E: - // [MS-DOC], v20140721, 2.6.4, sprmSRncEdn + // [MS-DOC], v20140721, 2.6.4, sprmSRncEdn newSEP.setRncEdn((short) sprm.getOperand()); break; case 0x3F: diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java index 1bcee335e8..af937e6465 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmBuffer.java @@ -127,9 +127,8 @@ public final class SprmBuffer implements Duplicatable { // commented - buffer shall not contain any additional bytes -- // sergey // byte[] newBuf = new byte[_offset + addition + 6]; - byte[] newBuf = IOUtils.safelyAllocate(_offset + addition, MAX_RECORD_LENGTH); - System.arraycopy(_buf, 0, newBuf, 0, _buf.length); - _buf = newBuf; + IOUtils.safelyAllocateCheck(_offset + addition, MAX_RECORD_LENGTH); + _buf = Arrays.copyOf(_buf, _offset + addition); } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmOperation.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmOperation.java index 287063c22d..3fc26d0712 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmOperation.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmOperation.java @@ -17,6 +17,8 @@ package org.apache.poi.hwpf.sprm; +import java.util.Arrays; + import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.Internal; @@ -75,9 +77,7 @@ public final class SprmOperation public byte[] toByteArray() { - byte[] result = new byte[size()]; - System.arraycopy( _grpprl, _offset, result, 0, size() ); - return result; + return Arrays.copyOfRange(_grpprl, _offset, _offset + size()); } public byte[] getGrpprl() diff --git a/src/scratchpad/testcases/org/apache/poi/hmef/TestCompressedRTF.java b/src/scratchpad/testcases/org/apache/poi/hmef/TestCompressedRTF.java index 04fa813732..3b5b1ec0b6 100644 --- a/src/scratchpad/testcases/org/apache/poi/hmef/TestCompressedRTF.java +++ b/src/scratchpad/testcases/org/apache/poi/hmef/TestCompressedRTF.java @@ -119,8 +119,7 @@ public final class TestCompressedRTF { MAPIRtfAttribute rtfAttr = (MAPIRtfAttribute) attr; // Truncate to header + flag + data for flag - byte[] data = new byte[16 + 12]; - System.arraycopy(rtfAttr.getRawData(), 0, data, 0, data.length); + byte[] data = Arrays.copyOf(rtfAttr.getRawData(), 16 + 12); // Decompress it CompressedRTF comp = new CompressedRTF(); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java index 40e20429ed..299a71e7fc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestPictures.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.URL; +import java.util.Arrays; import java.util.List; import org.apache.poi.POIDataSamples; @@ -49,7 +50,7 @@ import org.junit.Test; * @author Yegor Kozlov */ public final class TestPictures { - private static POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); + private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); /** * Test read/write Macintosh PICT @@ -66,11 +67,11 @@ public final class TestPictures { Dimension nDim = nHeader.getSize(); assertEquals(expWidth, nDim.getWidth(), 0); assertEquals(expHeight, nDim.getHeight(), 0); - + Dimension dim = data.getImageDimensionInPixels(); assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0); assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0); - + HSLFPictureShape pict = new HSLFPictureShape(data); assertEquals(data.getIndex(), pict.getPictureIndex()); slide.addShape(pict); @@ -91,12 +92,12 @@ public final class TestPictures { //check picture data List pictures = ppt.getPictureData(); assertEquals(1, pictures.size()); - + HSLFPictureData pd = pictures.get(0); dim = pd.getImageDimension(); assertEquals(expWidth, dim.width); assertEquals(expHeight, dim.height); - + //the Picture shape refers to the PictureData object in the Presentation assertEquals(pict.getPictureData(), pd); @@ -107,10 +108,8 @@ public final class TestPictures { byte[] ppt_bytes = pd.getData(); assertEquals(src_bytes.length, ppt_bytes.length); //in PICT the first 512 bytes are MAC specific and may not be preserved, ignore them - byte[] b1 = new byte[src_bytes.length-512]; - System.arraycopy(src_bytes, 512, b1, 0, b1.length); - byte[] b2 = new byte[ppt_bytes.length-512]; - System.arraycopy(ppt_bytes, 512, b2, 0, b2.length); + byte[] b1 = Arrays.copyOfRange(src_bytes, 512, src_bytes.length); + byte[] b2 = Arrays.copyOfRange(ppt_bytes, 512, ppt_bytes.length); assertArrayEquals(b1, b2); } @@ -133,7 +132,7 @@ public final class TestPictures { Dimension dim = data.getImageDimensionInPixels(); assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0); assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0); - + HSLFPictureShape pict = new HSLFPictureShape(data); assertEquals(data.getIndex(), pict.getPictureIndex()); slide.addShape(pict); @@ -159,20 +158,18 @@ public final class TestPictures { dim = pd.getImageDimension(); assertEquals(expWidth, dim.width); assertEquals(expHeight, dim.height); - + //the Picture shape refers to the PictureData object in the Presentation assertEquals(pict.getPictureData(), pd); - + assertEquals(PictureType.WMF, pd.getType()); assertTrue(pd instanceof WMF); //compare the content of the initial file with what is stored in the PictureData byte[] ppt_bytes = pd.getData(); assertEquals(src_bytes.length, ppt_bytes.length); //in WMF the first 22 bytes - is a metafile header - byte[] b1 = new byte[src_bytes.length-22]; - System.arraycopy(src_bytes, 22, b1, 0, b1.length); - byte[] b2 = new byte[ppt_bytes.length-22]; - System.arraycopy(ppt_bytes, 22, b2, 0, b2.length); + byte[] b1 = Arrays.copyOfRange(src_bytes, 22, src_bytes.length); + byte[] b2 = Arrays.copyOfRange(ppt_bytes, 22, ppt_bytes.length); assertArrayEquals(b1, b2); } @@ -195,7 +192,7 @@ public final class TestPictures { Dimension dim = data.getImageDimensionInPixels(); assertEquals(Units.pointsToPixel(expWidth), dim.getWidth(), 0); assertEquals(Units.pointsToPixel(expHeight), dim.getHeight(), 0); - + HSLFPictureShape pict = new HSLFPictureShape(data); assertEquals(data.getIndex(), pict.getPictureIndex()); slide.addShape(pict); @@ -216,12 +213,12 @@ public final class TestPictures { //check picture data List pictures = ppt.getPictureData(); assertEquals(1, pictures.size()); - + HSLFPictureData pd = pictures.get(0); dim = pd.getImageDimension(); assertEquals(expWidth, dim.width); assertEquals(expHeight, dim.height); - + //the Picture shape refers to the PictureData object in the Presentation assertEquals(pict.getPictureData(), pd); @@ -393,10 +390,8 @@ public final class TestPictures { ppt_bytes = slTests.readFile("santa.wmf"); assertEquals(src_bytes.length, ppt_bytes.length); //ignore the first 22 bytes - it is a WMF metafile header - b1 = new byte[src_bytes.length-22]; - System.arraycopy(src_bytes, 22, b1, 0, b1.length); - b2 = new byte[ppt_bytes.length-22]; - System.arraycopy(ppt_bytes, 22, b2, 0, b2.length); + b1 = Arrays.copyOfRange(src_bytes, 22, src_bytes.length); + b2 = Arrays.copyOfRange(ppt_bytes, 22, ppt_bytes.length); assertArrayEquals(b1, b2); pict = (HSLFPictureShape)slides.get(3).getShapes().get(0); //the forth slide contains PICT @@ -407,10 +402,8 @@ public final class TestPictures { ppt_bytes = slTests.readFile("cow.pict"); assertEquals(src_bytes.length, ppt_bytes.length); //ignore the first 512 bytes - it is a MAC specific crap - b1 = new byte[src_bytes.length-512]; - System.arraycopy(src_bytes, 512, b1, 0, b1.length); - b2 = new byte[ppt_bytes.length-512]; - System.arraycopy(ppt_bytes, 512, b2, 0, b2.length); + b1 = Arrays.copyOfRange(src_bytes, 512, src_bytes.length); + b2 = Arrays.copyOfRange(ppt_bytes, 512, ppt_bytes.length); assertArrayEquals(b1, b2); pict = (HSLFPictureShape)slides.get(4).getShapes().get(0); //the fifth slide contains EMF @@ -457,7 +450,7 @@ public final class TestPictures { pdata = pict.getPictureData(); assertTrue(pdata instanceof WMF); assertEquals(PictureType.WMF, pdata.getType()); - + ppt.close(); } 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 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()); }