diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2020-12-26 00:08:19 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2020-12-26 00:08:19 +0000 |
commit | d121353f612c6efbcf156e166158f1d403e8697e (patch) | |
tree | 078baea3d2ad8cb33ae4752e677aa8887372389f /src/java/org/apache/poi | |
parent | be1afe5ff586cfc01c68ca6dc58e84f951aca2ef (diff) | |
download | poi-d121353f612c6efbcf156e166158f1d403e8697e.tar.gz poi-d121353f612c6efbcf156e166158f1d403e8697e.zip |
Sonar fixes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884807 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
26 files changed, 123 insertions, 110 deletions
diff --git a/src/java/org/apache/poi/extractor/ExtractorFactory.java b/src/java/org/apache/poi/extractor/ExtractorFactory.java index 0833535189..b45b5e29e2 100644 --- a/src/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/java/org/apache/poi/extractor/ExtractorFactory.java @@ -178,6 +178,7 @@ public final class ExtractorFactory { return createExtractor(file, getCurrentUserPassword()); } + @SuppressWarnings({"java:S2095"}) public static POITextExtractor createExtractor(File file, String password) throws IOException { if (file.length() == 0) { throw new EmptyFileException(); @@ -318,6 +319,6 @@ public final class ExtractorFactory { } public static void removeProvider(Class<? extends ExtractorProvider> provider){ - Singleton.INSTANCE.provider.removeIf(p -> p.getClass().getName().equals(provider.getName())); + Singleton.INSTANCE.provider.removeIf(p -> p.getClass().isAssignableFrom(provider)); } } diff --git a/src/java/org/apache/poi/extractor/MainExtractorFactory.java b/src/java/org/apache/poi/extractor/MainExtractorFactory.java index 7f8733eecf..d485f864ff 100644 --- a/src/java/org/apache/poi/extractor/MainExtractorFactory.java +++ b/src/java/org/apache/poi/extractor/MainExtractorFactory.java @@ -41,6 +41,7 @@ public class MainExtractorFactory implements ExtractorProvider { return FileMagic.OLE2 == fm; } + @SuppressWarnings({"java:S2095"}) @Override public POITextExtractor create(File file, String password) throws IOException { return create(new POIFSFileSystem(file, true).getRoot(), password); diff --git a/src/java/org/apache/poi/hssf/dev/ReSave.java b/src/java/org/apache/poi/hssf/dev/ReSave.java index 7f19888973..2f31eb48d8 100644 --- a/src/java/org/apache/poi/hssf/dev/ReSave.java +++ b/src/java/org/apache/poi/hssf/dev/ReSave.java @@ -37,7 +37,6 @@ public class ReSave { public static void main(String[] args) throws Exception { boolean initDrawing = false; boolean saveToMemory = false; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); for(String filename : args) { if(filename.equals("-dg")) { initDrawing = true; @@ -45,11 +44,10 @@ public class ReSave { saveToMemory = true; } else { System.out.print("reading " + filename + "..."); - FileInputStream is = new FileInputStream(filename); - HSSFWorkbook wb = new HSSFWorkbook(is); - try { + try (FileInputStream is = new FileInputStream(filename); + HSSFWorkbook wb = new HSSFWorkbook(is)) { System.out.println("done"); - + for(int i = 0; i < wb.getNumberOfSheets(); i++){ HSSFSheet sheet = wb.getSheetAt(i); if(initDrawing) { @@ -57,25 +55,15 @@ public class ReSave { } } - OutputStream os; - if (saveToMemory) { - bos.reset(); - os = bos; - } else { - String outputFile = filename.replace(".xls", "-saved.xls"); + String outputFile = filename.replace(".xls", "-saved.xls"); + if (!saveToMemory) { System.out.print("saving to " + outputFile + "..."); - os = new FileOutputStream(outputFile); } - - try { + + try (OutputStream os = saveToMemory ? new ByteArrayOutputStream() : new FileOutputStream(outputFile)) { wb.write(os); - } finally { - os.close(); } System.out.println("done"); - } finally { - wb.close(); - is.close(); } } } diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 8ddb368e0a..e1d4f85718 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -1052,7 +1052,7 @@ public final class InternalWorkbook { // Can be a few short if new sheets were added if(records.getTabpos() > 0) { TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos()); - if(tir._tabids.length < boundsheets.size()) { + if(tir.getTabIdSize() < boundsheets.size()) { fixTabIdRecord(); } } diff --git a/src/java/org/apache/poi/hssf/record/GridsetRecord.java b/src/java/org/apache/poi/hssf/record/GridsetRecord.java index d5f705071f..b5cfd92055 100644 --- a/src/java/org/apache/poi/hssf/record/GridsetRecord.java +++ b/src/java/org/apache/poi/hssf/record/GridsetRecord.java @@ -30,7 +30,7 @@ import org.apache.poi.util.LittleEndianOutput; */ public final class GridsetRecord extends StandardRecord { public static final short sid = 0x82; - public short field_1_gridset_flag; + private short field_1_gridset_flag; public GridsetRecord() {} diff --git a/src/java/org/apache/poi/hssf/record/PageBreakRecord.java b/src/java/org/apache/poi/hssf/record/PageBreakRecord.java index 99369d9ae3..402c04d02c 100644 --- a/src/java/org/apache/poi/hssf/record/PageBreakRecord.java +++ b/src/java/org/apache/poi/hssf/record/PageBreakRecord.java @@ -53,9 +53,9 @@ public abstract class PageBreakRecord extends StandardRecord { public static final class Break implements GenericRecord { public static final int ENCODED_SIZE = 6; - public int main; - public int subFrom; - public int subTo; + private int main; + private int subFrom; + private int subTo; public Break(Break other) { main = other.main; @@ -75,6 +75,18 @@ public abstract class PageBreakRecord extends StandardRecord { subTo = in.readUShort(); } + public int getMain() { + return main; + } + + public int getSubFrom() { + return subFrom; + } + + public int getSubTo() { + return subTo; + } + public void serialize(LittleEndianOutput out) { out.writeShort(main + 1); out.writeShort(subFrom); diff --git a/src/java/org/apache/poi/hssf/record/PrecisionRecord.java b/src/java/org/apache/poi/hssf/record/PrecisionRecord.java index 49cbf31de5..27471b935f 100644 --- a/src/java/org/apache/poi/hssf/record/PrecisionRecord.java +++ b/src/java/org/apache/poi/hssf/record/PrecisionRecord.java @@ -32,7 +32,7 @@ import org.apache.poi.util.LittleEndianOutput; public final class PrecisionRecord extends StandardRecord { public static final short sid = 0xE; - public short field_1_precision; + private short field_1_precision; public PrecisionRecord() {} diff --git a/src/java/org/apache/poi/hssf/record/TabIdRecord.java b/src/java/org/apache/poi/hssf/record/TabIdRecord.java index 2fc905bb9e..c76a396bd7 100644 --- a/src/java/org/apache/poi/hssf/record/TabIdRecord.java +++ b/src/java/org/apache/poi/hssf/record/TabIdRecord.java @@ -30,7 +30,7 @@ public final class TabIdRecord extends StandardRecord { public static final short sid = 0x013D; private static final short[] EMPTY_SHORT_ARRAY = { }; - public short[] _tabids; + private short[] _tabids; public TabIdRecord() { _tabids = EMPTY_SHORT_ARRAY; @@ -63,6 +63,14 @@ public final class TabIdRecord extends StandardRecord { } } + public int getTabIdSize() { + return _tabids.length; + } + + public short getTabIdAt(int index) { + return _tabids[index]; + } + protected int getDataSize() { return _tabids.length * 2; } diff --git a/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java b/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java index 78b93a0e41..c8d931f349 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/PageSettingsBlock.java @@ -121,7 +121,7 @@ public final class PageSettingsBlock extends RecordAggregate { /** * @param sid the record sid - * + * * @return <code>true</code> if the specified Record sid is one belonging to the * 'Page Settings Block'. */ @@ -486,7 +486,7 @@ public final class PageSettingsBlock extends RecordAggregate { while(iterator.hasNext()) { PageBreakRecord.Break breakItem = iterator.next(); - int breakLocation = breakItem.main; + int breakLocation = breakItem.getMain(); boolean inStart = (breakLocation >= start); boolean inEnd = (breakLocation <= stop); if(inStart && inEnd) { @@ -497,8 +497,8 @@ public final class PageSettingsBlock extends RecordAggregate { iterator = shiftedBreak.iterator(); while (iterator.hasNext()) { PageBreakRecord.Break breakItem = iterator.next(); - breaks.removeBreak(breakItem.main); - breaks.addBreak((short)(breakItem.main+count), breakItem.subFrom, breakItem.subTo); + breaks.removeBreak(breakItem.getMain()); + breaks.addBreak((short)(breakItem.getMain()+count), breakItem.getSubFrom(), breakItem.getSubTo()); } } @@ -526,9 +526,9 @@ public final class PageSettingsBlock extends RecordAggregate { /** * Queries if the specified row has a page break - * + * * @param row the row to check for - * + * * @return true if the specified row has a page break */ public boolean isRowBroken(int row) { @@ -538,7 +538,7 @@ public final class PageSettingsBlock extends RecordAggregate { /** * Queries if the specified column has a page break - * + * * @param column the column to check for * * @return <code>true</code> if the specified column has a page break @@ -606,7 +606,7 @@ public final class PageSettingsBlock extends RecordAggregate { /** * HEADERFOOTER is new in 2007. Some apps seem to have scattered this record long after * the {@link PageSettingsBlock} where it belongs. - * + * * @param rec the HeaderFooterRecord to set */ public void addLateHeaderFooter(HeaderFooterRecord rec) { @@ -623,10 +623,10 @@ public final class PageSettingsBlock extends RecordAggregate { * This method reads PageSettingsBlock records from the supplied RecordStream until the first * non-PageSettingsBlock record is encountered. As each record is read, it is incorporated * into this PageSettingsBlock.<p> - * + * * The latest Excel version seems to write the PageSettingsBlock uninterrupted. However there * are several examples (that Excel reads OK) where these records are not written together: - * + * * <ul> * <li><b>HEADER_FOOTER(0x089C) after WINDOW2</b> - This record is new in 2007. Some apps * seem to have scattered this record long after the PageSettingsBlock where it belongs @@ -635,7 +635,7 @@ public final class PageSettingsBlock extends RecordAggregate { * This happens in the test sample file "NoGutsRecords.xls" and "WORKBOOK_in_capitals.xls"</li> * <li><b>Margins after DIMENSION</b> - All of PSB should be before DIMENSION. (Bug-47199)</li> * </ul> - * + * * These were probably written by other applications (or earlier versions of Excel). It was * decided to not write specific code for detecting each of these cases. POI now tolerates * PageSettingsBlock records scattered all over the sheet record stream, and in any order, but @@ -646,7 +646,7 @@ public final class PageSettingsBlock extends RecordAggregate { * were when they were originally read. * * @param rs the RecordStream to read from - * + * * @throws org.apache.poi.util.RecordFormatException if any PSB record encountered has the same type (sid) as * a record that is already part of this PageSettingsBlock */ diff --git a/src/java/org/apache/poi/hssf/record/common/PhRun.java b/src/java/org/apache/poi/hssf/record/common/PhRun.java index 2f5a9229da..404efb0bd0 100644 --- a/src/java/org/apache/poi/hssf/record/common/PhRun.java +++ b/src/java/org/apache/poi/hssf/record/common/PhRun.java @@ -59,4 +59,14 @@ public class PhRun { public int hashCode() { return Objects.hash(phoneticTextFirstCharacterOffset, realTextFirstCharacterOffset, realTextLength); } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PhRun phRun = (PhRun) o; + return phoneticTextFirstCharacterOffset == phRun.phoneticTextFirstCharacterOffset + && realTextFirstCharacterOffset == phRun.realTextFirstCharacterOffset + && realTextLength == phRun.realTextLength; + } } diff --git a/src/java/org/apache/poi/poifs/common/POIFSConstants.java b/src/java/org/apache/poi/poifs/common/POIFSConstants.java index 7cacb68fe8..f3804a90be 100644 --- a/src/java/org/apache/poi/poifs/common/POIFSConstants.java +++ b/src/java/org/apache/poi/poifs/common/POIFSConstants.java @@ -57,11 +57,4 @@ public interface POIFSConstants int END_OF_CHAIN = -2; /** Indicates the sector is not used (0xFFFFFFFF) */ int UNUSED_BLOCK = -1; - - /** The first 4 bytes of an OOXML file, used in detection */ - byte[] OOXML_FILE_HEADER = - new byte[] { 0x50, 0x4b, 0x03, 0x04 }; - /** The first 5 bytes of a raw XML file, used in detection */ - byte[] RAW_XML_FILE_HEADER = - new byte[] { 0x3c, 0x3f, 0x78, 0x6d, 0x6c }; -} // end public interface POIFSConstants; +} diff --git a/src/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java b/src/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java index f8efe0d201..898670eaba 100644 --- a/src/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/agile/AgileDecryptor.java @@ -205,6 +205,7 @@ public class AgileDecryptor extends Decryptor { } } + @SuppressWarnings({"java:S2095"}) @Override public InputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); 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 3aec22e55f..ff14f19ab3 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java @@ -140,7 +140,7 @@ public class BinaryRC4Decryptor extends Decryptor { } @Override - @SuppressWarnings("resource") + @SuppressWarnings({"java:S2095","resource"}) public ChunkedCipherInputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); diff --git a/src/java/org/apache/poi/poifs/filesystem/FileMagic.java b/src/java/org/apache/poi/poifs/filesystem/FileMagic.java index 501db481a6..60667f88a5 100644 --- a/src/java/org/apache/poi/poifs/filesystem/FileMagic.java +++ b/src/java/org/apache/poi/poifs/filesystem/FileMagic.java @@ -17,9 +17,6 @@ package org.apache.poi.poifs.filesystem; -import static org.apache.poi.poifs.common.POIFSConstants.OOXML_FILE_HEADER; -import static org.apache.poi.poifs.common.POIFSConstants.RAW_XML_FILE_HEADER; - import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; @@ -39,24 +36,24 @@ import org.apache.poi.util.LocaleUtil; public enum FileMagic { /** OLE2 / BIFF8+ stream used for Office 97 and higher documents */ OLE2(HeaderBlockConstants._signature), - /** OOXML / ZIP stream */ - OOXML(OOXML_FILE_HEADER), - /** XML file */ - XML(RAW_XML_FILE_HEADER), + /** OOXML / ZIP stream - The first 4 bytes of an OOXML file, used in detection */ + OOXML(0x50, 0x4b, 0x03, 0x04), + /** XML file - The first 5 bytes of a raw XML file, used in detection */ + XML(0x3c, 0x3f, 0x78, 0x6d, 0x6c), /** BIFF2 raw stream - for Excel 2 */ - BIFF2(new byte[]{ + BIFF2( 0x09, 0x00, // sid=0x0009 0x04, 0x00, // size=0x0004 0x00, 0x00, // unused '?', 0x00 // '?' = multiple values - }), + ), /** BIFF3 raw stream - for Excel 3 */ - BIFF3(new byte[]{ + BIFF3( 0x09, 0x02, // sid=0x0209 0x06, 0x00, // size=0x0006 0x00, 0x00, // unused '?', 0x00 // '?' = multiple values - }), + ), /** BIFF4 raw stream - for Excel 4 */ BIFF4(new byte[]{ 0x09, 0x04, // sid=0x0409 @@ -81,7 +78,7 @@ public enum FileMagic { HTML("<!DOCTYP", "<html","\n\r<html","\r\n<html","\r<html","\n<html", "<HTML","\r\n<HTML","\n\r<HTML","\r<HTML","\n<HTML"), - WORD2(new byte[]{ (byte)0xdb, (byte)0xa5, 0x2d, 0x00}), + WORD2(0xdb, 0xa5, 0x2d, 0x00), /** JPEG image */ JPEG( new byte[]{ (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xDB }, @@ -91,20 +88,18 @@ public enum FileMagic { /** GIF image */ GIF("GIF87a","GIF89a"), /** PNG Image */ - PNG(new byte[]{ (byte)0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A }), + PNG(0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A), /** TIFF Image */ TIFF("II*\u0000", "MM\u0000*" ), /** WMF image with a placeable header */ - WMF(new byte[]{ (byte)0xD7, (byte)0xCD, (byte)0xC6, (byte)0x9A }), + WMF(0xD7, 0xCD, 0xC6, 0x9A), /** EMF image */ - EMF(new byte[]{ - 1, 0, 0, 0, + EMF(1, 0, 0, 0, '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', - ' ', 'E', 'M', 'F' - }), + ' ', 'E', 'M', 'F'), /** BMP image */ - BMP(new byte[]{'B','M'}), + BMP('B','M'), // keep UNKNOWN always as last enum! /** UNKNOWN magic */ UNKNOWN(new byte[0]); @@ -119,6 +114,14 @@ public enum FileMagic { LittleEndian.putLong(this.magic[0], 0, magic); } + FileMagic(int... magic) { + byte[] one = new byte[magic.length]; + for (int i=0; i<magic.length; i++) { + one[i] = (byte)(magic[i] & 0xFF); + } + this.magic = new byte[][]{ one }; + } + FileMagic(byte[]... magic) { this.magic = magic; } diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index 22577980a3..20fc398e27 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -204,6 +204,7 @@ public class POIFSFileSystem extends BlockStore this(channel, null, readOnly, false); } + @SuppressWarnings("java:S2095") private POIFSFileSystem(FileChannel channel, File srcFile, boolean readOnly, boolean closeChannelOnError) throws IOException { this(false); diff --git a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java index b3d70fff26..e047b3468d 100644 --- a/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java +++ b/src/java/org/apache/poi/sl/draw/DrawTextParagraph.java @@ -177,7 +177,7 @@ public class DrawTextParagraph implements Drawable { penY -= line.getLayout().getAscent(); } - penX = x + (isHSLF ? leftMargin : leftMargin); + penX = x + leftMargin; if (lastLine == null) { if (!isEmptyParagraph()) { // TODO: find out character style for empty, but bulleted/numbered lines diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java index a69efaf8f8..cb0607de61 100644 --- a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java +++ b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java @@ -298,7 +298,7 @@ public final class SlideShowFactory { Singleton.INSTANCE.provider.add(provider); } - public static void removeProvider(Class<? extends SlideShowProvider> provider){ - Singleton.INSTANCE.provider.removeIf(p -> p.getClass().getName().equals(provider.getName())); + public static void removeProvider(Class<? extends SlideShowProvider<?,?>> provider){ + Singleton.INSTANCE.provider.removeIf(p -> p.getClass().isAssignableFrom(provider)); } } diff --git a/src/java/org/apache/poi/ss/formula/SheetIdentifier.java b/src/java/org/apache/poi/ss/formula/SheetIdentifier.java index 51c0852b30..69f6f99501 100644 --- a/src/java/org/apache/poi/ss/formula/SheetIdentifier.java +++ b/src/java/org/apache/poi/ss/formula/SheetIdentifier.java @@ -18,8 +18,8 @@ package org.apache.poi.ss.formula; public class SheetIdentifier { - public String _bookName; - public NameIdentifier _sheetIdentifier; + private final String _bookName; + private final NameIdentifier _sheetIdentifier; public SheetIdentifier(String bookName, NameIdentifier sheetIdentifier) { _bookName = bookName; diff --git a/src/java/org/apache/poi/ss/formula/SheetRangeIdentifier.java b/src/java/org/apache/poi/ss/formula/SheetRangeIdentifier.java index 8cb5b3201a..88c91991b9 100644 --- a/src/java/org/apache/poi/ss/formula/SheetRangeIdentifier.java +++ b/src/java/org/apache/poi/ss/formula/SheetRangeIdentifier.java @@ -18,7 +18,7 @@ package org.apache.poi.ss.formula; public class SheetRangeIdentifier extends SheetIdentifier { - public NameIdentifier _lastSheetIdentifier; + private final NameIdentifier _lastSheetIdentifier; public SheetRangeIdentifier(String bookName, NameIdentifier firstSheetIdentifier, NameIdentifier lastSheetIdentifier) { super(bookName, firstSheetIdentifier); diff --git a/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java b/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java index 77ed481481..aba00e7f6b 100644 --- a/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java +++ b/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java @@ -324,7 +324,7 @@ final class YearFracCalculator { /** day of month */ public final int day; /** milliseconds since 1970 */ - public long tsMilliseconds; + public final long tsMilliseconds; public SimpleDate(Calendar cal) { year = cal.get(Calendar.YEAR); diff --git a/src/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java b/src/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java index aee79ab274..513daa3a1b 100644 --- a/src/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java +++ b/src/java/org/apache/poi/ss/formula/functions/LinearRegressionFunction.java @@ -107,7 +107,7 @@ public final class LinearRegressionFunction extends Fixed2ArgFunction { } public enum FUNCTION {INTERCEPT, SLOPE} - public FUNCTION function; + private final FUNCTION function; public LinearRegressionFunction(FUNCTION function) { this.function = function; diff --git a/src/java/org/apache/poi/ss/formula/functions/Trend.java b/src/java/org/apache/poi/ss/formula/functions/Trend.java index 2a1227e2db..62eb545670 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Trend.java +++ b/src/java/org/apache/poi/ss/formula/functions/Trend.java @@ -60,11 +60,10 @@ import org.apache.poi.ss.formula.eval.ValueEval; */ public final class Trend implements Function { - MatrixFunction.MutableValueCollector collector = new MatrixFunction.MutableValueCollector(false, false); private static final class TrendResults { - public double[] vals; - public int resultWidth; - public int resultHeight; + private final double[] vals; + private final int resultWidth; + private final int resultHeight; public TrendResults(double[] vals, int resultWidth, int resultHeight) { this.vals = vals; @@ -149,7 +148,7 @@ public final class Trend implements Function { } double[] oneD = new double[twoD.length * twoD[0].length]; for (int i = 0; i < twoD.length; i++) { - System.arraycopy(twoD[i], 0, oneD, i * twoD[0].length + 0, twoD[0].length); + System.arraycopy(twoD[i], 0, oneD, i * twoD[0].length, twoD[0].length); } return oneD; } @@ -335,7 +334,7 @@ public final class Trend implements Function { if(isAllColumnsSame(x)){ double[] result = new double[newX.length]; double avg = Arrays.stream(y).average().orElse(0); - for(int i = 0; i < result.length; i++) result[i] = avg; + Arrays.fill(result, avg); return new TrendResults(result, resultWidth, resultHeight); } diff --git a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index b730731f7c..c2b6e6936f 100644 --- a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -305,6 +305,6 @@ public final class WorkbookFactory { } public static void removeProvider(Class<? extends WorkbookProvider> provider){ - Singleton.INSTANCE.provider.removeIf(p -> p.getClass().getName().equals(provider.getName())); + Singleton.INSTANCE.provider.removeIf(p -> p.getClass().isAssignableFrom(provider)); } } diff --git a/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java b/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java index 2826cc3323..d26d7a9864 100644 --- a/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java +++ b/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java @@ -17,6 +17,10 @@ package org.apache.poi.ss.util.cellwalk; +import static org.apache.commons.math3.util.ArithmeticUtils.addAndCheck; +import static org.apache.commons.math3.util.ArithmeticUtils.mulAndCheck; +import static org.apache.commons.math3.util.ArithmeticUtils.subAndCheck; + import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; @@ -26,13 +30,11 @@ import org.apache.poi.ss.util.CellRangeAddress; /** * Traverse cell range. - * - * @author Roman Kashitsyn */ public class CellWalk { - private Sheet sheet; - private CellRangeAddress range; + private final Sheet sheet; + private final CellRangeAddress range; private boolean traverseEmptyCells; @@ -74,8 +76,8 @@ public class CellWalk { int lastColumn = range.getLastColumn(); final int width = lastColumn - firstColumn + 1; SimpleCellWalkContext ctx = new SimpleCellWalkContext(); - Row currentRow = null; - Cell currentCell = null; + Row currentRow; + Cell currentCell; for (ctx.rowNumber = firstRow; ctx.rowNumber <= lastRow; ++ctx.rowNumber) { currentRow = sheet.getRow(ctx.rowNumber); @@ -92,10 +94,9 @@ public class CellWalk { continue; } - long rowSize = ArithmeticUtils.mulAndCheck( - (long)ArithmeticUtils.subAndCheck(ctx.rowNumber, firstRow), (long)width); + long rowSize = mulAndCheck(subAndCheck(ctx.rowNumber, firstRow), (long)width); - ctx.ordinalNumber = ArithmeticUtils.addAndCheck(rowSize, (ctx.colNumber - firstColumn + 1)); + ctx.ordinalNumber = addAndCheck(rowSize, (ctx.colNumber - firstColumn + 1)); handler.onCell(currentCell, ctx); } @@ -108,13 +109,11 @@ public class CellWalk { /** * Inner class to hold walk context. - * - * @author Roman Kashitsyn */ private static class SimpleCellWalkContext implements CellWalkContext { - public long ordinalNumber; - public int rowNumber; - public int colNumber; + private long ordinalNumber; + private int rowNumber; + private int colNumber; @Override public long getOrdinalNumber() { diff --git a/src/java/org/apache/poi/util/DrawingDump.java b/src/java/org/apache/poi/util/DrawingDump.java index ef7b2d29ac..72d9580b65 100644 --- a/src/java/org/apache/poi/util/DrawingDump.java +++ b/src/java/org/apache/poi/util/DrawingDump.java @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.util; import java.io.File; @@ -37,23 +37,20 @@ public final class DrawingDump { } public static void main( String[] args ) throws IOException { - OutputStreamWriter osw = new OutputStreamWriter(System.out, Charset.defaultCharset()); - PrintWriter pw = new PrintWriter(osw); - POIFSFileSystem fs = new POIFSFileSystem(new File(args[0])); - HSSFWorkbook wb = new HSSFWorkbook(fs); - try { + try (OutputStreamWriter osw = new OutputStreamWriter(System.out, Charset.defaultCharset()); + PrintWriter pw = new PrintWriter(osw); + POIFSFileSystem fs = new POIFSFileSystem(new File(args[0])); + HSSFWorkbook wb = new HSSFWorkbook(fs)) { + pw.println( "Drawing group:" ); wb.dumpDrawingGroupRecords(true); - + int i = 1; for (Sheet sheet : wb) { pw.println( "Sheet " + i + "(" + sheet.getSheetName() + "):" ); ((HSSFSheet) sheet).dumpDrawingRecords(true, pw); } - } finally { - wb.close(); - fs.close(); } } } diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 1a53cdd0b0..f1532b75e2 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -162,13 +162,13 @@ public final class IOUtils { * @return A byte array with the read bytes. * @throws IOException If reading data fails or EOF is encountered too early for the given length. */ - public static byte[] toByteArray(InputStream stream, final long length, final int maxLength) throws IOException { + public static byte[] toByteArray(InputStream stream, final int length, final int maxLength) throws IOException { if (length < 0L || maxLength < 0L) { throw new RecordFormatException("Can't allocate an array of length < 0"); } - if (length > (long)Integer.MAX_VALUE) { - throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE); - } + // if (length > (long)Integer.MAX_VALUE) { + // throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE); + // } if ((length != Integer.MAX_VALUE) || (maxLength != Integer.MAX_VALUE)) { checkLength(length, maxLength); } |