Skip to content

Commit

Permalink
Use try-with-resource
Browse files Browse the repository at this point in the history
Doesn't try to fix anything. Just code and warnings reduced.
  • Loading branch information
EcljpseB0T authored and jukzi committed Jun 13, 2023
1 parent 07e3c2c commit 9034a33
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ public EngineTypeDescriptor[] getEngineTypes() {
public void save() {
IPath stateLoc = HelpUIPlugin.getDefault().getStateLocation();
String fileName = stateLoc.append(USER_FILE).toOSString();
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(fileName);
osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
PrintWriter writer = new PrintWriter(osw);
try (PrintWriter writer = new PrintWriter(
new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8))) {
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
writer.println("<engines>"); //$NON-NLS-1$
for (int i = 0; i < descriptors.size(); i++) {
Expand All @@ -126,26 +122,9 @@ public void save() {
}
writer.println("</engines>"); //$NON-NLS-1$
writer.flush();
}
catch (IOException e) {
} catch (IOException e) {
Platform.getLog(getClass()).error(Messages.EngineDescriptorManager_errorSaving, e);
}
finally {
if (osw!=null) {
try {
osw.close();
}
catch (IOException e) {
}
}
if (fos!=null) {
try {
fos.close();
}
catch (IOException e) {
}
}
}
}

public void load() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,33 +268,17 @@ public void handleException(Throwable e) {
*/
public XMLMemento readMemento(String filename) {
XMLMemento memento;
InputStreamReader reader = null;

try {
// Read the cheatsheet state file.
final File stateFile = getCheatSheetStateFile(filename);

FileInputStream input = new FileInputStream(stateFile);
reader = new InputStreamReader(input, StandardCharsets.UTF_8);
// Read the cheatsheet state file.
final File stateFile = getCheatSheetStateFile(filename);
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(stateFile), StandardCharsets.UTF_8)) {
memento = XMLMemento.createReadRoot(reader);


} catch (FileNotFoundException e) {
memento = null;
// Do nothing, the file will not exist the first time the workbench in used.
} catch (Exception e) {
String message = Messages.ERROR_READING_STATE_FILE;
CheatSheetPlugin.getPlugin().getLog().error(message, e);
memento = null;
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
// Not much to do, just catch the exception and keep going.
String message = Messages.ERROR_READING_STATE_FILE;
CheatSheetPlugin.getPlugin().getLog().error(message, e);
}
}
return memento;
}
Expand Down Expand Up @@ -337,25 +321,15 @@ public void handleException(Throwable e) {
public IStatus saveMemento(XMLMemento memento, String filename) {
// Save the IMemento to a file.
File stateFile = getCheatSheetStateFile(filename);
OutputStreamWriter writer = null;
try {
FileOutputStream stream = new FileOutputStream(stateFile);
writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(stateFile),
StandardCharsets.UTF_8)) {
memento.save(writer);
return Status.OK_STATUS;
} catch (IOException e) {
stateFile.delete();
String message = Messages.ERROR_WRITING_STATE_FILE;
IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
return status;
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
String message = Messages.ERROR_WRITING_STATE_FILE;
CheatSheetPlugin.getPlugin().getLog().error(message, e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,49 +105,26 @@ public Set<String> getContributors() {

private XMLMemento getReadMemento(String filename) {
XMLMemento memento;
InputStreamReader reader = null;

try {
final File stateFile = getStateFile(filename);

FileInputStream input = new FileInputStream(stateFile);
reader = new InputStreamReader(input, StandardCharsets.UTF_8);
final File stateFile = getStateFile(filename);
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(stateFile), StandardCharsets.UTF_8)){
memento = XMLMemento.createReadRoot(reader);


} catch (FileNotFoundException e) {
memento = null;
// Do nothing, the file will not exist the first time the workbench in used.
} catch (Exception e) {
// TODO should we log an error?
memento = null;
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
// TODO should we log an error?
}
}
return memento;
}

private void saveMemento(XMLMemento memento, String filename) {
// Save the IMemento to a file.
File stateFile = getStateFile(filename);
OutputStreamWriter writer = null;
try {
FileOutputStream stream = new FileOutputStream(stateFile);
writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8);
try (OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream(stateFile), StandardCharsets.UTF_8)){
memento.save(writer);
} catch (IOException e) {
stateFile.delete();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
Expand Down Expand Up @@ -1143,19 +1143,13 @@ private boolean filteredFromPresentation(AbstractIntroElement element) {
private StringBuilder readFromFile(String src, String charsetName) {
if (src == null)
return null;
InputStream stream = null;
StringBuilder content = new StringBuilder();
BufferedReader reader = null;
try {
URL url = new URL(src);
stream = url.openStream();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(src).openStream(),
(charsetName == null) ? Charset.defaultCharset().name() : charsetName))) {

// TODO: Do we need to worry about the encoding here? e.g.:
// reader = new BufferedReader(new InputStreamReader(stream,
// ResourcesPlugin.getEncoding()));
if (charsetName == null)
reader = new BufferedReader(new InputStreamReader(stream));
else
reader = new BufferedReader(new InputStreamReader(stream, charsetName));
while (true) {
int character = reader.read();
if (character == -1) // EOF
Expand All @@ -1182,16 +1176,6 @@ else if (character == PluginIdParser.SUBSTITUTION_BEGIN) { // possible
}
} catch (Exception exception) {
Log.error("Error reading from file", exception); //$NON-NLS-1$
} finally {
try {
if (reader != null)
reader.close();
if (stream != null)
stream.close();
} catch (IOException e) {
Log.error("Error closing input stream", e); //$NON-NLS-1$
return null;
}
}
return content;
}
Expand Down

0 comments on commit 9034a33

Please sign in to comment.