Skip to content

Commit

Permalink
Merge pull request #1 from spauwels/master
Browse files Browse the repository at this point in the history
Close streams with Closer instead of with Closeables.
  • Loading branch information
wimsymons committed Dec 10, 2014
2 parents d95d27d + 6eb6217 commit e0dbf18
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/main/be/wimsymons/intellij/polopolyimport/PPImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,7 +50,8 @@ class PPImporter {
private int totalCount = 0;
private int skippedCount = 0;

public PPImporter(final ProgressIndicator progressIndicator, final Target target, final List<String> includeExtensions, final List<Replacement> replacements, final boolean makeJar) {
public PPImporter(final ProgressIndicator progressIndicator, final Target target, final List<String> includeExtensions,
final List<Replacement> replacements, final boolean makeJar) {
this.progressIndicator = progressIndicator;
this.target = target;
this.includeExtensions = includeExtensions;
Expand Down Expand Up @@ -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);

Expand All @@ -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()) {
Expand All @@ -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();
Expand All @@ -208,24 +212,30 @@ 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();

int responseCode = httpConnection.getResponseCode();
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++;
}
} catch (IOException e) {
failureCount++;
PPImportPlugin.doNotify("Import of " + name + " failed: " + e.getMessage(), NotificationType.ERROR);
} finally {
Closeables.closeQuietly(reader);
Closeables.closeQuietly(writer);
closer.close();
}
}

Expand All @@ -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<VirtualFile> sortedDeduplicatedFiles = new TreeSet<VirtualFile>(new Comparator<VirtualFile>() {
@Override
public int compare(VirtualFile f1, VirtualFile f2) {
return f1.getName().compareTo(f2.getName());
}
});
TreeSet<VirtualFile> sortedDeduplicatedFiles = new TreeSet<VirtualFile>(
new Comparator<VirtualFile>() {
@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) {
Expand All @@ -262,12 +273,13 @@ public int compare(VirtualFile f1, VirtualFile f2) {
private Collection<VirtualFile> getFileList(@NotNull VirtualFile dir) {
LOGGER.info("Getting file list ...");
final Collection<VirtualFile> result = new LinkedHashSet<VirtualFile>();
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;
}

Expand Down

0 comments on commit e0dbf18

Please sign in to comment.