From 6eb62172c5d9d555ce2cf15310b66441e31fc267 Mon Sep 17 00:00:00 2001 From: PAUWELS Stefan Date: Wed, 10 Dec 2014 09:05:52 +0100 Subject: [PATCH] refactor for http://docs.guava-libraries.googlecode.com/git-history/v15.0/javadoc/com/google/common/io/Closeables.html --- .../intellij/polopolyimport/PPImporter.java | 64 +++++++++++-------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/main/be/wimsymons/intellij/polopolyimport/PPImporter.java b/src/main/be/wimsymons/intellij/polopolyimport/PPImporter.java index 8f3a01b..39fea50 100644 --- a/src/main/be/wimsymons/intellij/polopolyimport/PPImporter.java +++ b/src/main/be/wimsymons/intellij/polopolyimport/PPImporter.java @@ -4,6 +4,7 @@ import com.google.common.base.Strings; import com.google.common.io.CharStreams; import com.google.common.io.Closeables; +import com.google.common.io.Closer; import com.intellij.notification.NotificationType; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.progress.ProgressIndicator; @@ -49,7 +50,8 @@ class PPImporter { private int totalCount = 0; private int skippedCount = 0; - public PPImporter(final ProgressIndicator progressIndicator, final Target target, final List includeExtensions, final List replacements, final boolean makeJar) { + public PPImporter(final ProgressIndicator progressIndicator, final Target target, final List includeExtensions, + final List replacements, final boolean makeJar) { this.progressIndicator = progressIndicator; this.target = target; this.includeExtensions = includeExtensions; @@ -139,6 +141,7 @@ private void doImportMultiple(final VirtualFile[] virtualFiles) { private byte[] makeJar(VirtualFile[] files) throws IOException { JarOutputStream jarOS = null; ByteArrayOutputStream byteOS = null; + Closer closer = Closer.create(); try { progressIndicator.setIndeterminate(true); @@ -153,8 +156,8 @@ private byte[] makeJar(VirtualFile[] files) throws IOException { totalCount = filesToProcess.size(); - byteOS = new ByteArrayOutputStream(); - jarOS = new JarOutputStream(byteOS); + byteOS = closer.register(new ByteArrayOutputStream()); + jarOS = closer.register(new JarOutputStream(byteOS)); int counter = 0; for (VirtualFile file : filesToProcess) { if (progressIndicator.isCanceled()) { @@ -175,29 +178,30 @@ private byte[] makeJar(VirtualFile[] files) throws IOException { jarOS.flush(); return progressIndicator.isCanceled() ? null : byteOS.toByteArray(); } finally { - Closeables.closeQuietly(jarOS); - Closeables.closeQuietly(byteOS); + closer.close(); } } private void addToJar(JarOutputStream jarOS, VirtualFile file) throws IOException { + Closer closer = Closer.create(); JarEntry entry = new JarEntry(file.getCanonicalPath()); entry.setTime(file.getTimeStamp()); jarOS.putNextEntry(entry); - Reader reader = wrapWithReplacements(file.getInputStream()); - Writer writer = new OutputStreamWriter(jarOS); try { + Reader reader = closer.register(wrapWithReplacements(file.getInputStream())); + Writer writer = closer.register(new OutputStreamWriter(jarOS)); CharStreams.copy(reader, writer); } finally { - Closeables.closeQuietly(reader); - Closeables.closeQuietly(writer); + closer.close(); } } - private void postData(final String name, final Reader reader, final String contentType, final String extraParams) { + private void postData(final String name, final Reader reader, final String contentType, final String extraParams) throws IOException { Writer writer = null; LOGGER.info("Doing HTTP POST for " + name); + Closer closer = Closer.create(); try { + closer.register(reader); URL httpURL = buildURL(target, extraParams); HttpURLConnection httpConnection = (HttpURLConnection) httpURL.openConnection(); @@ -208,7 +212,7 @@ private void postData(final String name, final Reader reader, final String conte httpConnection.setReadTimeout(60000); httpConnection.connect(); - writer = new OutputStreamWriter(httpConnection.getOutputStream()); + writer = closer.register(new OutputStreamWriter(httpConnection.getOutputStream())); CharStreams.copy(reader, writer); writer.flush(); @@ -216,7 +220,14 @@ private void postData(final String name, final Reader reader, final String conte String responseMessage = httpConnection.getResponseMessage(); if (responseCode < 200 || responseCode >= 300) { failureCount++; - PPImportPlugin.doNotify("Import of " + name + " failed: " + responseCode + " - " + responseMessage + "\nCheck the server log for more details.", NotificationType.ERROR); + PPImportPlugin.doNotify( + "Import of " + + name + + " failed: " + + responseCode + + " - " + + responseMessage + + "\nCheck the server log for more details.", NotificationType.ERROR); } else { successCount++; } @@ -224,8 +235,7 @@ private void postData(final String name, final Reader reader, final String conte failureCount++; PPImportPlugin.doNotify("Import of " + name + " failed: " + e.getMessage(), NotificationType.ERROR); } finally { - Closeables.closeQuietly(reader); - Closeables.closeQuietly(writer); + closer.close(); } } @@ -243,12 +253,13 @@ private URL buildURL(Target target, String extraParams) throws MalformedURLExcep @SuppressWarnings("UnsafeVfsRecursion") private void recurseFiles(@NotNull VirtualFile virtualFile, @NotNull ContentIterator iterator) { - TreeSet sortedDeduplicatedFiles = new TreeSet(new Comparator() { - @Override - public int compare(VirtualFile f1, VirtualFile f2) { - return f1.getName().compareTo(f2.getName()); - } - }); + TreeSet sortedDeduplicatedFiles = new TreeSet( + new Comparator() { + @Override + public int compare(VirtualFile f1, VirtualFile f2) { + return f1.getName().compareTo(f2.getName()); + } + }); if (virtualFile.isDirectory()) { Collections.addAll(sortedDeduplicatedFiles, virtualFile.getChildren()); for (VirtualFile child : sortedDeduplicatedFiles) { @@ -262,12 +273,13 @@ public int compare(VirtualFile f1, VirtualFile f2) { private Collection getFileList(@NotNull VirtualFile dir) { LOGGER.info("Getting file list ..."); final Collection result = new LinkedHashSet(); - recurseFiles(dir, new ContentIterator() { - @Override - public boolean processFile(VirtualFile virtualFile) { - return result.add(virtualFile); - } - }); + recurseFiles( + dir, new ContentIterator() { + @Override + public boolean processFile(VirtualFile virtualFile) { + return result.add(virtualFile); + } + }); return result; }