diff --git a/platform/util/src/com/intellij/execution/process/UnixProcessManager.java b/platform/util/src/com/intellij/execution/process/UnixProcessManager.java index 050e313f4631..5b8d3c5ef2f4 100644 --- a/platform/util/src/com/intellij/execution/process/UnixProcessManager.java +++ b/platform/util/src/com/intellij/execution/process/UnixProcessManager.java @@ -224,10 +224,8 @@ public class UnixProcessManager { } private static void processCommandOutput(Process process, Processor processor, boolean skipFirstLine, boolean throwOnError) throws IOException { - BufferedReader stdOutput = new BufferedReader(new InputStreamReader(process.getInputStream())); - try { - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - try { + try (BufferedReader stdOutput = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + try (BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { if (skipFirstLine) { stdOutput.readLine(); //ps output header } @@ -247,12 +245,6 @@ public class UnixProcessManager { throw new IOException("Error reading ps output:" + errorStr.toString()); } } - finally { - stdError.close(); - } - } - finally { - stdOutput.close(); } } diff --git a/platform/util/src/com/intellij/openapi/application/PathManager.java b/platform/util/src/com/intellij/openapi/application/PathManager.java index f80c0a66cafb..f911a60dc366 100644 --- a/platform/util/src/com/intellij/openapi/application/PathManager.java +++ b/platform/util/src/com/intellij/openapi/application/PathManager.java @@ -420,8 +420,7 @@ public class PathManager { for (String path : paths) { if (path != null && new File(path).exists()) { try { - Reader fis = new BufferedReader(new FileReader(path)); - try { + try (Reader fis = new BufferedReader(new FileReader(path))) { Map properties = FileUtil.loadProperties(fis); for (Map.Entry entry : properties.entrySet()) { String key = entry.getKey(); @@ -433,9 +432,6 @@ public class PathManager { } } } - finally { - fis.close(); - } } catch (IOException e) { log("Can't read property file '" + path + "': " + e.getMessage()); diff --git a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java index 21903caf1341..936cce1e14fb 100644 --- a/platform/util/src/com/intellij/openapi/util/JDOMUtil.java +++ b/platform/util/src/com/intellij/openapi/util/JDOMUtil.java @@ -355,13 +355,9 @@ public class JDOMUtil { } public static void writeDocument(@NotNull Document document, @NotNull String filePath, String lineSeparator) throws IOException { - OutputStream stream = new BufferedOutputStream(new FileOutputStream(filePath)); - try { + try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(filePath))) { writeDocument(document, stream, lineSeparator); } - finally { - stream.close(); - } } public static void writeDocument(@NotNull Document document, @NotNull File file, String lineSeparator) throws IOException { @@ -374,13 +370,9 @@ public class JDOMUtil { public static void write(@NotNull Element element, @NotNull File file, @Nullable String lineSeparator) throws IOException { FileUtil.createParentDirs(file); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CharsetToolkit.UTF8_CHARSET)); - try { + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CharsetToolkit.UTF8_CHARSET))) { writeElement(element, writer, createOutputter(lineSeparator)); } - finally { - writer.close(); - } } public static void write(@NotNull Parent element, @NotNull File file) throws IOException { @@ -390,13 +382,9 @@ public class JDOMUtil { public static void write(@NotNull Parent element, @NotNull File file, @NotNull String lineSeparator) throws IOException { FileUtil.createParentDirs(file); - OutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); - try { + try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) { write(element, stream, lineSeparator); } - finally { - stream.close(); - } } public static void writeDocument(@NotNull Document document, @NotNull OutputStream stream, String lineSeparator) throws IOException { @@ -404,8 +392,7 @@ public class JDOMUtil { } public static void write(@NotNull Parent element, @NotNull OutputStream stream, @NotNull String lineSeparator) throws IOException { - OutputStreamWriter writer = new OutputStreamWriter(stream, CharsetToolkit.UTF8_CHARSET); - try { + try (OutputStreamWriter writer = new OutputStreamWriter(stream, CharsetToolkit.UTF8_CHARSET)) { if (element instanceof Document) { writeDocument((Document)element, writer, lineSeparator); } @@ -413,9 +400,6 @@ public class JDOMUtil { writeElement((Element)element, writer, lineSeparator); } } - finally { - writer.close(); - } } @NotNull diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java index 60ef399519c0..d599612528d4 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java @@ -191,8 +191,7 @@ public class FileUtil extends FileUtilRt { @NotNull public static byte[] loadFileBytes(@NotNull File file) throws IOException { byte[] bytes; - final InputStream stream = new FileInputStream(file); - try { + try (InputStream stream = new FileInputStream(file)) { final long len = file.length(); if (len < 0) { throw new IOException("File length reported negative, probably doesn't exist"); @@ -204,9 +203,6 @@ public class FileUtil extends FileUtilRt { bytes = loadBytes(stream, (int)len); } - finally { - stream.close(); - } return bytes; } @@ -437,19 +433,11 @@ public class FileUtil extends FileUtilRt { private static void performCopy(@NotNull File fromFile, @NotNull File toFile, final boolean syncTimestamp) throws IOException { if (filesEqual(fromFile, toFile)) return; - final FileOutputStream fos = openOutputStream(toFile); - try { - final FileInputStream fis = new FileInputStream(fromFile); - try { + try (FileOutputStream fos = openOutputStream(toFile)) { + try (FileInputStream fis = new FileInputStream(fromFile)) { copy(fis, fos); } - finally { - fis.close(); - } - } - finally { - fos.close(); } if (syncTimestamp) { @@ -1109,13 +1097,9 @@ public class FileUtil extends FileUtilRt { private static void writeToFile(@NotNull File file, @NotNull byte[] text, int off, int len, boolean append) throws IOException { createParentDirs(file); - OutputStream stream = new FileOutputStream(file, append); - try { + try (OutputStream stream = new FileOutputStream(file, append)) { stream.write(text, off, len); } - finally { - stream.close(); - } } @NotNull diff --git a/platform/util/src/com/intellij/openapi/util/io/JarUtil.java b/platform/util/src/com/intellij/openapi/util/io/JarUtil.java index a119e00707bb..213ae5749380 100644 --- a/platform/util/src/com/intellij/openapi/util/io/JarUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/JarUtil.java @@ -52,13 +52,9 @@ public class JarUtil { public static boolean containsEntry(File file, String entryPath) { if (file.canRead()) { try { - JarFile jarFile = new JarFile(file); - try { + try (JarFile jarFile = new JarFile(file)) { return jarFile.getEntry(entryPath) != null; } - finally { - jarFile.close(); - } } catch (IOException ignored) { } } @@ -87,17 +83,13 @@ public class JarUtil { private static String getJarAttributeImpl(@NotNull File file, @Nullable String entryName, @NotNull Attributes.Name attribute) { if (file.canRead()) { try { - JarFile jarFile = new JarFile(file); - try { + try (JarFile jarFile = new JarFile(file)) { Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = entryName != null ? manifest.getAttributes(entryName) : manifest.getMainAttributes(); return attributes.getValue(attribute); } } - finally { - jarFile.close(); - } } catch (IOException e) { LOG.debug(e); @@ -115,8 +107,7 @@ public class JarUtil { public static Properties loadProperties(@NotNull File file, @NotNull String entryName) { if (file.canRead()) { try { - ZipFile zipFile = new ZipFile(file); - try { + try (ZipFile zipFile = new ZipFile(file)) { ZipEntry entry = zipFile.getEntry(entryName); if (entry != null) { Properties properties = new Properties(); @@ -124,9 +115,6 @@ public class JarUtil { return properties; } } - finally { - zipFile.close(); - } } catch (IOException e) { LOG.debug(e); diff --git a/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java b/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java index 6f965f4217ed..f67362193f5d 100644 --- a/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java +++ b/platform/util/src/com/intellij/openapi/vfs/CharsetToolkit.java @@ -472,13 +472,9 @@ public class CharsetToolkit { public static Charset guessEncoding(@NotNull File f, int bufferLength, @NotNull Charset defaultCharset) throws IOException { byte[] buffer = new byte[bufferLength]; int read; - FileInputStream fis = new FileInputStream(f); - try { + try (FileInputStream fis = new FileInputStream(f)) { read = fis.read(buffer); } - finally { - fis.close(); - } CharsetToolkit toolkit = new CharsetToolkit(buffer, defaultCharset); return toolkit.guessEncoding(read); } diff --git a/platform/util/src/com/intellij/util/ResourceUtil.java b/platform/util/src/com/intellij/util/ResourceUtil.java index 9af8dd677cbd..993cd3ab551a 100644 --- a/platform/util/src/com/intellij/util/ResourceUtil.java +++ b/platform/util/src/com/intellij/util/ResourceUtil.java @@ -114,8 +114,7 @@ public class ResourceUtil { public static String loadText(@NotNull URL url) throws IOException { InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url)); - InputStreamReader reader = new InputStreamReader(inputStream, CharsetToolkit.UTF8_CHARSET); - try { + try (InputStreamReader reader = new InputStreamReader(inputStream, CharsetToolkit.UTF8_CHARSET)) { StringBuilder text = new StringBuilder(); char[] buf = new char[5000]; while (reader.ready()) { @@ -125,8 +124,5 @@ public class ResourceUtil { } return text.toString(); } - finally { - reader.close(); - } } } diff --git a/platform/util/src/com/intellij/util/containers/ConcurrentBitSet.java b/platform/util/src/com/intellij/util/containers/ConcurrentBitSet.java index efee2a05acc2..d4237cee6c06 100644 --- a/platform/util/src/com/intellij/util/containers/ConcurrentBitSet.java +++ b/platform/util/src/com/intellij/util/containers/ConcurrentBitSet.java @@ -440,16 +440,12 @@ public class ConcurrentBitSet { } public void writeTo(@NotNull File file) throws IOException { - DataOutputStream bitSetStorage = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); - try { + try (DataOutputStream bitSetStorage = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) { long[] words = toLongArray(); for (long word : words) { bitSetStorage.writeLong(word); } } - finally { - bitSetStorage.close(); - } } @NotNull @@ -457,18 +453,14 @@ public class ConcurrentBitSet { if (!file.exists()) { return new ConcurrentBitSet(); } - DataInputStream bitSetStorage = new DataInputStream(new BufferedInputStream(new FileInputStream(file))); - try { + try (DataInputStream bitSetStorage = new DataInputStream(new BufferedInputStream(new FileInputStream(file)))) { long length = file.length(); - long[] words = new long[(int)(length/8)]; - for (int i=0; i Data read(final long addr, KeyDescriptor descriptor) throws IOException { - DataInputStream stream = getStream(addr); - try { + try (DataInputStream stream = getStream(addr)) { return descriptor.read(stream); - } finally { - stream.close(); } } @@ -112,14 +109,13 @@ public class CompressedAppendableFile { File chunkLengthFile = getChunkLengthFile(); if (chunkLengthFile.exists()) { - final DataInputStream chunkLengthStream = new DataInputStream(new BufferedInputStream( + try (DataInputStream chunkLengthStream = new DataInputStream(new BufferedInputStream( new LimitedInputStream(new FileInputStream(chunkLengthFile), (int)chunkLengthFile.length()) { @Override public int available() { return remainingLimit(); } - }, 32768)); - try{ + }, 32768))) { short[] chunkLengthTable = new short[(int)(chunkLengthFile.length() / 2)]; int chunkLengthTableLength = 0; @@ -139,28 +135,25 @@ public class CompressedAppendableFile { if (myChunkTableLength >= FACTOR) { long[] chunkOffsetTable = new long[myChunkTableLength / FACTOR]; long offset = 0; - for(int i = 0; i < chunkOffsetTable.length; ++i) { + for (int i = 0; i < chunkOffsetTable.length; ++i) { int start = i * FACTOR; - for(int j = 0; j < FACTOR; ++j) { + for (int j = 0; j < FACTOR; ++j) { offset += chunkLengthTable[start + j] & MAX_PAGE_LENGTH; } chunkOffsetTable[i] = offset; } myChunkOffsetTable = chunkOffsetTable; if (doDebug) { // check all offsets - for(int i = 0; i < chunkLengthTableLength; ++i) { + for (int i = 0; i < chunkLengthTableLength; ++i) { calcOffsetOfPage(i); } } - } else { + } + else { myChunkOffsetTable = ArrayUtil.EMPTY_LONG_ARRAY; } myFileLength = calcOffsetOfPage(myChunkTableLength - 1); - } finally { - try { - chunkLengthStream.close(); - } catch (IOException ignore) {} } } else { @@ -189,8 +182,7 @@ public class CompressedAppendableFile { if (myChunkLengthTable == null) initChunkLengthTable(); assert chunkNumber < myChunkTableLength; - final DataInputStream keysStream = getChunkStream(getChunksFile(), chunkNumber); - try { + try (DataInputStream keysStream = getChunkStream(getChunksFile(), chunkNumber)) { if (keysStream.available() > 0) { byte[] decompressedBytes = decompress(keysStream); if (decompressedBytes.length != myAppendBufferLength) { @@ -198,10 +190,6 @@ public class CompressedAppendableFile { } return decompressedBytes; } - } finally { - try { - keysStream.close(); - } catch (IOException ignore) {} } assert false:"data corruption detected:"+chunkNumber + "," + myChunkTableLength; @@ -306,17 +294,9 @@ public class CompressedAppendableFile { myBufferPosition = (int)tempAppendFile.length(); myNextChunkBuffer = new byte[calcBufferSize(myBufferPosition)]; - FileInputStream stream = new FileInputStream(tempAppendFile); - try { + try (FileInputStream stream = new FileInputStream(tempAppendFile)) { stream.read(myNextChunkBuffer, 0, myBufferPosition); } - finally { - try { - stream.close(); - } - catch (IOException ignore) { - } - } } else { myBufferPosition = 0; @@ -382,24 +362,13 @@ public class CompressedAppendableFile { } protected void saveChunk(BufferExposingByteArrayOutputStream compressedChunk, long endOfFileOffset) throws IOException { - DataOutputStream stream = - new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunksFile(), true))); - try { + try (DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunksFile(), true)))) { stream.write(compressedChunk.getInternalBuffer(), 0, compressedChunk.size()); - } finally { - try { - stream.close(); - } catch (IOException ignore) {} } - DataOutputStream chunkLengthStream = - new DataOutputStream(new BufferedOutputStream(new FileOutputStream(getChunkLengthFile(), true))); - try { + try (DataOutputStream chunkLengthStream = new DataOutputStream( + new BufferedOutputStream(new FileOutputStream(getChunkLengthFile(), true)))) { DataInputOutputUtil.writeINT(chunkLengthStream, compressedChunk.size()); - } finally { - try { - chunkLengthStream.close(); - } catch (IOException ignore) {} } } @@ -415,18 +384,9 @@ public class CompressedAppendableFile { try { saveNextChunkIfNeeded(); if (myBufferPosition != 0) { - BufferedOutputStream stream = - new BufferedOutputStream(new FileOutputStream(incompleteChunkFile)); - try { + try (BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(incompleteChunkFile))) { stream.write(myNextChunkBuffer, 0, myBufferPosition); } - finally { - try { - stream.close(); - } - catch (IOException ignore) { - } - } } else { incompleteChunkFile.delete(); } diff --git a/platform/util/src/com/intellij/util/io/Compressor.java b/platform/util/src/com/intellij/util/io/Compressor.java index 05e3e4691d7c..b0b9fc53e6ae 100644 --- a/platform/util/src/com/intellij/util/io/Compressor.java +++ b/platform/util/src/com/intellij/util/io/Compressor.java @@ -140,13 +140,9 @@ public abstract class Compressor implements Closeable { public final void addFile(@NotNull String entryName, @NotNull File file) throws IOException { entryName = entryName(entryName); if (accepts(entryName)) { - InputStream source = new FileInputStream(file); - try { + try (InputStream source = new FileInputStream(file)) { writeFileEntry(entryName, source, file.length(), file.lastModified()); } - finally { - source.close(); - } } } diff --git a/platform/util/src/com/intellij/util/io/Decompressor.java b/platform/util/src/com/intellij/util/io/Decompressor.java index 5d50387a4295..6862465fa05c 100644 --- a/platform/util/src/com/intellij/util/io/Decompressor.java +++ b/platform/util/src/com/intellij/util/io/Decompressor.java @@ -150,13 +150,9 @@ public abstract class Decompressor { InputStream inputStream = openEntryStream(entry); try { FileUtil.createParentDirs(outputFile); - FileOutputStream outputStream = new FileOutputStream(outputFile); - try { + try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { FileUtil.copy(inputStream, outputStream); } - finally { - outputStream.close(); - } } finally { closeEntryStream(inputStream); diff --git a/platform/util/src/com/intellij/util/io/PagedFileStorage.java b/platform/util/src/com/intellij/util/io/PagedFileStorage.java index 0f812f49b48b..7e73485da032 100644 --- a/platform/util/src/com/intellij/util/io/PagedFileStorage.java +++ b/platform/util/src/com/intellij/util/io/PagedFileStorage.java @@ -354,13 +354,9 @@ public class PagedFileStorage implements Forceable { private void resizeFile(long newSize) throws IOException { mySize = -1; - RandomAccessFile raf = new RandomAccessFile(myFile, RW); - try { + try (RandomAccessFile raf = new RandomAccessFile(myFile, RW)) { raf.setLength(newSize); } - finally { - raf.close(); - } mySize = newSize; } diff --git a/platform/util/src/com/intellij/util/io/PersistentHashMap.java b/platform/util/src/com/intellij/util/io/PersistentHashMap.java index 72205afd6ec5..91287e9babd5 100644 --- a/platform/util/src/com/intellij/util/io/PersistentHashMap.java +++ b/platform/util/src/com/intellij/util/io/PersistentHashMap.java @@ -602,14 +602,10 @@ public class PersistentHashMap extends PersistentEnumeratorDelegate< final PersistentHashMapValueStorage.ReadResult readResult = myValueStorage.readBytes(valueOffset); - DataInputStream input = new DataInputStream(new UnsyncByteArrayInputStream(readResult.buffer)); final Value valueRead; - try { + try (DataInputStream input = new DataInputStream(new UnsyncByteArrayInputStream(readResult.buffer))) { valueRead = myValueExternalizer.read(input); } - finally { - input.close(); - } if (myValueStorage.performChunksCompaction(readResult.chunksCount, readResult.buffer.length)) { long newValueOffset = myValueStorage.compactChunks(new ValueDataAppender() { diff --git a/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java b/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java index b5d1fe3bfcee..36591ab21353 100644 --- a/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java +++ b/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java @@ -31,18 +31,10 @@ public class ReadOnlyMappedBufferWrapper extends MappedBufferWrapper { @Override protected MappedByteBuffer map() throws IOException { - final FileInputStream stream = new FileInputStream(myFile); - try { - final FileChannel channel = stream.getChannel(); - try { + try (FileInputStream stream = new FileInputStream(myFile)) { + try (FileChannel channel = stream.getChannel()) { return channel.map(FileChannel.MapMode.READ_ONLY, myPosition, myLength); } - finally { - channel.close(); - } - } - finally { - stream.close(); } } } diff --git a/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java b/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java index 4f30be6632f4..37a4b4ef8529 100644 --- a/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java +++ b/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java @@ -35,18 +35,10 @@ public class ReadWriteMappedBufferWrapper extends MappedBufferWrapper { @Override protected MappedByteBuffer map() throws IOException { - final RandomAccessFile file = new RandomAccessFile(myFile, RW); - try { - final FileChannel channel = file.getChannel(); - try { + try (RandomAccessFile file = new RandomAccessFile(myFile, RW)) { + try (FileChannel channel = file.getChannel()) { return channel.map(FileChannel.MapMode.READ_WRITE, myPosition, myLength); } - finally { - channel.close(); - } - } - finally { - file.close(); } } } diff --git a/platform/util/src/com/intellij/util/io/SourceCodeCompressor.java b/platform/util/src/com/intellij/util/io/SourceCodeCompressor.java index cd458a3063e5..1041c4e0e149 100644 --- a/platform/util/src/com/intellij/util/io/SourceCodeCompressor.java +++ b/platform/util/src/com/intellij/util/io/SourceCodeCompressor.java @@ -65,16 +65,9 @@ public class SourceCodeCompressor { DEFLATER.reset(); DEFLATER.setDictionary(PRESET_BUF); try { - DeflaterOutputStream output = null; - try { - output = new DeflaterOutputStream(OUTPUT, DEFLATER); + try (DeflaterOutputStream output = new DeflaterOutputStream(OUTPUT, DEFLATER)) { output.write(source, off, len); } - finally { - if (output != null) { - output.close(); - } - } } catch (IOException e) { return source; @@ -99,9 +92,7 @@ public class SourceCodeCompressor { public static byte[] decompress(final byte[] compressed, final int len, final int off) throws IOException { INFLATER.reset(); - InflaterInputStream input = null; - try { - input = new InflaterInputStream(new ByteArrayInputStream(compressed, off, len), INFLATER); + try (InflaterInputStream input = new InflaterInputStream(new ByteArrayInputStream(compressed, off, len), INFLATER)) { final int b = input.read(); if (b == -1) { INFLATER.setDictionary(PRESET_BUF); @@ -116,9 +107,6 @@ public class SourceCodeCompressor { return OUTPUT.toByteArray(); } finally { - if (input != null) { - input.close(); - } OUTPUT.reset(); } } diff --git a/platform/util/src/com/intellij/util/io/URLUtil.java b/platform/util/src/com/intellij/util/io/URLUtil.java index 41bfecc06dd7..1decf1ec0464 100644 --- a/platform/util/src/com/intellij/util/io/URLUtil.java +++ b/platform/util/src/com/intellij/util/io/URLUtil.java @@ -117,13 +117,9 @@ public class URLUtil { return ThreeState.NO; } try { - ZipFile file = new ZipFile(paths.first); - try { + try (ZipFile file = new ZipFile(paths.first)) { return ThreeState.fromBoolean(file.getEntry(paths.second) != null); } - finally { - file.close(); - } } catch (IOException e) { return ThreeState.NO; diff --git a/platform/util/src/com/intellij/util/io/ZipUtil.java b/platform/util/src/com/intellij/util/io/ZipUtil.java index f5fd1311eece..542c8f244591 100644 --- a/platform/util/src/com/intellij/util/io/ZipUtil.java +++ b/platform/util/src/com/intellij/util/io/ZipUtil.java @@ -75,13 +75,9 @@ public class ZipUtil { } zos.putNextEntry(e); if (!isDir) { - InputStream is = contentProcessor.getContent(file); - try { + try (InputStream is = contentProcessor.getContent(file)) { FileUtilRt.copy(is, zos); } - finally { - is.close(); - } } zos.closeEntry(); return true; @@ -156,8 +152,7 @@ public class ZipUtil { @SuppressWarnings("unused") public static boolean isZipContainsFolder(File zip) throws IOException { - ZipFile zipFile = new ZipFile(zip); - try { + try (ZipFile zipFile = new ZipFile(zip)) { Enumeration en = zipFile.entries(); while (en.hasMoreElements()) { ZipEntry zipEntry = en.nextElement(); @@ -170,26 +165,15 @@ public class ZipUtil { } return false; } - finally { - zipFile.close(); - } } public static void compressFile(@NotNull File srcFile, @NotNull File zipFile) throws IOException { - InputStream is = new FileInputStream(srcFile); - try { - ZipOutputStream os = new ZipOutputStream(new FileOutputStream(zipFile)); - try { + try (InputStream is = new FileInputStream(srcFile)) { + try (ZipOutputStream os = new ZipOutputStream(new FileOutputStream(zipFile))) { os.putNextEntry(new ZipEntry(srcFile.getName())); FileUtilRt.copy(is, os); os.closeEntry(); } - finally { - os.close(); - } - } - finally { - is.close(); } } @@ -212,13 +196,9 @@ public class ZipUtil { } else if (!outputFile.exists() || overwrite) { FileUtil.createParentDirs(outputFile); - FileOutputStream os = new FileOutputStream(outputFile); - try { + try (FileOutputStream os = new FileOutputStream(outputFile)) { FileUtilRt.copy(inputStream, os); } - finally { - os.close(); - } } } finally { diff --git a/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java b/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java index f5edc7ec66a6..358bd15fe112 100644 --- a/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java +++ b/platform/util/src/com/intellij/util/io/storage/RefCountingStorage.java @@ -87,15 +87,11 @@ public class RefCountingStorage extends AbstractStorage { result = super.readBytes(record); } - InflaterInputStream in = new CustomInflaterInputStream(result); - try { + try (InflaterInputStream in = new CustomInflaterInputStream(result)) { final BufferExposingByteArrayOutputStream outputStream = new BufferExposingByteArrayOutputStream(); StreamUtil.copyStreamContent(in, outputStream); return outputStream; } - finally { - in.close(); - } } private static class CustomInflaterInputStream extends InflaterInputStream { @@ -165,13 +161,9 @@ public class RefCountingStorage extends AbstractStorage { private void zipAndWrite(ByteArraySequence bytes, int record, boolean fixedSize) throws IOException { BufferExposingByteArrayOutputStream s = new BufferExposingByteArrayOutputStream(); - DeflaterOutputStream out = new DeflaterOutputStream(s); - try { + try (DeflaterOutputStream out = new DeflaterOutputStream(s)) { out.write(bytes.getBytes(), bytes.getOffset(), bytes.getLength()); } - finally { - out.close(); - } synchronized (myLock) { doWrite(record, fixedSize, s); diff --git a/platform/util/src/com/intellij/util/io/zip/JBZipEntry.java b/platform/util/src/com/intellij/util/io/zip/JBZipEntry.java index 3a66f9cc67b8..7d342fdd849f 100644 --- a/platform/util/src/com/intellij/util/io/zip/JBZipEntry.java +++ b/platform/util/src/com/intellij/util/io/zip/JBZipEntry.java @@ -451,13 +451,9 @@ public class JBZipEntry implements Cloneable { } void doSetDataFromFile(File file) throws IOException { - InputStream input = new BufferedInputStream(new FileInputStream(file)); - try { + try (InputStream input = new BufferedInputStream(new FileInputStream(file))) { myFile.getOutputStream().putNextEntryContent(this, file.length(), input); } - finally { - input.close(); - } } public void writeDataTo(OutputStream output) throws IOException { @@ -470,13 +466,9 @@ public class JBZipEntry implements Cloneable { public byte[] getData() throws IOException { if (size == -1) throw new IOException("no data"); - final InputStream stream = getInputStream(); - try { + try (InputStream stream = getInputStream()) { return FileUtil.loadBytes(stream, (int)size); } - finally { - stream.close(); - } } private long calcDataOffset() throws IOException { diff --git a/platform/util/src/com/intellij/util/io/zip/JBZipOutputStream.java b/platform/util/src/com/intellij/util/io/zip/JBZipOutputStream.java index 9849c645af43..80515172994a 100644 --- a/platform/util/src/com/intellij/util/io/zip/JBZipOutputStream.java +++ b/platform/util/src/com/intellij/util/io/zip/JBZipOutputStream.java @@ -380,13 +380,9 @@ class JBZipOutputStream { if (entry.getMethod() == ZipEntry.DEFLATED) { def.setLevel(level); final BufferExposingByteArrayOutputStream compressedBytesStream = new BufferExposingByteArrayOutputStream(); - final DeflaterOutputStream stream = new DeflaterOutputStream(compressedBytesStream, def); - try { + try (DeflaterOutputStream stream = new DeflaterOutputStream(compressedBytesStream, def)) { stream.write(bytes); } - finally { - stream.close(); - } outputBytesLength = compressedBytesStream.size(); outputBytes = compressedBytesStream.getInternalBuffer(); } diff --git a/platform/util/src/com/intellij/util/ui/accessibility/ScreenReader.java b/platform/util/src/com/intellij/util/ui/accessibility/ScreenReader.java index a7e72f8d4b76..be53c8096396 100644 --- a/platform/util/src/com/intellij/util/ui/accessibility/ScreenReader.java +++ b/platform/util/src/com/intellij/util/ui/accessibility/ScreenReader.java @@ -73,11 +73,8 @@ public class ScreenReader { Properties properties = new Properties(); for (String path : paths) { try { - FileInputStream in = new FileInputStream(new File(path)); - try { + try (FileInputStream in = new FileInputStream(new File(path))) { properties.load(in); - } finally { - in.close(); } } catch (Exception ignore) {