543815: Mylyn should use buffered output streams

Buffered output streams and replaced try-finally with try resource

Change-Id: I84140a8cbf6b88ab02fdf1e90b5ca23a31a3d619
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=543815
Signed-off-by: Natasha Carson <natasha.carson@tasktop.com>
diff --git a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/internal/commons/core/ZipFileUtil.java b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/internal/commons/core/ZipFileUtil.java
index c6cdadc..1a89e3d 100644
--- a/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/internal/commons/core/ZipFileUtil.java
+++ b/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/internal/commons/core/ZipFileUtil.java
@@ -35,7 +35,7 @@
 
 /**
  * Contains utility methods for working with zip files
- * 
+ *
  * @author Wesley Coelho
  * @author Shawn Minto (Wrote methods that were moved here)
  */
@@ -43,7 +43,7 @@
 
 	/**
 	 * Only unzips files in zip file not directories
-	 * 
+	 *
 	 * @param zipped
 	 *            file
 	 * @param destPath
@@ -52,8 +52,7 @@
 	 */
 	public static List<File> unzipFiles(File zippedfile, String destPath, IProgressMonitor monitor)
 			throws FileNotFoundException, IOException {
-		ZipFile zipFile = new ZipFile(zippedfile);
-		try {
+		try (ZipFile zipFile = new ZipFile(zippedfile)) {
 			Enumeration<? extends ZipEntry> entries = zipFile.entries();
 			List<File> outputFiles = new ArrayList<File>();
 			File destinationFile = new File(destPath);
@@ -72,16 +71,9 @@
 					outputFile.getParentFile().mkdirs();
 				}
 
-				InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
-				try {
-					OutputStream outStream = new BufferedOutputStream(new FileOutputStream(outputFile));
-					try {
-						copyStream(inputStream, outStream);
-					} finally {
-						outStream.close();
-					}
-				} finally {
-					inputStream.close();
+				try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
+						OutputStream outStream = new BufferedOutputStream(new FileOutputStream(outputFile))) {
+					copyStream(inputStream, outStream);
 				}
 
 				outputFiles.add(outputFile);
@@ -90,8 +82,6 @@
 				}
 			}
 			return outputFiles;
-		} finally {
-			zipFile.close();
 		}
 	}
 
@@ -140,8 +130,7 @@
 			rootPath += "/"; //$NON-NLS-1$
 		}
 
-		ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
-		try {
+		try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)))) {
 			for (File file : files) {
 				try {
 					addZipEntry(zipOut, rootPath, file);
@@ -153,16 +142,14 @@
 							+ file.getName() + " to zip", e)); //$NON-NLS-1$
 				}
 			}
-		} finally {
-			zipOut.close();
 		}
 	}
 
 	/**
 	 * @author Shawn Minto
 	 */
-	private static void addZipEntry(ZipOutputStream zipOut, String rootPath, File file) throws FileNotFoundException,
-			IOException {
+	private static void addZipEntry(ZipOutputStream zipOut, String rootPath, File file)
+			throws FileNotFoundException, IOException {
 		if (file.exists()) {
 			if (file.isDirectory()) {
 				for (File child : file.listFiles()) {
diff --git a/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/CommonTestUtil.java b/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/CommonTestUtil.java
index 178dbf9..129befd 100644
--- a/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/CommonTestUtil.java
+++ b/org.eclipse.mylyn.commons.sdk.util/src/org/eclipse/mylyn/commons/sdk/util/CommonTestUtil.java
@@ -13,6 +13,7 @@
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -38,8 +39,6 @@
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
-import junit.framework.AssertionFailedError;
-
 import org.apache.commons.lang.reflect.MethodUtils;
 import org.eclipse.core.runtime.FileLocator;
 import org.eclipse.core.runtime.IPath;
@@ -55,6 +54,8 @@
 import org.eclipse.osgi.util.NLS;
 import org.osgi.framework.Bundle;
 
+import junit.framework.AssertionFailedError;
+
 /**
  * @author Steffen Pingel
  */
@@ -92,16 +93,9 @@
 	 * Copies the given source file to the given destination file.
 	 */
 	public static void copy(File source, File dest) throws IOException {
-		InputStream in = new FileInputStream(source);
-		try {
-			OutputStream out = new FileOutputStream(dest);
-			try {
-				transferData(in, out);
-			} finally {
-				out.close();
-			}
-		} finally {
-			in.close();
+		try (InputStream in = new FileInputStream(source);
+				OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) {
+			transferData(in, out);
 		}
 	}
 
@@ -274,8 +268,8 @@
 					return checkNotNull(getFileFromClassLoaderBeforeLuna(filename, classLoader));
 				}
 			} catch (Exception e) {
-				AssertionFailedError exception = new AssertionFailedError(NLS.bind(
-						"Could not locate {0} using classloader for {1}", filename, clazz));
+				AssertionFailedError exception = new AssertionFailedError(
+						NLS.bind("Could not locate {0} using classloader for {1}", filename, clazz));
 				exception.initCause(e);
 				throw exception;
 			}
@@ -457,27 +451,9 @@
 				String entryName = entry.getName();
 				File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
 				file.getParentFile().mkdirs();
-				InputStream src = null;
-				OutputStream dst = null;
-				try {
-					src = zipFile.getInputStream(entry);
-					dst = new FileOutputStream(file);
+				try (InputStream src = zipFile.getInputStream(entry);
+						OutputStream dst = new BufferedOutputStream(new FileOutputStream(file))) {
 					transferData(src, dst);
-				} finally {
-					if (dst != null) {
-						try {
-							dst.close();
-						} catch (IOException e) {
-							// don't need to catch this
-						}
-					}
-					if (src != null) {
-						try {
-							src.close();
-						} catch (IOException e) {
-							// don't need to catch this
-						}
-					}
 				}
 			}
 		} finally {
diff --git a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/support/CommonTestUtil.java b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/support/CommonTestUtil.java
index 6c66dfd..a0f9888 100644
--- a/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/support/CommonTestUtil.java
+++ b/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/support/CommonTestUtil.java
@@ -11,6 +11,7 @@
 
 package org.eclipse.mylyn.commons.tests.support;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -38,7 +39,7 @@
 	/**
 	 * Returns the given file path with its separator character changed from the given old separator to the given new
 	 * separator.
-	 * 
+	 *
 	 * @param path
 	 *            a file path
 	 * @param oldSeparator
@@ -56,16 +57,8 @@
 	 * Copies the given source file to the given destination file.
 	 */
 	public static void copy(File source, File dest) throws IOException {
-		InputStream in = new FileInputStream(source);
-		try {
-			OutputStream out = new FileOutputStream(dest);
-			try {
+		try (InputStream in = new FileInputStream(source); OutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) {
 				transferData(in, out);
-			} finally {
-				out.close();
-			}
-		} finally {
-			in.close();
 		}
 	}
 
@@ -81,8 +74,8 @@
 				File destDir = new File(targetFolder, currFile.getName());
 				if (!destDir.exists()) {
 					if (!destDir.mkdir()) {
-						throw new IOException("Unable to create destination context folder: "
-								+ destDir.getAbsolutePath());
+						throw new IOException(
+								"Unable to create destination context folder: " + destDir.getAbsolutePath());
 					}
 				}
 				for (File file : currFile.listFiles()) {
@@ -178,7 +171,7 @@
 
 	/**
 	 * Copies all bytes in the given source stream to the given destination stream. Neither streams are closed.
-	 * 
+	 *
 	 * @param source
 	 *            the given source stream
 	 * @param destination
@@ -197,7 +190,7 @@
 	/**
 	 * Unzips the given zip file to the given destination directory extracting only those entries the pass through the
 	 * given filter.
-	 * 
+	 *
 	 * @param zipFile
 	 *            the zip file to unzip
 	 * @param dstDir
@@ -222,27 +215,9 @@
 				String entryName = entry.getName();
 				File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
 				file.getParentFile().mkdirs();
-				InputStream src = null;
-				OutputStream dst = null;
-				try {
-					src = zipFile.getInputStream(entry);
-					dst = new FileOutputStream(file);
+				try (InputStream src = zipFile.getInputStream(entry);
+						OutputStream dst = new BufferedOutputStream(new FileOutputStream(file))) {
 					transferData(src, dst);
-				} finally {
-					if (dst != null) {
-						try {
-							dst.close();
-						} catch (IOException e) {
-							// don't need to catch this
-						}
-					}
-					if (src != null) {
-						try {
-							src.close();
-						} catch (IOException e) {
-							// don't need to catch this
-						}
-					}
 				}
 			}
 		} finally {
diff --git a/org.eclipse.mylyn.discovery.core/src/org/eclipse/mylyn/internal/discovery/core/util/WebUtil.java b/org.eclipse.mylyn.discovery.core/src/org/eclipse/mylyn/internal/discovery/core/util/WebUtil.java
index c05845d..f39a539 100644
--- a/org.eclipse.mylyn.discovery.core/src/org/eclipse/mylyn/internal/discovery/core/util/WebUtil.java
+++ b/org.eclipse.mylyn.discovery.core/src/org/eclipse/mylyn/internal/discovery/core/util/WebUtil.java
@@ -64,11 +64,8 @@
 	 */

 	public static IStatus download(URI uri, File target, IProgressMonitor monitor) throws IOException {

 		IStatus result;

-		OutputStream out = new BufferedOutputStream(new FileOutputStream(target));

-		try {

+		try (OutputStream out = new BufferedOutputStream(new FileOutputStream(target))) {

 			result = download(uri, out, monitor);

-		} finally {

-			out.close();

 		}

 		if (!result.isOK()) {

 			target.delete();