Bug 528717 - Use try-with-resources
Change-Id: Ia7be3ac6cbc8f95dfb56db645b032377ca80b588
Signed-off-by: Mickael Istria <mistria@redhat.com>
diff --git a/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.core.prefs b/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.core.prefs
index 2cacc08..3a7958c 100644
--- a/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.core.prefs
+++ b/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.core.prefs
@@ -125,8 +125,10 @@
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.8
+org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
+org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
@@ -137,15 +139,23 @@
org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80
org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16
+org.eclipse.jdt.core.formatter.alignment_for_expressions_in_for_loop_header=0
+org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0
+org.eclipse.jdt.core.formatter.alignment_for_module_statements=16
org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16
+org.eclipse.jdt.core.formatter.alignment_for_parameterized_type_references=0
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16
+org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=53
org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16
+org.eclipse.jdt.core.formatter.alignment_for_type_arguments=0
+org.eclipse.jdt.core.formatter.alignment_for_type_parameters=0
+org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16
org.eclipse.jdt.core.formatter.blank_lines_after_imports=1
org.eclipse.jdt.core.formatter.blank_lines_after_package=1
org.eclipse.jdt.core.formatter.blank_lines_before_field=0
@@ -165,11 +175,13 @@
org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line
+org.eclipse.jdt.core.formatter.brace_position_for_lambda_body=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false
+org.eclipse.jdt.core.formatter.comment.count_line_length_from_starting_position=false
org.eclipse.jdt.core.formatter.comment.format_block_comments=false
org.eclipse.jdt.core.formatter.comment.format_header=false
org.eclipse.jdt.core.formatter.comment.format_html=true
@@ -181,10 +193,16 @@
org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=do not insert
org.eclipse.jdt.core.formatter.comment.line_length=80
+org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true
+org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries=true
+org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=false
org.eclipse.jdt.core.formatter.compact_else_if=true
org.eclipse.jdt.core.formatter.continuation_indentation=2
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2
+org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off
+org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on
org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false
+org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true
@@ -196,13 +214,16 @@
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true
org.eclipse.jdt.core.formatter.indentation.size=4
+org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_enum_constant=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert
+org.eclipse.jdt.core.formatter.insert_new_line_after_label=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert
+org.eclipse.jdt.core.formatter.insert_new_line_after_type_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=do not insert
@@ -250,6 +271,7 @@
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert
org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert
+org.eclipse.jdt.core.formatter.insert_space_after_lambda_arrow=insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert
@@ -268,12 +290,14 @@
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized=do not insert
+org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert
org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert
+org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert
org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert
org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert
@@ -297,6 +321,7 @@
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized=do not insert
+org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert=insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case=insert
@@ -324,6 +349,7 @@
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert
+org.eclipse.jdt.core.formatter.insert_space_before_lambda_arrow=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert
@@ -352,6 +378,7 @@
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=insert
+org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while=insert
org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return=insert
org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw=insert
@@ -361,6 +388,7 @@
org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert
+org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert
@@ -370,16 +398,37 @@
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert
+org.eclipse.jdt.core.formatter.join_lines_in_comments=true
+org.eclipse.jdt.core.formatter.join_wrapped_lines=true
org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
org.eclipse.jdt.core.formatter.lineSplit=800
+org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false
+org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve=1
+org.eclipse.jdt.core.formatter.parentheses_positions_in_annotation=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_catch_clause=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_enum_constant_declaration=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_for_statment=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_if_while_statement=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_lambda_declaration=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_method_delcaration=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_method_invocation=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_switch_statement=common_lines
+org.eclipse.jdt.core.formatter.parentheses_positions_in_try_clause=separate_lines
org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=false
org.eclipse.jdt.core.formatter.tabulation.char=tab
org.eclipse.jdt.core.formatter.tabulation.size=4
+org.eclipse.jdt.core.formatter.use_on_off_tags=false
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
+org.eclipse.jdt.core.formatter.wrap_before_assignment_operator=false
+org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
+org.eclipse.jdt.core.formatter.wrap_before_conditional_operator=true
+org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
+org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=false
org.eclipse.jdt.core.incompatibleJDKLevel=ignore
org.eclipse.jdt.core.incompleteClasspath=error
+org.eclipse.jdt.core.javaFormatter=org.eclipse.jdt.core.defaultJavaFormatter
diff --git a/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.ui.prefs b/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.ui.prefs
index 522ca51..531fa2a 100644
--- a/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.ui.prefs
+++ b/bundles/org.eclipse.core.resources/.settings/org.eclipse.jdt.ui.prefs
@@ -1,7 +1,7 @@
eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
-formatter_profile=_core
-formatter_settings_version=11
+formatter_profile=_Unmanaged profile 'core' - 2
+formatter_settings_version=13
internal.default.compliance=user
org.eclipse.jdt.ui.ignorelowercasenames=true
org.eclipse.jdt.ui.importorder=;
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/Bucket.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/Bucket.java
index 85d56c0..35a641f 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/Bucket.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/Bucket.java
@@ -16,7 +16,6 @@
import java.util.*;
import org.eclipse.core.internal.resources.ResourceException;
import org.eclipse.core.internal.resources.ResourceStatus;
-import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.runtime.*;
@@ -294,8 +293,7 @@
this.entries.clear();
if (!this.location.isFile())
return;
- DataInputStream source = new DataInputStream(new BufferedInputStream(new FileInputStream(location), 8192));
- try {
+ try (DataInputStream source = new DataInputStream(new BufferedInputStream(new FileInputStream(location), 8192))) {
int version = source.readByte();
if (version != getVersion()) {
// unknown version
@@ -306,8 +304,6 @@
int entryCount = source.readInt();
for (int i = 0; i < entryCount; i++)
this.entries.put(readEntryKey(source), readEntryValue(source));
- } finally {
- source.close();
}
} catch (IOException ioe) {
String message = NLS.bind(Messages.resources_readMeta, location.getAbsolutePath());
@@ -344,17 +340,13 @@
if (parent == null)
throw new IOException();//caught and rethrown below
parent.mkdirs();
- DataOutputStream destination = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(location), 8192));
- try {
+ try (DataOutputStream destination = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(location), 8192))) {
destination.write(getVersion());
destination.writeInt(entries.size());
for (java.util.Map.Entry<String, Object> entry : entries.entrySet()) {
writeEntryKey(destination, entry.getKey());
writeEntryValue(destination, entry.getValue());
}
- destination.close();
- } finally {
- FileUtil.safeClose(destination);
}
needSaving = false;
} catch (IOException ioe) {
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/BucketTree.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/BucketTree.java
index f1c22cb..9347d0e 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/BucketTree.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/BucketTree.java
@@ -15,7 +15,6 @@
import org.eclipse.core.internal.localstore.Bucket.Visitor;
import org.eclipse.core.internal.resources.ResourceException;
import org.eclipse.core.internal.resources.Workspace;
-import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
@@ -139,16 +138,11 @@
File versionFile = getVersionFile();
if (!versionFile.getParentFile().exists())
versionFile.getParentFile().mkdirs();
- FileOutputStream stream = null;
- try {
- stream = new FileOutputStream(versionFile);
+ try (FileOutputStream stream = new FileOutputStream(versionFile)) {
stream.write(current.getVersion());
- stream.close();
} catch (IOException e) {
String message = NLS.bind(Messages.resources_writeWorkspaceMeta, versionFile.getAbsolutePath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, null, message, e);
- } finally {
- FileUtil.safeClose(stream);
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java
index 50e4d54..b814541 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/localstore/FileSystemResourceManager.java
@@ -373,11 +373,11 @@
* are not considered.
*/
private boolean descriptionChanged(IFile descriptionFile, byte[] newContents) {
- InputStream oldStream = null;
- try {
- //buffer size: twice the description length, but maximum 8KB
- int bufsize = newContents.length > 4096 ? 8192 : newContents.length * 2;
- oldStream = new BufferedInputStream(descriptionFile.getContents(true), bufsize);
+ //buffer size: twice the description length, but maximum 8KB
+ int bufsize = newContents.length > 4096 ? 8192 : newContents.length * 2;
+ try (
+ InputStream oldStream = new BufferedInputStream(descriptionFile.getContents(true), bufsize);
+ ) {
InputStream newStream = new ByteArrayInputStream(newContents);
//compare streams char by char, ignoring line endings
int newChar = newStream.read();
@@ -405,8 +405,6 @@
} catch (Exception e) {
Policy.log(e);
//if we failed to compare, just write the new contents
- } finally {
- FileUtil.safeClose(oldStream);
}
return true;
}
@@ -416,9 +414,9 @@
*/
@Deprecated
public int doGetEncoding(IFileStore store) throws CoreException {
- InputStream input = null;
- try {
- input = store.openInputStream(EFS.NONE, null);
+ try (
+ InputStream input = store.openInputStream(EFS.NONE, null);
+ ) {
int first = input.read();
int second = input.read();
if (first == -1 || second == -1)
@@ -440,8 +438,6 @@
} catch (IOException e) {
String message = NLS.bind(Messages.localstore_couldNotRead, store.toString());
throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, null, message, e);
- } finally {
- FileUtil.safeClose(input);
}
}
@@ -889,9 +885,9 @@
ProjectDescription description = null;
//hold onto any exceptions until after sync info is updated, then throw it
ResourceException error = null;
- InputStream in = null;
- try {
- in = new BufferedInputStream(descriptionStore.openInputStream(EFS.NONE, SubMonitor.convert(null)));
+ try (
+ InputStream in = new BufferedInputStream(descriptionStore.openInputStream(EFS.NONE, SubMonitor.convert(null)));
+ ) {
// IFileStore#openInputStream may cancel the monitor, thus the monitor state is checked
description = new ProjectDescriptionReader(target).read(new InputSource(in));
} catch (OperationCanceledException e) {
@@ -908,8 +904,8 @@
}
String msg = NLS.bind(Messages.resources_readProjectMeta, target.getName());
error = new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e);
- } finally {
- FileUtil.safeClose(in);
+ } catch (IOException ex) {
+ // ignore
}
if (error == null && description == null) {
String msg = NLS.bind(Messages.resources_readProjectMeta, target.getName());
@@ -1224,17 +1220,14 @@
//write the file that represents the project description
IFileStore fileStore = projectStore.getChild(IProjectDescription.DESCRIPTION_FILE_NAME);
- OutputStream out = null;
- try {
- out = fileStore.openOutputStream(EFS.NONE, null);
+ try (
+ OutputStream out = fileStore.openOutputStream(EFS.NONE, null)
+ ) {
IFile file = target.getFile(IProjectDescription.DESCRIPTION_FILE_NAME);
new ModelObjectWriter().write(desc, out, FileUtil.getLineSeparator(file));
- out.close();
} catch (IOException e) {
String msg = NLS.bind(Messages.resources_writeMeta, target.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), msg, e);
- } finally {
- FileUtil.safeClose(out);
}
//for backwards compatibility, ensure the old .prj file is deleted
getWorkspace().getMetaArea().clearOldDescription(target);
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ContentDescriptionManager.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ContentDescriptionManager.java
index 837247a..c5d8a83 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ContentDescriptionManager.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ContentDescriptionManager.java
@@ -445,8 +445,9 @@
if (Policy.DEBUG_CONTENT_TYPE)
Policy.debug("reading contents of " + file); //$NON-NLS-1$
// tries to obtain a description for this file contents
- InputStream contents = new LazyFileInputStream(file.getStore());
- try {
+ try (
+ InputStream contents = new LazyFileInputStream(file.getStore());
+ ) {
IContentTypeMatcher matcher = getContentTypeMatcher((Project) file.getProject());
return matcher.getDescriptionFor(contents, file.getName(), IContentDescription.ALL);
} catch (FileNotFoundException e) {
@@ -455,8 +456,6 @@
} catch (IOException e) {
String message = NLS.bind(Messages.resources_errorContentDescription, file.getFullPath());
throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, file.getFullPath(), message, e);
- } finally {
- FileUtil.safeClose(contents);
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/FileState.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/FileState.java
index ef24960..72ab461 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/FileState.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/FileState.java
@@ -13,7 +13,8 @@
import java.io.*;
import org.eclipse.core.internal.localstore.IHistoryStore;
-import org.eclipse.core.internal.utils.*;
+import org.eclipse.core.internal.utils.Messages;
+import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.content.IContentDescription;
@@ -48,16 +49,12 @@
// tries to obtain a description for the file contents
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
- InputStream contents = new BufferedInputStream(getContents());
- try {
+ try (InputStream contents = new BufferedInputStream(getContents())) {
IContentDescription description = contentTypeManager.getDescriptionFor(contents, getName(), new QualifiedName[] {IContentDescription.CHARSET});
- contents.close();
return description == null ? null : description.getCharset();
} catch (IOException e) {
String message = NLS.bind(Messages.history_errorContentDescription, getFullPath());
throw new ResourceException(IResourceStatus.FAILED_DESCRIBING_CONTENTS, getFullPath(), message, e);
- } finally {
- FileUtil.safeClose(contents);
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java
index 7fc7c70..ac851b1 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/LocalMetaArea.java
@@ -22,7 +22,8 @@
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.internal.localstore.SafeChunkyInputStream;
import org.eclipse.core.internal.localstore.SafeChunkyOutputStream;
-import org.eclipse.core.internal.utils.*;
+import org.eclipse.core.internal.utils.Messages;
+import org.eclipse.core.internal.utils.Policy;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
@@ -359,63 +360,57 @@
if (!file.exists())
return;
}
- try {
- SafeChunkyInputStream input = new SafeChunkyInputStream(file, 500);
- DataInputStream dataIn = new DataInputStream(input);
+ try (DataInputStream dataIn = new DataInputStream(new SafeChunkyInputStream(file, 500))) {
try {
- try {
- String location = dataIn.readUTF();
- if (location.length() > 0) {
- //location format < 3.2 was a local file system OS path
- //location format >= 3.2 is: URI_PREFIX + uri.toString()
- if (location.startsWith(URI_PREFIX))
- description.setLocationURI(URI.create(location.substring(URI_PREFIX.length())));
- else
- description.setLocationURI(URIUtil.toURI(Path.fromOSString(location)));
- }
- } catch (Exception e) {
- //don't allow failure to read the location to propagate
- String msg = NLS.bind(Messages.resources_exReadProjectLocation, target.getName());
- Policy.log(new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e));
+ String location = dataIn.readUTF();
+ if (location.length() > 0) {
+ //location format < 3.2 was a local file system OS path
+ //location format >= 3.2 is: URI_PREFIX + uri.toString()
+ if (location.startsWith(URI_PREFIX))
+ description.setLocationURI(URI.create(location.substring(URI_PREFIX.length())));
+ else
+ description.setLocationURI(URIUtil.toURI(Path.fromOSString(location)));
}
- //try to read the dynamic references - will fail for old location files
- int numRefs = dataIn.readInt();
- IProject[] references = new IProject[numRefs];
- IWorkspaceRoot root = getWorkspace().getRoot();
- for (int i = 0; i < numRefs; i++)
- references[i] = root.getProject(dataIn.readUTF());
- description.setDynamicReferences(references);
-
- // Since 3.7 - Build Configurations
- String[] configs = new String[dataIn.readInt()];
- for (int i = 0; i < configs.length; i++)
- configs[i] = dataIn.readUTF();
- if (configs.length > 0)
- // In the future we may decide this is better stored in the
- // .project, so only set if configs.length > 0
- description.setBuildConfigs(configs);
- // Active configuration name
- description.setActiveBuildConfig(dataIn.readUTF());
- // Build configuration references?
- int numBuildConifgsWithRefs = dataIn.readInt();
- HashMap<String, IBuildConfiguration[]> m = new HashMap<>(numBuildConifgsWithRefs);
- for (int i = 0; i < numBuildConifgsWithRefs; i++) {
- String configName = dataIn.readUTF();
- numRefs = dataIn.readInt();
- IBuildConfiguration[] refs = new IBuildConfiguration[numRefs];
- for (int j = 0; j < numRefs; j++) {
- String projName = dataIn.readUTF();
- if (dataIn.readBoolean())
- refs[j] = new BuildConfiguration(root.getProject(projName), dataIn.readUTF());
- else
- refs[j] = new BuildConfiguration(root.getProject(projName), null);
- }
- m.put(configName, refs);
- }
- description.setBuildConfigReferences(m);
- } finally {
- dataIn.close();
+ } catch (Exception e) {
+ //don't allow failure to read the location to propagate
+ String msg = NLS.bind(Messages.resources_exReadProjectLocation, target.getName());
+ Policy.log(new ResourceStatus(IStatus.ERROR, IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), msg, e));
}
+ //try to read the dynamic references - will fail for old location files
+ int numRefs = dataIn.readInt();
+ IProject[] references = new IProject[numRefs];
+ IWorkspaceRoot root = getWorkspace().getRoot();
+ for (int i = 0; i < numRefs; i++)
+ references[i] = root.getProject(dataIn.readUTF());
+ description.setDynamicReferences(references);
+
+ // Since 3.7 - Build Configurations
+ String[] configs = new String[dataIn.readInt()];
+ for (int i = 0; i < configs.length; i++)
+ configs[i] = dataIn.readUTF();
+ if (configs.length > 0)
+ // In the future we may decide this is better stored in the
+ // .project, so only set if configs.length > 0
+ description.setBuildConfigs(configs);
+ // Active configuration name
+ description.setActiveBuildConfig(dataIn.readUTF());
+ // Build configuration references?
+ int numBuildConifgsWithRefs = dataIn.readInt();
+ HashMap<String, IBuildConfiguration[]> m = new HashMap<>(numBuildConifgsWithRefs);
+ for (int i = 0; i < numBuildConifgsWithRefs; i++) {
+ String configName = dataIn.readUTF();
+ numRefs = dataIn.readInt();
+ IBuildConfiguration[] refs = new IBuildConfiguration[numRefs];
+ for (int j = 0; j < numRefs; j++) {
+ String projName = dataIn.readUTF();
+ if (dataIn.readBoolean())
+ refs[j] = new BuildConfiguration(root.getProject(projName), dataIn.readUTF());
+ else
+ refs[j] = new BuildConfiguration(root.getProject(projName), null);
+ }
+ m.put(configName, refs);
+ }
+ description.setBuildConfigReferences(m);
} catch (IOException e) {
//ignore - this is an old location file or an exception occurred
// closing the stream
@@ -463,49 +458,42 @@
if (projectLocation == null && prjRefs.length == 0 && buildConfigs.length == 0 && configRefs.isEmpty())
return;
//write the private metadata file
- try {
- SafeChunkyOutputStream output = new SafeChunkyOutputStream(file);
- DataOutputStream dataOut = new DataOutputStream(output);
- try {
- if (projectLocation == null)
- dataOut.writeUTF(""); //$NON-NLS-1$
- else
- dataOut.writeUTF(URI_PREFIX + projectLocation.toString());
- dataOut.writeInt(prjRefs.length);
- for (IProject prjRef : prjRefs)
- dataOut.writeUTF(prjRef.getName());
+ try (SafeChunkyOutputStream output = new SafeChunkyOutputStream(file); DataOutputStream dataOut = new DataOutputStream(output);) {
+ if (projectLocation == null)
+ dataOut.writeUTF(""); //$NON-NLS-1$
+ else
+ dataOut.writeUTF(URI_PREFIX + projectLocation.toString());
+ dataOut.writeInt(prjRefs.length);
+ for (IProject prjRef : prjRefs)
+ dataOut.writeUTF(prjRef.getName());
- // Since 3.7 - build configurations + references
- // Write out the build configurations
- dataOut.writeInt(buildConfigs.length);
- for (String buildConfig : buildConfigs) {
- dataOut.writeUTF(buildConfig);
- }
- // Write active configuration name
- dataOut.writeUTF(desc.getActiveBuildConfig());
- // Write out the configuration level references
- dataOut.writeInt(configRefs.size());
- for (Map.Entry<String, IBuildConfiguration[]> e : configRefs.entrySet()) {
- String refdName = e.getKey();
- IBuildConfiguration[] refs = e.getValue();
+ // Since 3.7 - build configurations + references
+ // Write out the build configurations
+ dataOut.writeInt(buildConfigs.length);
+ for (String buildConfig : buildConfigs) {
+ dataOut.writeUTF(buildConfig);
+ }
+ // Write active configuration name
+ dataOut.writeUTF(desc.getActiveBuildConfig());
+ // Write out the configuration level references
+ dataOut.writeInt(configRefs.size());
+ for (Map.Entry<String, IBuildConfiguration[]> e : configRefs.entrySet()) {
+ String refdName = e.getKey();
+ IBuildConfiguration[] refs = e.getValue();
- dataOut.writeUTF(refdName);
- dataOut.writeInt(refs.length);
- for (IBuildConfiguration ref : refs) {
- dataOut.writeUTF(ref.getProject().getName());
- if (ref.getName() == null) {
- dataOut.writeBoolean(false);
- } else {
- dataOut.writeBoolean(true);
- dataOut.writeUTF(ref.getName());
- }
+ dataOut.writeUTF(refdName);
+ dataOut.writeInt(refs.length);
+ for (IBuildConfiguration ref : refs) {
+ dataOut.writeUTF(ref.getProject().getName());
+ if (ref.getName() == null) {
+ dataOut.writeBoolean(false);
+ } else {
+ dataOut.writeBoolean(true);
+ dataOut.writeUTF(ref.getName());
}
}
- output.succeed();
- dataOut.close();
- } finally {
- FileUtil.safeClose(dataOut);
}
+ output.succeed();
} catch (IOException e) {
String message = NLS.bind(Messages.resources_exSaveProjectLocation, target.getName());
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e);
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/MarkerManager.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/MarkerManager.java
index 7baded7..3ea3216 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/MarkerManager.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/MarkerManager.java
@@ -533,12 +533,11 @@
if (!sourceFile.exists() && !tempFile.exists())
return;
try {
- DataInputStream input = new DataInputStream(new SafeFileInputStream(sourceLocation.toOSString(), tempLocation.toOSString()));
- try {
+ try (
+ DataInputStream input = new DataInputStream(new SafeFileInputStream(sourceLocation.toOSString(), tempLocation.toOSString()));
+ ) {
MarkerReader reader = new MarkerReader(workspace);
reader.read(input, generateDeltas);
- } finally {
- input.close();
}
} catch (Exception e) {
//don't let runtime exceptions such as ArrayIndexOutOfBounds prevent startup
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java
index 5c02b25..5857ed4 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ModelObjectWriter.java
@@ -153,14 +153,11 @@
* to be used in case we could not successfully write the new file.
*/
public void write(Object object, IPath location, IPath tempLocation, String lineSeparator) throws IOException {
- SafeFileOutputStream file = null;
String tempPath = tempLocation == null ? null : tempLocation.toOSString();
- try {
- file = new SafeFileOutputStream(location.toOSString(), tempPath);
+ try (
+ SafeFileOutputStream file = new SafeFileOutputStream(location.toOSString(), tempPath);
+ ) {
write(object, file, lineSeparator);
- file.close();
- } finally {
- FileUtil.safeClose(file);
}
}
@@ -168,11 +165,11 @@
* The OutputStream is closed in this method.
*/
public void write(Object object, OutputStream output, String lineSeparator) throws IOException {
- try {
+ try (
XMLWriter writer = new XMLWriter(output, lineSeparator);
+ ) {
write(object, writer);
writer.flush();
- writer.close();
if (writer.checkError())
throw new IOException();
} finally {
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescriptionReader.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescriptionReader.java
index ccf469c..dd14cf8 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescriptionReader.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescriptionReader.java
@@ -23,7 +23,8 @@
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.internal.events.BuildCommand;
import org.eclipse.core.internal.localstore.SafeFileInputStream;
-import org.eclipse.core.internal.utils.*;
+import org.eclipse.core.internal.utils.Messages;
+import org.eclipse.core.internal.utils.Policy;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
@@ -952,12 +953,10 @@
* Reads and returns a project description stored at the given location
*/
public ProjectDescription read(IPath location) throws IOException {
- BufferedInputStream file = null;
- try {
- file = new BufferedInputStream(new FileInputStream(location.toFile()));
+ try (
+ BufferedInputStream file = new BufferedInputStream(new FileInputStream(location.toFile()));
+ ) {
return read(new InputSource(file));
- } finally {
- FileUtil.safeClose(file);
}
}
@@ -966,11 +965,10 @@
* temporary location.
*/
public ProjectDescription read(IPath location, IPath tempLocation) throws IOException {
- SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString());
- try {
+ try (
+ SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString());
+ ) {
return read(new InputSource(file));
- } finally {
- file.close();
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPreferences.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPreferences.java
index 00054eb..3d7f742 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPreferences.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectPreferences.java
@@ -162,9 +162,9 @@
if (Policy.DEBUG_PREFERENCES)
Policy.debug("Loading preferences from file: " + file.getFullPath()); //$NON-NLS-1$
Properties result = new Properties();
- InputStream input = null;
- try {
- input = new BufferedInputStream(file.getContents(true));
+ try (
+ InputStream input = new BufferedInputStream(file.getContents(true));
+ ) {
result.load(input);
} catch (CoreException e) {
if (e.getStatus().getCode() == IResourceStatus.RESOURCE_NOT_FOUND) {
@@ -179,8 +179,6 @@
String message = NLS.bind(Messages.preferences_loadException, file.getFullPath());
log(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, IStatus.ERROR, message, e));
throw new BackingStoreException(message);
- } finally {
- FileUtil.safeClose(input);
}
return result;
}
@@ -507,9 +505,9 @@
if (Policy.DEBUG_PREFERENCES)
Policy.debug("Loading preferences from file: " + localFile.getFullPath()); //$NON-NLS-1$
Properties fromDisk = new Properties();
- InputStream input = null;
- try {
- input = new BufferedInputStream(localFile.getContents(true));
+ try (
+ InputStream input = new BufferedInputStream(localFile.getContents(true));
+ ) {
fromDisk.load(input);
convertFromProperties(this, fromDisk, true);
loadedNodes.add(absolutePath());
@@ -530,8 +528,6 @@
log(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, IStatus.ERROR, message, e));
throw new BackingStoreException(message);
}
- } finally {
- FileUtil.safeClose(input);
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SafeFileTable.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SafeFileTable.java
index 35e6730..c19210c 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SafeFileTable.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SafeFileTable.java
@@ -14,7 +14,6 @@
import java.io.*;
import java.util.Properties;
import java.util.Set;
-import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.ResourcesPlugin;
@@ -64,11 +63,10 @@
if (!target.exists())
return;
try {
- FileInputStream input = new FileInputStream(target);
- try {
+ try (
+ FileInputStream input = new FileInputStream(target);
+ ) {
table.load(input);
- } finally {
- input.close();
}
} catch (IOException e) {
String message = Messages.resources_exSafeRead;
@@ -79,12 +77,10 @@
public void save() throws CoreException {
java.io.File target = location.toFile();
try {
- FileOutputStream output = new FileOutputStream(target);
- try {
+ try (
+ FileOutputStream output = new FileOutputStream(target);
+ ) {
table.store(output, "safe table"); //$NON-NLS-1$
- output.close();
- } finally {
- FileUtil.safeClose(output);
}
} catch (IOException e) {
String message = Messages.resources_exSafeSave;
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SaveManager.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SaveManager.java
index a4eff88..7cd7803 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SaveManager.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/SaveManager.java
@@ -838,11 +838,10 @@
return;
}
try {
- SafeChunkyInputStream input = new SafeChunkyInputStream(target);
- try {
+ try (
+ SafeChunkyInputStream input = new SafeChunkyInputStream(target);
+ ) {
masterTable.load(input);
- } finally {
- input.close();
}
} catch (IOException e) {
String message = Messages.resources_exMasterTable;
@@ -948,12 +947,12 @@
/* Read each of the snapshots and lay them on top of the current tree.*/
ElementTree complete = workspace.getElementTree();
complete.immutable();
- DataInputStream input = new DataInputStream(new SafeChunkyInputStream(localFile));
- try {
+ try (
+ DataInputStream input = new DataInputStream(new SafeChunkyInputStream(localFile));
+ ) {
WorkspaceTreeReader reader = WorkspaceTreeReader.getReader(workspace, input.readInt());
complete = reader.readSnapshotTree(input, complete, monitor);
} finally {
- FileUtil.safeClose(input);
//reader returned an immutable tree, but since we're inside
//an operation, we must return an open tree
lastSnap = complete;
@@ -1034,11 +1033,10 @@
return;
}
try {
- DataInputStream input = new DataInputStream(new SafeFileInputStream(treeLocation.toOSString(), tempLocation.toOSString(), TREE_BUFFER_SIZE));
- try {
+ try (
+ DataInputStream input = new DataInputStream(new SafeFileInputStream(treeLocation.toOSString(), tempLocation.toOSString(), TREE_BUFFER_SIZE));
+ ) {
WorkspaceTreeReader.getReader(workspace, input.readInt()).readTree(input, monitor);
- } finally {
- input.close();
}
} catch (IOException e) {
String msg = NLS.bind(Messages.resources_readMeta, treeLocation.toOSString());
@@ -1068,12 +1066,11 @@
IPath tempLocation = workspace.getMetaArea().getBackupLocationFor(treeLocation);
if (!treeLocation.toFile().exists() && !tempLocation.toFile().exists())
return false;
- DataInputStream input = new DataInputStream(new SafeFileInputStream(treeLocation.toOSString(), tempLocation.toOSString()));
- try {
+ try (
+ DataInputStream input = new DataInputStream(new SafeFileInputStream(treeLocation.toOSString(), tempLocation.toOSString()));
+ ) {
WorkspaceTreeReader reader = WorkspaceTreeReader.getReader(workspace, input.readInt());
reader.readTree(project, input, Policy.subMonitorFor(monitor, Policy.totalWork));
- } finally {
- input.close();
}
} catch (IOException e) {
message = NLS.bind(Messages.resources_readMeta, project.getFullPath());
@@ -1106,12 +1103,12 @@
zip.close();
return false;
}
- DataInputStream input = new DataInputStream(zip);
- try {
+ try (
+ DataInputStream input = new DataInputStream(zip);
+ ) {
WorkspaceTreeReader reader = WorkspaceTreeReader.getReader(workspace, input.readInt(), true);
reader.readTree(project, input, Policy.subMonitorFor(monitor, Policy.totalWork));
} finally {
- input.close();
zip.close();
}
} catch (IOException e) {
@@ -1266,13 +1263,11 @@
try {
if (kind == ISaveContext.FULL_SAVE || kind == ISaveContext.SNAPSHOT)
validateMasterTableBeforeSave(target);
- SafeChunkyOutputStream output = new SafeChunkyOutputStream(target);
- try {
+ try (
+ SafeChunkyOutputStream output = new SafeChunkyOutputStream(target);
+ ) {
masterTable.store(output, "master table"); //$NON-NLS-1$
output.succeed();
- output.close();
- } finally {
- FileUtil.safeClose(output);
}
} catch (IOException e) {
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, NLS.bind(Messages.resources_exSaveMaster, location.toOSString()), e);
@@ -1342,13 +1337,11 @@
ZipOutputStream out = null;
try {
FileOutputStream fis = new FileOutputStream(tmpTree);
- DataOutputStream output = new DataOutputStream(fis);
- try {
+ try (
+ DataOutputStream output = new DataOutputStream(fis);
+ ) {
output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
writeTree(project, output, monitor);
- output.close();
- } finally {
- FileUtil.safeClose(output);
}
OutputStream snapOut = store.openOutputStream(EFS.NONE, monitor);
out = new ZipOutputStream(snapOut);
@@ -1357,15 +1350,13 @@
out.putNextEntry(e);
int read = 0;
byte[] buffer = new byte[4096];
- InputStream in = new FileInputStream(tmpTree);
- try {
+ try (
+ InputStream in = new FileInputStream(tmpTree);
+ ) {
while ((read = in.read(buffer)) >= 0) {
out.write(buffer, 0, read);
}
out.closeEntry();
- in.close();
- } finally {
- FileUtil.safeClose(in);
}
out.close();
} catch (IOException e) {
@@ -1388,13 +1379,11 @@
IPath treeLocation = workspace.getMetaArea().getTreeLocationFor(workspace.getRoot(), true);
try {
IPath tempLocation = workspace.getMetaArea().getBackupLocationFor(treeLocation);
- DataOutputStream output = new DataOutputStream(new SafeFileOutputStream(treeLocation.toOSString(), tempLocation.toOSString()));
- try {
+ try (
+ DataOutputStream output = new DataOutputStream(new SafeFileOutputStream(treeLocation.toOSString(), tempLocation.toOSString()));
+ ) {
output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
writeTree(computeStatesToSave(contexts, workspace.getElementTree()), output, monitor);
- output.close();
- } finally {
- FileUtil.safeClose(output);
}
} catch (Exception e) {
String msg = NLS.bind(Messages.resources_writeWorkspaceMeta, treeLocation);
@@ -1486,15 +1475,14 @@
java.io.File localFile = snapPath.toFile();
try {
SafeChunkyOutputStream safeStream = new SafeChunkyOutputStream(localFile);
- DataOutputStream out = new DataOutputStream(safeStream);
- try {
+ try (
+ DataOutputStream out = new DataOutputStream(safeStream);
+ ) {
out.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
writeWorkspaceFields(out, monitor);
writer.writeDelta(tree, lastSnap, Path.ROOT, ElementTreeWriter.D_INFINITE, out, ResourceComparator.getSaveComparator());
safeStream.succeed();
out.close();
- } finally {
- FileUtil.safeClose(out);
}
} catch (IOException e) {
message = NLS.bind(Messages.resources_writeWorkspaceMeta, localFile.getAbsolutePath());
@@ -1590,8 +1578,9 @@
private void validateMasterTableBeforeSave(java.io.File target) throws IOException {
if (target.exists()) {
MasterTable previousMasterTable = new MasterTable();
- SafeChunkyInputStream input = new SafeChunkyInputStream(target);
- try {
+ try (
+ SafeChunkyInputStream input = new SafeChunkyInputStream(target);
+ ) {
previousMasterTable.load(input);
String stringValue = previousMasterTable.getProperty(ROOT_SEQUENCE_NUMBER_KEY);
// if there was a full save, then there must be a non-null entry for root
@@ -1603,8 +1592,6 @@
String message = "Cannot set lower sequence number for root (previous: " + valueInFile + ", new: " + valueInMemory + "). Location: " + target.getAbsolutePath(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
Assert.isLegal(valueInMemory >= valueInFile, message);
}
- } finally {
- input.close();
}
}
}
@@ -2081,13 +2068,11 @@
IPath tempLocation = workspace.getMetaArea().getBackupLocationFor(treeLocation);
try {
SafeFileOutputStream safe = new SafeFileOutputStream(treeLocation.toOSString(), tempLocation.toOSString());
- DataOutputStream output = new DataOutputStream(safe);
- try {
+ try (
+ DataOutputStream output = new DataOutputStream(safe);
+ ) {
output.writeInt(ICoreConstants.WORKSPACE_TREE_VERSION_2);
writeTree(project, output, null);
- output.close();
- } finally {
- FileUtil.safeClose(output);
}
} catch (IOException e) {
String msg = NLS.bind(Messages.resources_writeMeta, project.getFullPath());
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Synchronizer.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Synchronizer.java
index 274a1d7..b700d18 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Synchronizer.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/Synchronizer.java
@@ -152,12 +152,11 @@
if (!sourceLocation.toFile().exists() && !tempLocation.toFile().exists())
return;
try {
- DataInputStream input = new DataInputStream(new SafeFileInputStream(sourceLocation.toOSString(), tempLocation.toOSString()));
- try {
+ try (
+ DataInputStream input = new DataInputStream(new SafeFileInputStream(sourceLocation.toOSString(), tempLocation.toOSString()));
+ ) {
SyncInfoReader reader = new SyncInfoReader(workspace, this);
reader.readSyncInfo(input);
- } finally {
- input.close();
}
} catch (Exception e) {
//don't let runtime exceptions such as ArrayIndexOutOfBounds prevent startup
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/WorkspaceDescriptionReader.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/WorkspaceDescriptionReader.java
index dfea8b6..b9b0355 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/WorkspaceDescriptionReader.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/WorkspaceDescriptionReader.java
@@ -83,11 +83,10 @@
}
public Object read(IPath location, IPath tempLocation) throws IOException {
- SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString());
- try {
+ try (
+ SafeFileInputStream file = new SafeFileInputStream(location.toOSString(), tempLocation.toOSString());
+ ) {
return read(file);
- } finally {
- file.close();
}
}
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/refresh/win32/Win32Monitor.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/refresh/win32/Win32Monitor.java
index 1dfe52a..a3d57ab 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/refresh/win32/Win32Monitor.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/refresh/win32/Win32Monitor.java
@@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.core.internal.resources.refresh.win32;
+import java.io.Closeable;
import java.io.File;
import java.util.*;
import org.eclipse.core.internal.utils.Messages;
@@ -89,12 +90,10 @@
if (next != null) {
if (next.isOpen()) {
if (!next.exists()) {
+ next.close();
if (next instanceof LinkedResourceHandle) {
- next.close();
LinkedResourceHandle linkedResourceHandle = (LinkedResourceHandle) next;
linkedResourceHandle.postRefreshRequest();
- } else {
- next.close();
}
ChainedHandle previous = getPrevious();
if (previous != null)
@@ -137,13 +136,14 @@
}
}
- protected abstract class Handle {
+ protected abstract class Handle implements Closeable {
protected long handleValue;
public Handle() {
handleValue = Win32Natives.INVALID_HANDLE_VALUE;
}
+ @Override
public void close() {
if (isOpen()) {
if (!Win32Natives.FindCloseChangeNotification(handleValue)) {
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/FileUtil.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/FileUtil.java
index faf48bb..62c3449 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/FileUtil.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/FileUtil.java
@@ -283,9 +283,9 @@
*/
public static String getLineSeparator(IFile file) {
if (file.exists()) {
- InputStream input = null;
- try {
- input = file.getContents();
+ try (
+ InputStream input = file.getContents()
+ ) {
int c = input.read();
while (c != -1 && c != '\r' && c != '\n')
c = input.read();
@@ -300,8 +300,6 @@
// ignore
} catch (IOException e) {
// ignore
- } finally {
- safeClose(input);
}
}
Preferences rootNode = Platform.getPreferencesService().getRootNode();
diff --git a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/UniversalUniqueIdentifier.java b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/UniversalUniqueIdentifier.java
index 53ed4ae..0a6dcc6 100644
--- a/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/UniversalUniqueIdentifier.java
+++ b/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/utils/UniversalUniqueIdentifier.java
@@ -86,7 +86,7 @@
of the byte array is compatible with the <code>toBytes()</code> method.
<p>The constructor returns the undefined uuid if the byte array is invalid.
-
+
@see #toBytes()
@see #BYTES_SIZE
*/
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java
index 8ea9f9d..3111667 100644
--- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java
+++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java
@@ -401,14 +401,10 @@
description.append("</natures></projectDescription>\n");
// write the description
- OutputStream output = null;
- try {
- output = descStore.openOutputStream(EFS.NONE, getMonitor());
+ try (OutputStream output = descStore.openOutputStream(EFS.NONE, getMonitor());) {
output.write(description.toString().getBytes());
} catch (CoreException e) {
fail("1.0");
- } finally {
- FileUtil.safeClose(output);
}
try {
diff --git a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java
index 3a10089..b0180b6 100644
--- a/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java
+++ b/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/ResourceTest.java
@@ -19,7 +19,6 @@
import java.util.Set;
import org.eclipse.core.filesystem.*;
import org.eclipse.core.internal.resources.Resource;
-import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.internal.utils.UniversalUniqueIdentifier;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
@@ -845,13 +844,8 @@
}
java.io.File osFile = location.toFile();
try {
- FileOutputStream os = null;
- try {
- os = new FileOutputStream(osFile);
+ try (FileOutputStream os = new FileOutputStream(osFile);) {
os.write(newContent.getBytes("UTF8"));
- os.close();
- } finally {
- FileUtil.safeClose(os);
}
} catch (IOException e) {
fail(m + "0.0", e);