Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close inputStream to avoid resource leaks #1025

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -37,31 +38,37 @@ public void update(IProject project) throws CoreException, IOException
File file = getClangdConfigFile(project);

// Load existing clangd file
FileInputStream inputStream = new FileInputStream(file);
Yaml yaml = new Yaml();
Object obj = yaml.load(inputStream);
try (FileInputStream inputStream = new FileInputStream(file))
{
Yaml yaml = new Yaml();
Object obj = yaml.load(inputStream);

// Create new YAML structure if file is empty
Map<String, Object> data = createOrGetExistingYamlStructure(obj);
// Create new YAML structure if file is empty
Map<String, Object> data = createOrGetExistingYamlStructure(obj);

// Add or update CompileFlags section
Map<String, Object> compileFlags = (Map<String, Object>) data.get("CompileFlags"); //$NON-NLS-1$
if (compileFlags == null)
{
compileFlags = new LinkedHashMap<>();
data.put("CompileFlags", compileFlags); //$NON-NLS-1$
}
updateCompileFlagsSection(compileFlags, project
.getPersistentProperty(new QualifiedName(IDFCorePlugin.PLUGIN_ID, IDFConstants.BUILD_DIR_PROPERTY)));
// Add or update CompileFlags section
Map<String, Object> compileFlags = (Map<String, Object>) data.get("CompileFlags"); //$NON-NLS-1$
if (compileFlags == null)
{
compileFlags = new LinkedHashMap<>();
data.put("CompileFlags", compileFlags); //$NON-NLS-1$
}
updateCompileFlagsSection(compileFlags, project.getPersistentProperty(
new QualifiedName(IDFCorePlugin.PLUGIN_ID, IDFConstants.BUILD_DIR_PROPERTY)));

// Write updated clangd back to file
try (Writer writer = new FileWriter(file))
{
yaml.dump(data, writer);
// Write updated clangd back to file
try (Writer writer = new FileWriter(file, StandardCharsets.UTF_8))
{
yaml.dump(data, writer);
}
catch (IOException e)
{
throw new IOException("Error writing .clangd file: " + e.getMessage(), e); //$NON-NLS-1$
}
}
catch (IOException e)
{
throw new IOException("Error writing .clangd file: " + e.getMessage(), e); //$NON-NLS-1$
throw new IOException("Error reading .clangd file: " + e.getMessage(), e); //$NON-NLS-1$
}
}

Expand Down
Loading