Skip to content

Commit

Permalink
fix some 'Potential resource leak' warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Dec 5, 2023
1 parent 6276394 commit 3ecf44a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,17 +39,9 @@ public abstract class TextStorePerformanceTest extends TextPerformanceTestCase2
protected static final Map<String, String> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 3ecf44a

Please sign in to comment.