diff options
Diffstat (limited to 'bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java')
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java index f0ccac8d63..1345448d11 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Program/win32/org/eclipse/swt/program/Program.java @@ -14,6 +14,8 @@ package org.eclipse.swt.program; import java.util.*; +import java.util.concurrent.*; +import java.util.stream.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; @@ -84,7 +86,6 @@ public static Program findProgram (String extension) { if (extension == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); if (extension.length () == 0) return null; if (extension.charAt (0) != '.') extension = "." + extension; //$NON-NLS-1$ - /* Use the character encoding for the default locale */ TCHAR key = new TCHAR (0, extension, true); Program program = null; String command = assocQueryString (OS.ASSOCSTR_COMMAND, key, true); @@ -115,9 +116,8 @@ public static String [] getExtensions () { String [] extensions = new String [1024]; char [] lpName = new char [1024]; int [] lpcName = {lpName.length}; - FILETIME ft = new FILETIME (); int dwIndex = 0, count = 0; - while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) { + while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, 0) != OS.ERROR_NO_MORE_ITEMS) { String extension = new String (lpName, 0, lpcName [0]); lpcName [0] = lpName.length; if (extension.length () > 0 && extension.charAt (0) == '.') { @@ -139,9 +139,8 @@ public static String [] getExtensions () { } static String getKeyValue (String string, boolean expand) { - /* Use the character encoding for the default locale */ TCHAR key = new TCHAR (0, string, true); - long /*int*/ [] phkResult = new long /*int*/ [1]; + long [] phkResult = new long [1]; if (OS.RegOpenKeyEx (OS.HKEY_CLASSES_ROOT, key, 0, OS.KEY_READ, phkResult) != 0) { return null; } @@ -221,21 +220,28 @@ static Program getProgram (String key, String extension) { * @return an array of programs */ public static Program [] getPrograms () { - LinkedHashSet<Program> programs = new LinkedHashSet<>(1024); char [] lpName = new char [1024]; int [] lpcName = new int [] {lpName.length}; - FILETIME ft = new FILETIME (); int dwIndex = 0; - while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, ft) != OS.ERROR_NO_MORE_ITEMS) { + LinkedHashSet<String> paths = new LinkedHashSet<>(); + // enumerating all class names takes ~ 1/5 of the time: + while (OS.RegEnumKeyEx (OS.HKEY_CLASSES_ROOT, dwIndex, lpName, lpcName, null, null, null, 0) != OS.ERROR_NO_MORE_ITEMS) { String path = new String (lpName, 0, lpcName [0]); lpcName [0] = lpName.length; - Program program = getProgram (path, null); - if (program != null) { - programs.add(program); - } + paths.add(path); dwIndex++; } - return programs.toArray(new Program[programs.size()]); + //map paths to programs in parallel which takes now ~ 4/5 of time: + ConcurrentHashMap<String, Program> programs = new ConcurrentHashMap<>(paths.size()); + paths.stream().parallel().forEach(path -> { + Program program = getProgram(path, null); // getProgram takes most time + if (program != null) { + programs.put(path, program); + } + }); + // restore sort order and make distinct in terms of Program::equals: + LinkedHashSet<Program> sortedPrograms = paths.stream().map(name->programs.get(name)).filter(p->p!=null).collect(Collectors.toCollection(LinkedHashSet::new)); + return sortedPrograms.toArray(new Program[sortedPrograms.size()]); } /** @@ -252,7 +258,7 @@ public static Program [] getPrograms () { * </ul> */ public static boolean launch (String fileName) { - return launch(fileName, null); + return launch(fileName, null); } /** @@ -277,19 +283,18 @@ public static boolean launch (String fileName) { public static boolean launch (String fileName, String workingDir) { if (fileName == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); - /* Use the character encoding for the default locale */ - long /*int*/ hHeap = OS.GetProcessHeap (); + long hHeap = OS.GetProcessHeap (); TCHAR buffer = new TCHAR (0, fileName, true); int byteCount = buffer.length () * TCHAR.sizeof; - long /*int*/ lpFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); + long lpFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); OS.MoveMemory (lpFile, buffer, byteCount); - long /*int*/ lpDirectory = 0; + long lpDirectory = 0; if (workingDir != null && OS.PathIsExe(lpFile)) { - TCHAR buffer1 = new TCHAR (0, workingDir, true); - byteCount = buffer1.length () * TCHAR.sizeof; - lpDirectory = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); - OS.MoveMemory (lpDirectory, buffer1, byteCount); + TCHAR buffer1 = new TCHAR (0, workingDir, true); + byteCount = buffer1.length () * TCHAR.sizeof; + lpDirectory = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); + OS.MoveMemory (lpDirectory, buffer1, byteCount); } SHELLEXECUTEINFO info = new SHELLEXECUTEINFO (); @@ -333,11 +338,10 @@ public boolean execute (String fileName) { } if (append) fileName = " \"" + fileName + "\""; String commandLine = prefix + fileName + suffix; - long /*int*/ hHeap = OS.GetProcessHeap (); - /* Use the character encoding for the default locale */ + long hHeap = OS.GetProcessHeap (); TCHAR buffer = new TCHAR (0, commandLine, true); int byteCount = buffer.length () * TCHAR.sizeof; - long /*int*/ lpCommandLine = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); + long lpCommandLine = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); OS.MoveMemory (lpCommandLine, buffer, byteCount); STARTUPINFO lpStartupInfo = new STARTUPINFO (); lpStartupInfo.cb = STARTUPINFO.sizeof; @@ -386,9 +390,8 @@ public ImageData getImageData () { fileName = fileName.substring (1, length - 1); } } - /* Use the character encoding for the default locale */ TCHAR lpszFile = new TCHAR (0, fileName, true); - long /*int*/ [] phiconSmall = new long /*int*/[1], phiconLarge = null; + long [] phiconSmall = new long[1], phiconLarge = null; OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1); if (phiconSmall [0] == 0) return null; Image image = Image.win32_new (null, SWT.ICON, phiconSmall [0]); |