From 82bbbed2b504dc965fcdd7fabe0eb24f0a9edd50 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 1 Sep 2015 21:46:30 +0000 Subject: [PATCH] Fix some Forbidden APIs errors git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1700677 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hpsf/ClassID.java | 8 ++------ src/java/org/apache/poi/hpsf/CodePageString.java | 5 +++-- src/java/org/apache/poi/hpsf/SpecialPropertySet.java | 3 ++- .../org/apache/poi/hssf/usermodel/HSSFWorkbook.java | 4 +++- .../org/apache/poi/ss/formula/functions/Dec2Hex.java | 4 +--- .../org/apache/poi/ss/formula/functions/EDate.java | 10 +++++++++- .../org/apache/poi/hssf/dev/TestBiffViewer.java | 7 ++----- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/ClassID.java b/src/java/org/apache/poi/hpsf/ClassID.java index c78702c3c1..4db64c96bc 100644 --- a/src/java/org/apache/poi/hpsf/ClassID.java +++ b/src/java/org/apache/poi/hpsf/ClassID.java @@ -18,15 +18,13 @@ package org.apache.poi.hpsf; import org.apache.poi.util.HexDump; +import org.apache.poi.util.StringUtil; /** *

Represents a class ID (16 bytes). Unlike other little-endian * type the {@link ClassID} is not just 16 bytes stored in the wrong * order. Instead, it is a double word (4 bytes) followed by two * words (2 bytes each) followed by 8 bytes.

- * - * @author Rainer Klute <klute@rainer-klute.de> */ public class ClassID { @@ -238,11 +236,9 @@ public class ClassID */ public int hashCode() { - return new String(bytes).hashCode(); + return new String(bytes, StringUtil.UTF8).hashCode(); } - - /** *

Returns a human-readable representation of the Class ID in standard * format "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}".

diff --git a/src/java/org/apache/poi/hpsf/CodePageString.java b/src/java/org/apache/poi/hpsf/CodePageString.java index b94cb67c13..8eb8987b34 100644 --- a/src/java/org/apache/poi/hpsf/CodePageString.java +++ b/src/java/org/apache/poi/hpsf/CodePageString.java @@ -25,6 +25,7 @@ import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.apache.poi.util.StringUtil; @Internal class CodePageString @@ -64,7 +65,7 @@ class CodePageString { String result; if ( codepage == -1 ) - result = new String( _value ); + result = new String( _value, StringUtil.UTF8 ); else result = CodePageUtil.getStringFromCodePage(_value, codepage); final int terminator = result.indexOf( '\0' ); @@ -96,7 +97,7 @@ class CodePageString { String stringNT = string + "\0"; if ( codepage == -1 ) - _value = stringNT.getBytes(); + _value = stringNT.getBytes(StringUtil.UTF8); else _value = CodePageUtil.getBytesInCodePage(stringNT, codepage); } diff --git a/src/java/org/apache/poi/hpsf/SpecialPropertySet.java b/src/java/org/apache/poi/hpsf/SpecialPropertySet.java index 4bcd8ca71f..9eb5a9954a 100644 --- a/src/java/org/apache/poi/hpsf/SpecialPropertySet.java +++ b/src/java/org/apache/poi/hpsf/SpecialPropertySet.java @@ -20,6 +20,7 @@ package org.apache.poi.hpsf; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; import java.util.List; import org.apache.poi.hpsf.wellknown.PropertyIDMap; @@ -345,7 +346,7 @@ public abstract class SpecialPropertySet extends MutablePropertySet return Long.toString( LittleEndian.getUInt(b) ); } // Maybe it's a string? who knows! - return new String(b); + return new String(b, Charset.forName("ASCII")); } return propertyValue.toString(); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index f7d2e93fdd..dfca245899 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -23,7 +23,9 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -1713,7 +1715,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss DrawingGroupRecord r = (DrawingGroupRecord) workbook.findFirstRecordBySid( DrawingGroupRecord.sid ); r.decode(); List escherRecords = r.getEscherRecords(); - PrintWriter w = new PrintWriter(System.out); + PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset())); for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) { EscherRecord escherRecord = iterator.next(); diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java index 8bae27b8ac..caf92bb333 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java +++ b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java @@ -51,8 +51,6 @@ import org.apache.poi.ss.formula.eval.*; *
  • If this argument is negative, this function returns the #NUM! error value.
  • *
  • If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.
  • * - * - * @author cedric dot walter @ gmail dot com */ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction { @@ -111,7 +109,7 @@ public final class Dec2Hex extends Var1or2ArgFunction implements FreeRefFunction String hex; if (placesNumber != 0) { - hex = String.format("%0"+placesNumber+"X", number1.intValue()); + hex = String.format("%0"+placesNumber+"X", number1.intValue(), Locale.ROOT); } else { hex = Integer.toHexString(number1.intValue()); diff --git a/src/java/org/apache/poi/ss/formula/functions/EDate.java b/src/java/org/apache/poi/ss/formula/functions/EDate.java index 78c773d4a9..205879fedd 100644 --- a/src/java/org/apache/poi/ss/formula/functions/EDate.java +++ b/src/java/org/apache/poi/ss/formula/functions/EDate.java @@ -19,6 +19,8 @@ package org.apache.poi.ss.formula.functions; import java.util.Calendar; import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; import org.apache.poi.ss.formula.OperationEvaluationContext; import org.apache.poi.ss.formula.eval.BlankEval; @@ -33,6 +35,12 @@ import org.apache.poi.ss.usermodel.DateUtil; * Implementation for Excel EDATE () function. */ public class EDate implements FreeRefFunction { + /** + * Excel doesn't store TimeZone information in the file, so if in doubt, + * use UTC to perform calculations + */ + private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC"); + public static final FreeRefFunction instance = new EDate(); public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { @@ -44,7 +52,7 @@ public class EDate implements FreeRefFunction { int offsetInMonthAsNumber = (int) getValue(args[1]); Date startDate = DateUtil.getJavaDate(startDateAsNumber); - Calendar calendar = Calendar.getInstance(); + Calendar calendar = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT); calendar.setTime(startDate); calendar.add(Calendar.MONTH, offsetInMonthAsNumber); return new NumberEval(DateUtil.getExcelDate(calendar.getTime())); diff --git a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java index 776e278526..c96aec4f7f 100644 --- a/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java +++ b/src/testcases/org/apache/poi/hssf/dev/TestBiffViewer.java @@ -19,12 +19,9 @@ package org.apache.poi.hssf.dev; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.PrintStream; +import java.io.PrintWriter; -import org.apache.poi.POIDataSamples; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; -import org.junit.Ignore; -import org.junit.Test; public class TestBiffViewer extends BaseXLSIteratingTest { static { @@ -52,7 +49,7 @@ public class TestBiffViewer extends BaseXLSIteratingTest { InputStream is = BiffViewer.getPOIFSInputStream(fs); try { // use a NullOutputStream to not write the bytes anywhere for best runtime - BiffViewer.runBiffViewer(new PrintStream(NULL_OUTPUT_STREAM), is, true, true, true, false); + BiffViewer.runBiffViewer(new PrintWriter(NULL_OUTPUT_STREAM), is, true, true, true, false); } finally { is.close(); fs.close(); -- 2.39.5