mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
cleanup: use try with resources
This commit is contained in:
@@ -117,13 +117,9 @@ public class CmdlineProtoUtil {
|
||||
}
|
||||
if (cause != null) {
|
||||
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
final PrintStream stream = new PrintStream(baos);
|
||||
try {
|
||||
try (PrintStream stream = new PrintStream(baos)) {
|
||||
cause.printStackTrace(stream);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
final String stacktrace = new String(baos.toByteArray());
|
||||
builder.setStacktrace(stacktrace);
|
||||
if (description == null) {
|
||||
|
||||
@@ -157,15 +157,9 @@ public class CompilerReferenceIndex<Input> {
|
||||
public void saveVersion(@NotNull File buildDir, int version) {
|
||||
File versionFile = new File(getIndexDir(buildDir), VERSION_FILE);
|
||||
|
||||
try {
|
||||
FileUtil.createIfDoesntExist(versionFile);
|
||||
final DataOutputStream os = new DataOutputStream(new FileOutputStream(versionFile));
|
||||
try {
|
||||
os.writeInt(version);
|
||||
}
|
||||
finally {
|
||||
os.close();
|
||||
}
|
||||
FileUtil.createIfDoesntExist(versionFile);
|
||||
try (DataOutputStream os = new DataOutputStream(new FileOutputStream(versionFile))) {
|
||||
os.writeInt(version);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
LOG.error(ex);
|
||||
|
||||
@@ -3114,14 +3114,8 @@ public class Mappings {
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
final File file = new File(outputRoot, info[i]);
|
||||
FileUtil.createIfDoesntExist(file);
|
||||
try {
|
||||
final PrintStream stream = new PrintStream(file);
|
||||
try {
|
||||
data[i].toStream(myContext, stream);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
try (PrintStream stream = new PrintStream(file)) {
|
||||
data[i].toStream(myContext, stream);
|
||||
}
|
||||
catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -507,6 +507,13 @@ final class BuildSession implements Runnable, CanceledStatus {
|
||||
}
|
||||
|
||||
private static void saveOnDisk(BufferExposingByteArrayOutputStream bytes, final File file) throws IOException {
|
||||
try (FileOutputStream fos = writeOrCreate(file)) {
|
||||
fos.write(bytes.getInternalBuffer(), 0, bytes.size());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static FileOutputStream writeOrCreate(@NotNull File file) throws FileNotFoundException {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
//noinspection IOResourceOpenedButNotSafelyClosed
|
||||
@@ -519,12 +526,7 @@ final class BuildSession implements Runnable, CanceledStatus {
|
||||
if (fos == null) {
|
||||
fos = new FileOutputStream(file);
|
||||
}
|
||||
try {
|
||||
fos.write(bytes.getInternalBuffer(), 0, bytes.size());
|
||||
}
|
||||
finally {
|
||||
fos.close();
|
||||
}
|
||||
return fos;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -231,7 +231,9 @@ public class JarsBuilder {
|
||||
if (manifestFile.exists()) {
|
||||
final String fullManifestPath = FileUtil.toSystemIndependentName(manifestFile.getAbsolutePath());
|
||||
packedFilePaths.add(fullManifestPath);
|
||||
return createManifest(new FileInputStream(manifestFile), manifestFile);
|
||||
try (FileInputStream stream = new FileInputStream(manifestFile)) {
|
||||
return createManifest(stream, manifestFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -240,7 +242,9 @@ public class JarsBuilder {
|
||||
@Override
|
||||
public void process(@Nullable InputStream inputStream, @NotNull String relativePath, ZipEntry entry) throws IOException {
|
||||
if (manifestRef.isNull() && relativePath.equals(manifestPath) && inputStream != null) {
|
||||
manifestRef.set(createManifest(inputStream, descriptor.getRootFile()));
|
||||
try (InputStream stream = inputStream) {
|
||||
manifestRef.set(createManifest(stream, descriptor.getRootFile()));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -256,12 +260,7 @@ public class JarsBuilder {
|
||||
@Nullable
|
||||
private Manifest createManifest(InputStream manifestStream, File manifestFile) {
|
||||
try {
|
||||
try {
|
||||
return new Manifest(manifestStream);
|
||||
}
|
||||
finally {
|
||||
manifestStream.close();
|
||||
}
|
||||
return new Manifest(manifestStream);
|
||||
}
|
||||
catch (IOException e) {
|
||||
myContext.processMessage(new CompilerMessage(IncArtifactBuilder.BUILDER_NAME, BuildMessage.Kind.ERROR,
|
||||
|
||||
@@ -49,25 +49,19 @@ public class JarBasedArtifactRootDescriptor extends ArtifactRootDescriptor {
|
||||
prefix = "";
|
||||
}
|
||||
|
||||
try {
|
||||
ZipFile zipFile = new ZipFile(myRoot);
|
||||
try {
|
||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
try (ZipFile zipFile = new ZipFile(myRoot)) {
|
||||
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
final String name = entry.getName();
|
||||
if (name.startsWith(prefix)) {
|
||||
String relativePath = name.substring(prefix.length());
|
||||
if (myPathInJarFilter.value(relativePath)) {
|
||||
processor.process(entry.isDirectory() ? null : zipFile.getInputStream(entry), relativePath, entry);
|
||||
}
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
final String name = entry.getName();
|
||||
if (name.startsWith(prefix)) {
|
||||
String relativePath = name.substring(prefix.length());
|
||||
if (myPathInJarFilter.value(relativePath)) {
|
||||
processor.process(entry.isDirectory() ? null : zipFile.getInputStream(entry), relativePath, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
zipFile.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IOException("Error occurred during processing zip file " + myRoot + ": " + e.getMessage(), e);
|
||||
|
||||
@@ -389,16 +389,10 @@ public class BuildDataManager implements StorageOwner {
|
||||
public void saveVersion() {
|
||||
final Boolean differs = myVersionDiffers;
|
||||
if (differs == null || differs) {
|
||||
try {
|
||||
FileUtil.createIfDoesntExist(myVersionFile);
|
||||
final DataOutputStream os = new DataOutputStream(new FileOutputStream(myVersionFile));
|
||||
try {
|
||||
os.writeInt(VERSION);
|
||||
myVersionDiffers = Boolean.FALSE;
|
||||
}
|
||||
finally {
|
||||
os.close();
|
||||
}
|
||||
FileUtil.createIfDoesntExist(myVersionFile);
|
||||
try (DataOutputStream os = new DataOutputStream(new FileOutputStream(myVersionFile))) {
|
||||
os.writeInt(VERSION);
|
||||
myVersionDiffers = Boolean.FALSE;
|
||||
}
|
||||
catch (IOException ignored) {
|
||||
}
|
||||
|
||||
@@ -102,14 +102,10 @@ public class BuildTargetConfiguration {
|
||||
try {
|
||||
File configFile = getConfigFile();
|
||||
FileUtil.createParentDirs(configFile);
|
||||
Writer out = new BufferedWriter(new FileWriter(configFile));
|
||||
try {
|
||||
try (Writer out = new BufferedWriter(new FileWriter(configFile))) {
|
||||
out.write(data);
|
||||
myConfiguration = data;
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info("Cannot save configuration of " + myConfiguration, e);
|
||||
|
||||
@@ -88,25 +88,19 @@ public class BuildTargetTypeState {
|
||||
}
|
||||
|
||||
public synchronized void save() {
|
||||
try {
|
||||
FileUtil.createParentDirs(myTargetsFile);
|
||||
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myTargetsFile)));
|
||||
try {
|
||||
output.writeInt(VERSION);
|
||||
output.writeInt(myTargetIds.size() + myStaleTargetIds.size());
|
||||
for (Map.Entry<BuildTarget<?>, Integer> entry : myTargetIds.entrySet()) {
|
||||
IOUtil.writeString(entry.getKey().getId(), output);
|
||||
output.writeInt(entry.getValue());
|
||||
}
|
||||
for (Pair<String, Integer> pair : myStaleTargetIds) {
|
||||
IOUtil.writeString(pair.first, output);
|
||||
output.writeInt(pair.second);
|
||||
}
|
||||
output.writeLong(myAverageTargetBuildTimeMs);
|
||||
FileUtil.createParentDirs(myTargetsFile);
|
||||
try (DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myTargetsFile)))) {
|
||||
output.writeInt(VERSION);
|
||||
output.writeInt(myTargetIds.size() + myStaleTargetIds.size());
|
||||
for (Map.Entry<BuildTarget<?>, Integer> entry : myTargetIds.entrySet()) {
|
||||
IOUtil.writeString(entry.getKey().getId(), output);
|
||||
output.writeInt(entry.getValue());
|
||||
}
|
||||
finally {
|
||||
output.close();
|
||||
for (Pair<String, Integer> pair : myStaleTargetIds) {
|
||||
IOUtil.writeString(pair.first, output);
|
||||
output.writeInt(pair.second);
|
||||
}
|
||||
output.writeLong(myAverageTargetBuildTimeMs);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info("Cannot save " + myTargetType.getTypeId() + " targets data: " + e.getMessage(), e);
|
||||
|
||||
@@ -68,13 +68,9 @@ public class BuildTargetsState {
|
||||
try {
|
||||
File targetTypesFile = getTargetTypesFile();
|
||||
FileUtil.createParentDirs(targetTypesFile);
|
||||
DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(targetTypesFile)));
|
||||
try {
|
||||
try (DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(targetTypesFile)))) {
|
||||
output.writeInt(myMaxTargetId.get());
|
||||
}
|
||||
finally {
|
||||
output.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info("Cannot save targets info: " + e.getMessage(), e);
|
||||
|
||||
@@ -59,14 +59,10 @@ public class BuildResult implements MessageHandler {
|
||||
void storeMappingsDump(ProjectDescriptor pd) throws IOException {
|
||||
final ByteArrayOutputStream dump = new ByteArrayOutputStream();
|
||||
|
||||
final PrintStream stream = new PrintStream(dump);
|
||||
try {
|
||||
try (PrintStream stream = new PrintStream(dump)) {
|
||||
pd.dataManager.getMappings().toStream(stream);
|
||||
dumpSourceToOutputMappings(pd, stream);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
|
||||
dump.close();
|
||||
myMappingsDump = dump.toString();
|
||||
|
||||
@@ -19,14 +19,10 @@ public class JDKSerializer<T> implements DataNodeSerializer<T> {
|
||||
@Override
|
||||
public byte[] getBytes(T data) throws IOException {
|
||||
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oOut = new ObjectOutputStream(bOut);
|
||||
try {
|
||||
try (ObjectOutputStream oOut = new ObjectOutputStream(bOut)) {
|
||||
oOut.writeObject(data);
|
||||
return bOut.toByteArray();
|
||||
}
|
||||
finally {
|
||||
oOut.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -342,22 +342,14 @@ public class ExternalProjectsDataStorage implements SettingsSavingComponent, Per
|
||||
});
|
||||
}
|
||||
|
||||
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(projectConfigurationFile)));
|
||||
try {
|
||||
try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(Files.newOutputStream(projectConfigurationFile)))) {
|
||||
out.writeUTF(STORAGE_VERSION);
|
||||
out.writeInt(externalProjects.size());
|
||||
ObjectOutputStream os = new ObjectOutputStream(out);
|
||||
try {
|
||||
try (ObjectOutputStream os = new ObjectOutputStream(out)) {
|
||||
for (InternalExternalProjectInfo externalProject : externalProjects) {
|
||||
os.writeObject(externalProject);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -384,15 +376,12 @@ public class ExternalProjectsDataStorage implements SettingsSavingComponent, Per
|
||||
throw new IOException("External projects data storage was invalidated");
|
||||
}
|
||||
|
||||
DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(configurationFile)));
|
||||
|
||||
try {
|
||||
try (DataInputStream in = new DataInputStream(new BufferedInputStream(Files.newInputStream(configurationFile)))) {
|
||||
final String storage_version = in.readUTF();
|
||||
if (!STORAGE_VERSION.equals(storage_version)) return projects;
|
||||
final int size = in.readInt();
|
||||
|
||||
ObjectInputStream os = new ObjectInputStream(in);
|
||||
try {
|
||||
try (ObjectInputStream os = new ObjectInputStream(in)) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
InternalExternalProjectInfo projectDataDataNode = (InternalExternalProjectInfo)os.readObject();
|
||||
projects.add(projectDataDataNode);
|
||||
@@ -401,12 +390,6 @@ public class ExternalProjectsDataStorage implements SettingsSavingComponent, Per
|
||||
catch (Exception e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
finally {
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
return projects;
|
||||
}
|
||||
|
||||
@@ -99,10 +99,7 @@ public class FirefoxUtil {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
try {
|
||||
BufferedReader reader;
|
||||
reader = new BufferedReader(new FileReader(profilesFile));
|
||||
try {
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(profilesFile))) {
|
||||
final List<FirefoxProfile> profiles = new SmartList<>();
|
||||
boolean insideProfile = false;
|
||||
String currentName = null;
|
||||
@@ -147,16 +144,12 @@ public class FirefoxUtil {
|
||||
}
|
||||
else //noinspection SpellCheckingInspection
|
||||
if (name.equalsIgnoreCase("isrelative") && value.equals("1")) {
|
||||
isRelative = true;
|
||||
}
|
||||
isRelative = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return profiles;
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
return Collections.emptyList();
|
||||
|
||||
@@ -71,19 +71,14 @@ public final class ConsentOptions {
|
||||
|
||||
@NotNull
|
||||
private String loadText(InputStream stream) {
|
||||
try {
|
||||
if (stream != null) {
|
||||
final Reader reader = new InputStreamReader(CharsetToolkit.inputStreamSkippingBOM(new BufferedInputStream(stream)), StandardCharsets.UTF_8);
|
||||
try {
|
||||
return new String(FileUtil.adaptiveLoadText(reader));
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
if (stream != null) {
|
||||
try (Reader reader = new InputStreamReader(CharsetToolkit.inputStreamSkippingBOM(new BufferedInputStream(stream)),
|
||||
StandardCharsets.UTF_8)) {
|
||||
return new String(FileUtil.adaptiveLoadText(reader));
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@@ -114,19 +114,14 @@ public final class EndUserAgreement {
|
||||
|
||||
@NotNull
|
||||
private static Document loadContent(final String docName, InputStream stream) {
|
||||
try {
|
||||
if (stream != null) {
|
||||
final Reader reader = new InputStreamReader(stream instanceof ByteArrayInputStream? stream : new BufferedInputStream(stream), StandardCharsets.UTF_8);
|
||||
try {
|
||||
return new Document(docName, new String(FileUtil.adaptiveLoadText(reader)));
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
if (stream != null) {
|
||||
try (Reader reader = new InputStreamReader(stream instanceof ByteArrayInputStream ? stream : new BufferedInputStream(stream),
|
||||
StandardCharsets.UTF_8)) {
|
||||
return new Document(docName, new String(FileUtil.adaptiveLoadText(reader)));
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
}
|
||||
return new Document(docName, "");
|
||||
}
|
||||
@@ -195,23 +190,17 @@ public final class EndUserAgreement {
|
||||
@NotNull
|
||||
private static Version parseVersion(String text) {
|
||||
if (!StringUtil.isEmptyOrSpaces(text)) {
|
||||
try {
|
||||
final BufferedReader reader = new BufferedReader(new StringReader(text));
|
||||
try {
|
||||
final String line = reader.readLine();
|
||||
if (line != null) {
|
||||
final int startComment = line.indexOf(VERSION_COMMENT_START);
|
||||
if (startComment >= 0 ) {
|
||||
final int endComment = line.indexOf(VERSION_COMMENT_END);
|
||||
if (endComment > startComment) {
|
||||
return Version.fromString(line.substring(startComment + VERSION_COMMENT_START.length(), endComment).trim());
|
||||
}
|
||||
try (BufferedReader reader = new BufferedReader(new StringReader(text))) {
|
||||
final String line = reader.readLine();
|
||||
if (line != null) {
|
||||
final int startComment = line.indexOf(VERSION_COMMENT_START);
|
||||
if (startComment >= 0) {
|
||||
final int endComment = line.indexOf(VERSION_COMMENT_END);
|
||||
if (endComment > startComment) {
|
||||
return Version.fromString(line.substring(startComment + VERSION_COMMENT_START.length(), endComment).trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.info(e);
|
||||
|
||||
@@ -153,7 +153,9 @@ public class RepositoryHelper {
|
||||
}
|
||||
}
|
||||
else {
|
||||
return parsePluginList(request.getReader());
|
||||
try (BufferedReader reader = request.getReader()) {
|
||||
return parsePluginList(reader);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -207,7 +209,10 @@ public class RepositoryHelper {
|
||||
}
|
||||
|
||||
private static List<IdeaPluginDescriptor> loadPluginList(File file) throws IOException {
|
||||
return parsePluginList(new InputStreamReader(new BufferedInputStream(new FileInputStream(file)), CharsetToolkit.UTF8_CHARSET));
|
||||
try (InputStreamReader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)),
|
||||
CharsetToolkit.UTF8_CHARSET)) {
|
||||
return parsePluginList(reader);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<IdeaPluginDescriptor> parsePluginList(Reader reader) throws IOException {
|
||||
@@ -220,9 +225,6 @@ public class RepositoryHelper {
|
||||
catch (ParserConfigurationException | SAXException | RuntimeException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<IdeaPluginDescriptor> process(String repositoryUrl, List<IdeaPluginDescriptor> list) {
|
||||
|
||||
@@ -25,7 +25,6 @@ import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.parsers.SAXParser;
|
||||
@@ -76,19 +75,12 @@ public class ImportedToGeneralTestEventsConverter extends OutputToGeneralTestEve
|
||||
}
|
||||
|
||||
public static void parseTestResults(Supplier<? extends Reader> readerSupplier, GeneralTestEventsProcessor processor) throws IOException {
|
||||
parseTestResults(readerSupplier.get(), ImportTestOutputExtension.findHandler(readerSupplier, processor));
|
||||
}
|
||||
|
||||
public static void parseTestResults(Reader reader, final DefaultHandler contentHandler) throws IOException {
|
||||
try {
|
||||
try (Reader reader = readerSupplier.get()) {
|
||||
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
|
||||
parser.parse(new InputSource(reader), contentHandler);
|
||||
parser.parse(new InputSource(reader), ImportTestOutputExtension.findHandler(readerSupplier, processor));
|
||||
}
|
||||
catch (ParserConfigurationException | SAXException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,8 +300,7 @@ public class ChangesCacheFile {
|
||||
|
||||
private void loadHeader() throws IOException {
|
||||
if (!myHeaderLoaded) {
|
||||
RandomAccessFile stream = new RandomAccessFile(myPath, "r");
|
||||
try {
|
||||
try (RandomAccessFile stream = new RandomAccessFile(myPath, "r")) {
|
||||
int version = stream.readInt();
|
||||
if (version != VERSION) {
|
||||
throw new VersionMismatchException();
|
||||
@@ -318,9 +317,6 @@ public class ChangesCacheFile {
|
||||
myIncomingCount = stream.readInt();
|
||||
assert stream.getFilePointer() == HEADER_SIZE;
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
myHeaderLoaded = true;
|
||||
}
|
||||
}
|
||||
@@ -679,10 +675,9 @@ public class ChangesCacheFile {
|
||||
partialFile.delete();
|
||||
}
|
||||
else if (accounted > 0) {
|
||||
RandomAccessFile file = new RandomAccessFile(partialFile, "rw");
|
||||
try {
|
||||
try (RandomAccessFile file = new RandomAccessFile(partialFile, "rw")) {
|
||||
file.writeInt(accounted);
|
||||
for(Change c: data.accountedChanges) {
|
||||
for (Change c : data.accountedChanges) {
|
||||
boolean isAfterRevision = true;
|
||||
ContentRevision revision = c.getAfterRevision();
|
||||
if (revision == null) {
|
||||
@@ -694,9 +689,6 @@ public class ChangesCacheFile {
|
||||
file.writeUTF(revision.getFile().getIOFile().toString());
|
||||
}
|
||||
}
|
||||
finally {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -705,8 +697,7 @@ public class ChangesCacheFile {
|
||||
try {
|
||||
File partialFile = getPartialPath(data.indexEntry.offset);
|
||||
if (partialFile.exists()) {
|
||||
RandomAccessFile file = new RandomAccessFile(partialFile, "r");
|
||||
try {
|
||||
try (RandomAccessFile file = new RandomAccessFile(partialFile, "r")) {
|
||||
int count = file.readInt();
|
||||
if (count > 0) {
|
||||
final Collection<Change> changes = data.changeList.getChanges();
|
||||
@@ -720,14 +711,15 @@ public class ChangesCacheFile {
|
||||
afterPaths.put(FilePathsHelper.convertPath(change.getAfterRevision().getFile()), change);
|
||||
}
|
||||
}
|
||||
for(int i=0; i<count; i++) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
boolean isAfterRevision = (file.readByte() != 0);
|
||||
String path = file.readUTF();
|
||||
final String converted = FilePathsHelper.convertPath(path);
|
||||
final Change change;
|
||||
if (isAfterRevision) {
|
||||
change = afterPaths.get(converted);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
change = beforePaths.get(converted);
|
||||
}
|
||||
if (change != null) {
|
||||
@@ -736,9 +728,6 @@ public class ChangesCacheFile {
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(IOException ex) {
|
||||
|
||||
@@ -978,14 +978,8 @@ public class ShelveChangesManager implements PersistentStateComponent<Element>,
|
||||
final String path,
|
||||
final List<FilePatch> remainingPatches,
|
||||
CommitContext commitContext) {
|
||||
try {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), CharsetToolkit.UTF8_CHARSET);
|
||||
try {
|
||||
UnifiedDiffWriter.write(project, remainingPatches, writer, "\n", commitContext);
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(path), CharsetToolkit.UTF8_CHARSET)) {
|
||||
UnifiedDiffWriter.write(project, remainingPatches, writer, "\n", commitContext);
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
|
||||
@@ -58,15 +58,11 @@ public class HashSerializeTest {
|
||||
@NotNull
|
||||
private static File writeToTempFile(@NotNull HashImpl... hashes) throws IOException {
|
||||
File file = FileUtil.createTempFile("", "");
|
||||
DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
|
||||
try {
|
||||
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(file))) {
|
||||
for (HashImpl hash : hashes) {
|
||||
hash.write(out);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,14 +81,10 @@ public class TestClientRunner {
|
||||
final Process clientProcess = builder.start();
|
||||
|
||||
if (stdin != null) {
|
||||
final OutputStream outputStream = clientProcess.getOutputStream();
|
||||
try {
|
||||
try (OutputStream outputStream = clientProcess.getOutputStream()) {
|
||||
final byte[] bytes = stdin.getBytes();
|
||||
outputStream.write(bytes);
|
||||
}
|
||||
finally {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
final CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), StringUtil.join(arguments, " "));
|
||||
|
||||
@@ -181,13 +181,9 @@ public class ByteCodeViewerManager extends DockablePopupManager<ByteCodeViewerCo
|
||||
private static String processClassFile(byte[] bytes) {
|
||||
final ClassReader classReader = new ClassReader(bytes);
|
||||
final StringWriter writer = new StringWriter();
|
||||
final PrintWriter printWriter = new PrintWriter(writer);
|
||||
try {
|
||||
try (PrintWriter printWriter = new PrintWriter(writer)) {
|
||||
classReader.accept(new TraceClassVisitor(null, new Textifier(), printWriter), 0);
|
||||
}
|
||||
finally {
|
||||
printWriter.close();
|
||||
}
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -960,13 +960,9 @@ public class GitCheckinEnvironment implements CheckinEnvironment {
|
||||
//noinspection SSBasedInspection
|
||||
file.deleteOnExit();
|
||||
@NonNls String encoding = GitConfigUtil.getCommitEncoding(project, root);
|
||||
Writer out = new OutputStreamWriter(new FileOutputStream(file), encoding);
|
||||
try {
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(file), encoding)) {
|
||||
out.write(message);
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,28 +78,20 @@ class GitInteractiveRebaseFile {
|
||||
}
|
||||
|
||||
public void cancel() throws IOException {
|
||||
PrintWriter out = new PrintWriter(new FileWriter(myFile));
|
||||
try {
|
||||
try (PrintWriter out = new PrintWriter(new FileWriter(myFile))) {
|
||||
out.println("# rebase is cancelled");
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void save(@NotNull List<GitRebaseEntry> entries) throws IOException {
|
||||
String encoding = GitConfigUtil.getLogEncoding(myProject, myRoot);
|
||||
PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(myFile), encoding));
|
||||
try {
|
||||
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(myFile), encoding))) {
|
||||
for (GitRebaseEntry e : entries) {
|
||||
if (e.getAction() != GitRebaseEntry.Action.SKIP) {
|
||||
out.println(e.getAction().toString() + " " + e.getCommit() + " " + e.getSubject());
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -223,24 +223,18 @@ public class GitRebaseUtils {
|
||||
File commitFile = new File(rebaseDir, String.format("%04d", next));
|
||||
String hash = null;
|
||||
String subject = null;
|
||||
try {
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(commitFile), CharsetToolkit.UTF8_CHARSET));
|
||||
try {
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (line.startsWith("From ")) {
|
||||
hash = line.substring(5, 5 + 40);
|
||||
}
|
||||
if (line.startsWith("Subject: ")) {
|
||||
subject = line.substring("Subject: ".length());
|
||||
}
|
||||
if (hash != null && subject != null) {
|
||||
break;
|
||||
}
|
||||
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(commitFile), CharsetToolkit.UTF8_CHARSET))) {
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (line.startsWith("From ")) {
|
||||
hash = line.substring(5, 5 + 40);
|
||||
}
|
||||
if (line.startsWith("Subject: ")) {
|
||||
subject = line.substring("Subject: ".length());
|
||||
}
|
||||
if (hash != null && subject != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -161,13 +161,9 @@ public class GitTagDialog extends DialogWrapper {
|
||||
try {
|
||||
messageFile = FileUtil.createTempFile(MESSAGE_FILE_PREFIX, MESSAGE_FILE_SUFFIX);
|
||||
messageFile.deleteOnExit();
|
||||
Writer out = new OutputStreamWriter(new FileOutputStream(messageFile), MESSAGE_FILE_ENCODING);
|
||||
try {
|
||||
try (Writer out = new OutputStreamWriter(new FileOutputStream(messageFile), MESSAGE_FILE_ENCODING)) {
|
||||
out.write(message);
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {
|
||||
Messages.showErrorDialog(myProject, GitBundle.message("tag.error.creating.message.file.message", ex.toString()),
|
||||
|
||||
@@ -298,16 +298,12 @@ public class GithubConnection {
|
||||
|
||||
@NotNull
|
||||
private static JsonElement parseResponse(@NotNull InputStream githubResponse) throws IOException {
|
||||
Reader reader = new InputStreamReader(githubResponse, CharsetToolkit.UTF8_CHARSET);
|
||||
try {
|
||||
try (Reader reader = new InputStreamReader(githubResponse, CharsetToolkit.UTF8_CHARSET)) {
|
||||
return new JsonParser().parse(reader);
|
||||
}
|
||||
catch (JsonParseException jse) {
|
||||
throw new GithubJsonException("Couldn't parse GitHub response", jse);
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static abstract class PagedRequestBase<T> implements PagedRequest<T> {
|
||||
|
||||
@@ -98,20 +98,15 @@ public abstract class AbstractConfigUtils {
|
||||
if (jars.length > 1) {
|
||||
Arrays.sort(jars);
|
||||
}
|
||||
JarFile jarFile = new JarFile(jars[0]);
|
||||
try {
|
||||
try (JarFile jarFile = new JarFile(jars[0])) {
|
||||
JarEntry jarEntry = jarFile.getJarEntry(manifestPath);
|
||||
if (jarEntry == null) {
|
||||
return null;
|
||||
}
|
||||
final InputStream inputStream = jarFile.getInputStream(jarEntry);
|
||||
Manifest manifest;
|
||||
try {
|
||||
try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
|
||||
manifest = new Manifest(inputStream);
|
||||
}
|
||||
finally {
|
||||
inputStream.close();
|
||||
}
|
||||
final String version = manifest.getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION);
|
||||
if (version != null) {
|
||||
return version;
|
||||
@@ -128,9 +123,6 @@ public abstract class AbstractConfigUtils {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
finally {
|
||||
jarFile.close();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOG.debug(e);
|
||||
|
||||
@@ -42,13 +42,9 @@ public class SocketServer {
|
||||
try {
|
||||
boolean _continue = true;
|
||||
while (_continue) {
|
||||
Socket socket = myServerSocket.accept();
|
||||
try {
|
||||
try (Socket socket = myServerSocket.accept()) {
|
||||
_continue = myProtocol.handleConnection(socket);
|
||||
}
|
||||
finally {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SocketException e) {
|
||||
|
||||
@@ -117,13 +117,9 @@ public class HgRepositoryReader {
|
||||
@NotNull
|
||||
private static byte[] readHashBytesFromFile(@NotNull File file) throws IOException {
|
||||
byte[] bytes;
|
||||
final InputStream stream = new FileInputStream(file);
|
||||
try {
|
||||
try (InputStream stream = new FileInputStream(file)) {
|
||||
bytes = FileUtil.loadBytes(stream, 20);
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
@@ -46,12 +46,8 @@ public class HgAnnotateCommandTest extends HgSingleUserTest {
|
||||
final File etalonFile = new File(myAnnotateDataDir, "etalon");
|
||||
|
||||
XStream xStream = new XStream();
|
||||
FileReader reader = new FileReader(etalonFile);
|
||||
try {
|
||||
myAnnotations = (List<HgAnnotationLine>) xStream.fromXML(reader);
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
try (FileReader reader = new FileReader(etalonFile)) {
|
||||
myAnnotations = (List<HgAnnotationLine>)xStream.fromXML(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,13 +101,9 @@ public class HgAnnotateCommandTest extends HgSingleUserTest {
|
||||
}
|
||||
|
||||
XStream xStream = new XStream();
|
||||
FileWriter writer = new FileWriter(etalonFile);
|
||||
try {
|
||||
try (FileWriter writer = new FileWriter(etalonFile)) {
|
||||
xStream.toXML(annotationLines, writer);
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -128,16 +128,10 @@ public abstract class SpellCheckerDictionaryGenerator {
|
||||
}
|
||||
builder.append(name);
|
||||
}
|
||||
try {
|
||||
final File dictionaryFile = new File(outFile);
|
||||
FileUtil.createIfDoesntExist(dictionaryFile);
|
||||
final FileWriter writer = new FileWriter(dictionaryFile.getPath());
|
||||
try {
|
||||
writer.write(builder.toString());
|
||||
}
|
||||
finally {
|
||||
writer.close();
|
||||
}
|
||||
final File dictionaryFile = new File(outFile);
|
||||
FileUtil.createIfDoesntExist(dictionaryFile);
|
||||
try (FileWriter writer = new FileWriter(dictionaryFile.getPath())) {
|
||||
writer.write(builder.toString());
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
|
||||
@@ -434,16 +434,10 @@ public class SplitterTest {
|
||||
if (is != null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, CharsetToolkit.UTF8_CHARSET));
|
||||
try {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, CharsetToolkit.UTF8_CHARSET))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user