diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentLineDifferTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentLineDifferTest.java index 58bd8fa0649..22abaee6b4c 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentLineDifferTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentLineDifferTest.java @@ -14,7 +14,8 @@ package org.eclipse.jdt.text.tests.performance; import java.io.IOException; -import java.io.InputStreamReader; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; @@ -73,14 +74,7 @@ public void setId(String id) { protected static final String SMALL_FAUST_MANY_CHANGES_SAME_SIZE; static { - String faust; - try { - faust= FileTool.read(new InputStreamReader(AbstractDocumentLineDifferTest.class.getResourceAsStream("faust1.txt"))).toString(); - } catch (IOException x) { - faust= ""; - x.printStackTrace(); - } - FAUST1= faust; + FAUST1= AbstractDocumentLineDifferTest.getFaust(); FAUST_FEW_CHANGES= FAUST1.replaceAll("MARGARETE", "GRETCHEN"); @@ -122,4 +116,12 @@ protected final void setUpDiffer(DocumentLineDiffer differ) { differ.setReferenceProvider(fReferenceProvider); } + static String getFaust() { + try (InputStream resourceAsStream= AbstractDocumentLineDifferTest.class.getResourceAsStream("faust1.txt")) { + return new String(resourceAsStream.readAllBytes(), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentPerformanceTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentPerformanceTest.java index 3fc47713234..48df2efac6e 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentPerformanceTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/AbstractDocumentPerformanceTest.java @@ -13,8 +13,6 @@ *******************************************************************************/ package org.eclipse.jdt.text.tests.performance; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -52,16 +50,9 @@ public Doc(ITextStore store, ILineTracker tracker) { static { - String faust; - try { - faust = FileTool.read(new InputStreamReader(AbstractDocumentPerformanceTest.class.getResourceAsStream("faust1.txt"))).toString(); - } catch (IOException x) { - faust = ""; - x.printStackTrace(); - } - FAUST1 = faust; - FAUST100 = faust.substring(0, 100).intern(); - FAUST500 = faust.substring(0, 500).intern(); + FAUST1 = AbstractDocumentLineDifferTest.getFaust(); + FAUST100 = FAUST1.substring(0, 100).intern(); + FAUST500 = FAUST1.substring(0, 500).intern(); // Set local fingerprints LOCAL_FINGERPRINTS.put("measureDeleteInsert", "Document: delete and insert"); diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java index 746234da9b7..e34da42aaaa 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/FileTool.java @@ -17,12 +17,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.Reader; import java.io.Writer; import java.net.URL; import java.util.Enumeration; @@ -152,37 +150,9 @@ public static File getFileInPlugin(Plugin plugin, IPath path) { } } - public static StringBuffer read(String fileName) throws IOException { - return read(new FileReader(fileName)); - } - - public static StringBuffer read(Reader reader) throws IOException { - StringBuffer s= new StringBuffer(); - try { - char[] buffer= new char[8196]; - int chars= reader.read(buffer); - while (chars != -1) { - s.append(buffer, 0, chars); - chars= reader.read(buffer); - } - } finally { - try { - reader.close(); - } catch (IOException e) { - } - } - return s; - } - public static void write(String fileName, StringBuffer content) throws IOException { - Writer writer= new FileWriter(fileName); - try { + try (Writer writer= new FileWriter(fileName)) { writer.write(content.toString()); - } finally { - try { - writer.close(); - } catch (IOException e) { - } } } } diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/LineTrackerPerformanceTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/LineTrackerPerformanceTest.java index a206ef65765..261b5232022 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/LineTrackerPerformanceTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/LineTrackerPerformanceTest.java @@ -13,20 +13,17 @@ *******************************************************************************/ package org.eclipse.jdt.text.tests.performance; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.Arrays; import java.util.Random; -import junit.framework.Test; -import junit.framework.TestSuite; - import org.eclipse.test.performance.PerformanceMeter; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.DefaultLineTracker; import org.eclipse.jface.text.ILineTracker; +import junit.framework.Test; +import junit.framework.TestSuite; /** * @@ -38,15 +35,9 @@ public class LineTrackerPerformanceTest extends TextPerformanceTestCase { protected static final String FAUST500; static { - String faust; - try { - faust= FileTool.read(new InputStreamReader(AbstractDocumentLineDifferTest.class.getResourceAsStream("faust1.txt"))).toString(); - } catch (IOException x) { - faust= ""; - x.printStackTrace(); - } - FAUST1= faust; - FAUST500= faust.substring(0, 500); + FAUST1 = AbstractDocumentLineDifferTest.getFaust(); + + FAUST500= FAUST1.substring(0, 500); } diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ResourceTestHelper.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ResourceTestHelper.java index c8e74b55fba..3615fa1ae9c 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ResourceTestHelper.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ResourceTestHelper.java @@ -16,9 +16,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipException; @@ -132,7 +132,9 @@ public static IFile[] findFiles(String prefix, String suffix, int i, int n) { } public static StringBuffer read(String src) throws IOException, CoreException { - return FileTool.read(new InputStreamReader(getFile(src).getContents())); + try (InputStream contents= getFile(src).getContents()){ + return new StringBuffer(new String(contents.readAllBytes(), StandardCharsets.UTF_8)); + } } public static void write(String dest, final String content) throws CoreException { diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ScrollVerticalRulerTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ScrollVerticalRulerTest.java index 60340fa9ecb..77fce5a3fff 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ScrollVerticalRulerTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/ScrollVerticalRulerTest.java @@ -13,8 +13,8 @@ *******************************************************************************/ package org.eclipse.jdt.text.tests.performance; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import org.eclipse.core.runtime.preferences.IEclipsePreferences; @@ -120,7 +120,7 @@ protected void tearDown() throws Exception { protected AbstractTextEditor openEditor(ScrollingMode mode) throws Exception { IFile file= ResourceTestHelper.getProject(PerformanceTestSetup.PROJECT).getFile("faust.txt"); if (!file.exists()) { - file.create(getFaustInputStream(), true, null); + file.create(new ByteArrayInputStream(AbstractDocumentLineDifferTest.getFaust().getBytes(StandardCharsets.UTF_8)), true, null); } AbstractTextEditor result= (AbstractTextEditor) EditorTestHelper.openInEditor(file, true); @@ -134,17 +134,13 @@ protected AbstractTextEditor openEditor(ScrollingMode mode) throws Exception { } private String getFaust(int numberOfLines) throws Exception { - String faust= FileTool.read(new InputStreamReader(getFaustInputStream())).toString(); + String faust= AbstractDocumentLineDifferTest.getFaust(); IDocument document= new Document(faust); int lineOffset= document.getLineOffset(numberOfLines); return document.get(0, lineOffset); } - private InputStream getFaustInputStream() { - return AbstractDocumentPerformanceTest.class.getResourceAsStream("faust1.txt"); - } - /* * @see org.eclipse.jdt.text.tests.performance.ScrollEditorTest#assertEditor(org.eclipse.ui.texteditor.AbstractTextEditor) */ diff --git a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextStorePerformanceTest.java b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextStorePerformanceTest.java index 53c2398f9b0..8fd197d9846 100644 --- a/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextStorePerformanceTest.java +++ b/org.eclipse.jdt.text.tests/src/org/eclipse/jdt/text/tests/performance/TextStorePerformanceTest.java @@ -13,8 +13,6 @@ *******************************************************************************/ package org.eclipse.jdt.text.tests.performance; -import java.io.IOException; -import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -41,17 +39,9 @@ public abstract class TextStorePerformanceTest extends TextPerformanceTestCase2 protected static final Map LOCAL_FINGERPRINTS= new HashMap<>(); static { - String faust; - try { - faust = FileTool.read(new InputStreamReader(TextStorePerformanceTest.class.getResourceAsStream("faust1.txt"))).toString(); - } catch (IOException x) { - faust = ""; - x.printStackTrace(); - } - - FAUST1 = faust; - FAUST100 = faust.substring(0, 100).intern(); - FAUST500 = faust.substring(0, 500).intern(); + FAUST1 = AbstractDocumentLineDifferTest.getFaust(); + FAUST100 = FAUST1.substring(0, 100).intern(); + FAUST500 = FAUST1.substring(0, 500).intern(); } abstract protected ITextStore createTextStore(); diff --git a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/GenericRefactoringTest.java b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/GenericRefactoringTest.java index afde628fecd..e3fa992570f 100644 --- a/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/GenericRefactoringTest.java +++ b/org.eclipse.jdt.ui.tests.refactoring/test cases/org/eclipse/jdt/ui/tests/refactoring/GenericRefactoringTest.java @@ -476,7 +476,8 @@ protected void printTestDisabledMessage(String explanation){ } //----------------------- - public static InputStream getStream(String content){ + /* return ByteArrayInputStream because that does not need to be closed */ + public static ByteArrayInputStream getStream(String content){ return new ByteArrayInputStream(content.getBytes(ENCODING)); }