Bug 559070 - Fix resource related compiler warnings in platform.debug

Change-Id: If9102e5fcd24e7888596fd33a14ed5529419acc2
Signed-off-by: Paul Pazderski <paul-eclipse@ppazderski.de>
diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/core/sourcelookup/containers/ZipEntryStorage.java b/org.eclipse.debug.core/core/org/eclipse/debug/core/sourcelookup/containers/ZipEntryStorage.java
index 2bf9eee..5ffe0fc 100644
--- a/org.eclipse.debug.core/core/org/eclipse/debug/core/sourcelookup/containers/ZipEntryStorage.java
+++ b/org.eclipse.debug.core/core/org/eclipse/debug/core/sourcelookup/containers/ZipEntryStorage.java
@@ -38,6 +38,7 @@
  * @since 3.0
  * @noextend This class is not intended to be subclassed by clients.
  */
+@SuppressWarnings("resource")
 public class ZipEntryStorage extends PlatformObject implements IStorage {
 
 	/**
diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/NullStreamsProxy.java b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/NullStreamsProxy.java
index 9a399e2..4ac8054 100644
--- a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/NullStreamsProxy.java
+++ b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/NullStreamsProxy.java
@@ -24,6 +24,7 @@
 	private NullStreamMonitor outputStreamMonitor;
 	private NullStreamMonitor errorStreamMonitor;
 
+	@SuppressWarnings("resource")
 	public NullStreamsProxy(Process process) {
 		outputStreamMonitor = new NullStreamMonitor(process.getInputStream());
 		errorStreamMonitor = new NullStreamMonitor(process.getErrorStream());
diff --git a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/StreamsProxy.java b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/StreamsProxy.java
index 823757f..54fc4c9 100644
--- a/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/StreamsProxy.java
+++ b/org.eclipse.debug.core/core/org/eclipse/debug/internal/core/StreamsProxy.java
@@ -52,6 +52,7 @@
 	 * @param process system process to create a streams proxy on
 	 * @param charset the process's charset or <code>null</code> if default
 	 */
+	@SuppressWarnings("resource")
 	public StreamsProxy(Process process, Charset charset) {
 		if (process == null) {
 			return;
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTestUtil.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTestUtil.java
index ce67942..526d263 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTestUtil.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTestUtil.java
@@ -122,6 +122,7 @@
 	 * @return this {@link IOConsoleTestUtil} to chain methods
 	 * @see #write(String)
 	 */
+	@SuppressWarnings("resource")
 	public IOConsoleTestUtil writeFast(final String s) throws IOException {
 		return writeFast(s, getDefaultOutputStream());
 	}
@@ -134,6 +135,7 @@
 	 * @return this {@link IOConsoleTestUtil} to chain methods
 	 * @see #writeFast(String)
 	 */
+	@SuppressWarnings("resource")
 	public IOConsoleTestUtil write(final String s) throws Exception {
 		return write(s, getDefaultOutputStream());
 	}
@@ -149,6 +151,7 @@
 	 * @param s content to write in output stream
 	 * @return this {@link IOConsoleTestUtil} to chain methods
 	 */
+	@SuppressWarnings("resource")
 	public IOConsoleTestUtil writeAndVerify(final String s) throws Exception {
 		return writeAndVerify(s, getDefaultOutputStream());
 	}
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTests.java
index 88fa402..1277b81 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/IOConsoleTests.java
@@ -274,14 +274,15 @@
 		final IOConsoleTestUtil c = getTestUtil("Test input file");
 		// open default output stream to match usual behavior where two output
 		// streams are open and to prevent premature console closing
-		c.getDefaultOutputStream();
-		try (InputStream in = new ByteArrayInputStream(new byte[0])) {
-			try (InputStream defaultIn = c.getConsole().getInputStream()) {
-				// just close input stream
+		try (IOConsoleOutputStream defaultOutputStream = c.getDefaultOutputStream()) {
+			try (InputStream in = new ByteArrayInputStream(new byte[0])) {
+				try (InputStream defaultIn = c.getConsole().getInputStream()) {
+					// just close input stream
+				}
+				c.getConsole().setInputStream(in);
 			}
-			c.getConsole().setInputStream(in);
+			closeConsole(c);
 		}
-		closeConsole(c);
 		assertEquals("Test triggered errors in IOConsole.", 0, loggedErrors.get());
 	}
 
@@ -582,21 +583,23 @@
 	 */
 	public void testTrim() throws Exception {
 		final IOConsoleTestUtil c = getTestUtil("Test trim");
-		try (IOConsoleOutputStream otherOut = c.getConsole().newOutputStream()) {
-			c.writeFast("first\n");
-			for (int i = 0; i < 20; i++) {
-				c.writeFast("0123456789\n", (i & 1) == 0 ? c.getDefaultOutputStream() : otherOut);
-			}
-			c.write("last\n");
-			c.verifyContentByLine("first", 0).verifyContentByLine("last", -2);
-			assertTrue("Document not filled.", c.getDocument().getNumberOfLines() > 15);
+		try (IOConsoleOutputStream defaultOut = c.getDefaultOutputStream()) {
+			try (IOConsoleOutputStream otherOut = c.getConsole().newOutputStream()) {
+				c.writeFast("first\n");
+				for (int i = 0; i < 20; i++) {
+					c.writeFast("0123456789\n", (i & 1) == 0 ? defaultOut : otherOut);
+				}
+				c.write("last\n");
+				c.verifyContentByLine("first", 0).verifyContentByLine("last", -2);
+				assertTrue("Document not filled.", c.getDocument().getNumberOfLines() > 15);
 
-			c.getConsole().setWaterMarks(50, 100);
-			c.waitForScheduledJobs();
-			c.verifyContentByOffset("0123456789", 0);
-			assertTrue("Document not trimmed.", c.getDocument().getNumberOfLines() < 15);
+				c.getConsole().setWaterMarks(50, 100);
+				c.waitForScheduledJobs();
+				c.verifyContentByOffset("0123456789", 0);
+				assertTrue("Document not trimmed.", c.getDocument().getNumberOfLines() < 15);
+			}
+			closeConsole(c);
 		}
-		closeConsole(c);
 	}
 
 	/**
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleTests.java
index 10ff244..3f7eb9f 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ProcessConsoleTests.java
@@ -44,6 +44,7 @@
 import org.eclipse.ui.console.IConsole;
 import org.eclipse.ui.console.IConsoleConstants;
 import org.eclipse.ui.console.IConsoleManager;
+import org.eclipse.ui.console.IOConsoleInputStream;
 
 /**
  * Tests the ProcessConsole.
@@ -139,7 +140,9 @@
 			final org.eclipse.debug.internal.ui.views.console.ProcessConsole console = new org.eclipse.debug.internal.ui.views.console.ProcessConsole(process, new ConsoleColorProvider(), StandardCharsets.UTF_8.toString());
 			try {
 				console.initialize();
-				console.getInputStream().appendData(input);
+				@SuppressWarnings("resource")
+				IOConsoleInputStream consoleIn = console.getInputStream();
+				consoleIn.appendData(input);
 				mockProcess.waitFor(testTimeout, TimeUnit.MILLISECONDS);
 			} finally {
 				console.destroy();
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchConfigurationTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchConfigurationTests.java
index e35e9e7..c5797cf 100644
--- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchConfigurationTests.java
+++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchConfigurationTests.java
@@ -1267,8 +1267,11 @@
 		c2.doSave();
 
 		// file contents should be the same
-		char[] chars1 = getInputStreamAsCharArray(c1.getFile().getContents());
-		char[] chars2 = getInputStreamAsCharArray(c2.getFile().getContents());
+		char[] chars1, chars2;
+		try (InputStream in1 = c1.getFile().getContents(); InputStream in2 = c2.getFile().getContents()) {
+			chars1 = getInputStreamAsCharArray(in1);
+			chars2 = getInputStreamAsCharArray(in2);
+		}
 		assertEquals("Should be the same characters", chars1.length, chars2.length); //$NON-NLS-1$
 		for (int i = 0; i < chars2.length; i++) {
 			assertEquals("Should be the same character", chars1[i], chars2[i]); //$NON-NLS-1$
@@ -1345,8 +1348,11 @@
 		c2.doSave();
 
 		// file contents should be the same
-		char[] chars1 = getInputStreamAsCharArray(c1.getFile().getContents());
-		char[] chars2 = getInputStreamAsCharArray(c2.getFile().getContents());
+		char[] chars1, chars2;
+		try (InputStream in1 = c1.getFile().getContents(); InputStream in2 = c2.getFile().getContents()) {
+			chars1 = getInputStreamAsCharArray(in1);
+			chars2 = getInputStreamAsCharArray(in2);
+		}
 		assertEquals("Should be the same characters", chars1.length, chars2.length); //$NON-NLS-1$
 		for (int i = 0; i < chars2.length; i++) {
 			assertEquals("Should be the same character", chars1[i], chars2[i]); //$NON-NLS-1$
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartition.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartition.java
index 34ffa88..48af576 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartition.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartition.java
@@ -236,6 +236,26 @@
 		return inputStream;
 	}
 
+	/**
+	 * Test if this partition belongs to the given input stream.
+	 *
+	 * @param in the input stream to test or <code>null</code>
+	 * @return <code>true</code> if this partition belongs to input stream
+	 */
+	boolean belongsTo(IOConsoleInputStream in) {
+		return inputStream == in;
+	}
+
+	/**
+	 * Test if this partition belongs to the given output stream.
+	 *
+	 * @param out the output stream to test or <code>null</code>
+	 * @return <code>true</code> if this partition belongs to output stream
+	 */
+	boolean belongsTo(IOConsoleOutputStream out) {
+		return outputStream == out;
+	}
+
 	@Override
 	public String toString() {
 		final StringBuilder sb = new StringBuilder(40);
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
index 3102a3a..f6df185 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
@@ -643,6 +643,7 @@
 	 *               and a new partition will start
 	 * @return the newly created partition (i.e. the right side of the split)
 	 */
+	@SuppressWarnings("resource") // suppress wrong 'not closed' warnings
 	private IOConsolePartition splitPartition(int offset) {
 		final int partitionIndex = findPartitionCandidate(offset);
 		final IOConsolePartition existingPartition = partitions.get(partitionIndex);
@@ -861,7 +862,7 @@
 								Assert.isTrue(atOutputPartitionIndex == findPartitionCandidate(outputOffset - 1));
 							}
 						}
-						if (atOutputPartition == null || atOutputPartition.getOutputStream() != pending.stream) {
+						if (atOutputPartition == null || !atOutputPartition.belongsTo(pending.stream)) {
 							// no partitions yet or last partition is incompatible to reuse -> add new one
 							atOutputPartition = new IOConsolePartition(outputOffset, pending.stream);
 							partitions.add(atOutputPartition);
@@ -904,7 +905,7 @@
 								atOutputPartition.getLength() - (outputOffset - atOutputPartition.getOffset()));
 						Assert.isTrue(chunkLength > 0); // do not remove since it can prevent an infinity loop
 
-						if (atOutputPartition.getOutputStream() != pending.stream) {
+						if (!atOutputPartition.belongsTo(pending.stream)) {
 							// new output is from other stream then overwritten output
 
 							// Note: this implementation ignores the possibility to reuse the partition
@@ -925,7 +926,7 @@
 								atOutputPartition = splitPartition(outputOffset);
 								atOutputPartitionIndex++;
 							}
-							if (outputPartition == null || outputPartition.getOutputStream() != pending.stream) {
+							if (outputPartition == null || !outputPartition.belongsTo(pending.stream)) {
 								outputPartition = new IOConsolePartition(outputOffset, pending.stream);
 								partitions.add(atOutputPartitionIndex, outputPartition);
 								atOutputPartitionIndex++;