Bug 576399: Use the builtin APIs to copy into byte[] from files

Change-Id: I8ec9675e3c56ee2811e929432a3982ef1a4cc4c6
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.debug/+/186062
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Andrey Loskutov <loskutov@gmx.de>
diff --git a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaStackTraceConsole.java b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaStackTraceConsole.java
index 5b80944..fd6bbc9 100644
--- a/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaStackTraceConsole.java
+++ b/org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/console/JavaStackTraceConsole.java
@@ -16,12 +16,10 @@
  *******************************************************************************/
 package org.eclipse.jdt.internal.debug.ui.console;
 
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -127,19 +125,9 @@
 	public void initializeDocument() {
         File file = new File(FILE_NAME);
         if (file.exists()) {
-			try (InputStream fin = new BufferedInputStream(new FileInputStream(file))) {
-                int fileLength = (int) file.length();
-                byte[] fileContent = new byte[fileLength];
-                int bufIndex = 0;
-				int read = 0;
-				while (bufIndex < fileContent.length) {
-					read = fin.read(fileContent, bufIndex, fileContent.length - bufIndex);
-					if (read < 0) {
-						break;
-					}
-					bufIndex += read;
-                }
-				getDocument().set(new String(fileContent, 0, bufIndex));
+			try {
+				byte[] fileContent = Files.readAllBytes(file.toPath());
+				getDocument().set(new String(fileContent));
             } catch (IOException e) {
 				getDocument().set(NLS.bind(ConsoleMessages.JavaStackTraceConsole_2, e.getMessage()));
             }
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaAppletLaunchConfigurationDelegate.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaAppletLaunchConfigurationDelegate.java
index c9137b8..09837d8 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaAppletLaunchConfigurationDelegate.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaAppletLaunchConfigurationDelegate.java
@@ -14,25 +14,21 @@
 package org.eclipse.jdt.internal.launching;
 
 
-import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.Path;
-
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
-
 import org.eclipse.debug.core.DebugEvent;
 import org.eclipse.debug.core.DebugPlugin;
 import org.eclipse.debug.core.IDebugEventSetListener;
@@ -40,7 +36,6 @@
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.model.IDebugTarget;
 import org.eclipse.debug.core.model.IProcess;
-
 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
 import org.eclipse.jdt.launching.JavaLaunchDelegate;
 import org.eclipse.jdt.launching.JavaRuntime;
@@ -81,17 +76,16 @@
 	 * @return system property for the policy file
 	 */
 	public String getJavaPolicyFile(File workingDir) {
-			File file = new File(workingDir, "java.policy.applet");//$NON-NLS-1$
-			if (!file.exists()) {
-				// copy it to the working directory
-				File test = LaunchingPlugin.getFileInPlugin(new Path("java.policy.applet")); //$NON-NLS-1$
+		File file = new File(workingDir, "java.policy.applet");//$NON-NLS-1$
+		if (!file.exists()) {
+			// copy it to the working directory
+			File test = LaunchingPlugin.getFileInPlugin(new Path("java.policy.applet")); //$NON-NLS-1$
 			try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) {
-					byte[] bytes = getFileByteContent(test);
-					outputStream.write(bytes);
-				} catch (IOException e) {
-					return "";//$NON-NLS-1$
+				Files.copy(test.toPath(), outputStream);
+			} catch (IOException e) {
+				return "";//$NON-NLS-1$
 			}
-			}
+		}
 		return "-Djava.security.policy=java.policy.applet";//$NON-NLS-1$
 	}
 
@@ -249,81 +243,6 @@
 		}
 	}
 
-	/**
-	 * Returns the contents of the given file as a byte array.
-	 * @param file the file
-	 * @return the byte array form the file
-	 * @throws IOException if a problem occurred reading the file.
-	 */
-	protected static byte[] getFileByteContent(File file) throws IOException {
-		try (InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
-			return getInputStreamAsByteArray(stream, (int) file.length());
-		}
-	}
-
-	/**
-	 * Returns the given input stream's contents as a byte array.
-	 * If a length is specified (ie. if length != -1), only length bytes
-	 * are returned. Otherwise all bytes in the stream are returned.
-	 * Note this doesn't close the stream.
-	 * @param stream the stream
-	 * @param length the length to read
-	 * @return the byte array from the stream
-	 * @throws IOException if a problem occurred reading the stream.
-	 */
-	protected static byte[] getInputStreamAsByteArray(InputStream stream, int length)
-		throws IOException {
-		byte[] contents;
-		if (length == -1) {
-			contents = new byte[0];
-			int contentsLength = 0;
-			int bytesRead = -1;
-			do {
-				int available = stream.available();
-
-				// resize contents if needed
-				if (contentsLength + available > contents.length) {
-					System.arraycopy(
-						contents,
-						0,
-						contents = new byte[contentsLength + available],
-						0,
-						contentsLength);
-				}
-
-				// read as many bytes as possible
-				bytesRead = stream.read(contents, contentsLength, available);
-
-				if (bytesRead > 0) {
-					// remember length of contents
-					contentsLength += bytesRead;
-				}
-			} while (bytesRead > 0);
-
-			// resize contents if necessary
-			if (contentsLength < contents.length) {
-				System.arraycopy(
-					contents,
-					0,
-					contents = new byte[contentsLength],
-					0,
-					contentsLength);
-			}
-		} else {
-			contents = new byte[length];
-			int len = 0;
-			int readSize = 0;
-			while ((readSize != -1) && (len != length)) {
-				// See PR 1FMS89U
-				// We record first the read size. In this case len is the actual read size.
-				len += readSize;
-				readSize = stream.read(contents, len, length - len);
-			}
-		}
-
-		return contents;
-	}
-
 	/* (non-Javadoc)
 	 * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
 	 */