(no commit message)
[jalview.git] / forester / java / src / org / forester / util / WindowsUtils.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // From: http://www.rgagnon.com/javadetails/java-0652.html
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public
18 // License along with this library; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 //
21 // Contact: phylosoft @ gmail . com
22 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
23
24 package org.forester.util;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.StringWriter;
29
30 public class WindowsUtils {
31
32     private static final String REGQUERY_UTIL      = "reg query ";
33     private static final String REGSTR_TOKEN       = "REG_SZ";
34     private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL
35             + "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
36             + "Explorer\\Shell Folders\" /v DESKTOP";
37
38     private WindowsUtils() {
39     }
40
41     public static String getCurrentUserDesktopPath() {
42         try {
43             final Process process = Runtime.getRuntime().exec( DESKTOP_FOLDER_CMD );
44             final StreamReader reader = new StreamReader( process.getInputStream() );
45             reader.start();
46             process.waitFor();
47             reader.join();
48             final String result = reader.getResult();
49             final int p = result.indexOf( REGSTR_TOKEN );
50             if ( p == -1 ) {
51                 return null;
52             }
53             return result.substring( p + REGSTR_TOKEN.length() ).trim();
54         }
55         catch ( final Exception e ) {
56             return null;
57         }
58     }
59
60     static class StreamReader extends Thread {
61
62         private final InputStream  is;
63         private final StringWriter sw;
64
65         StreamReader( final InputStream is ) {
66             this.is = is;
67             sw = new StringWriter();
68         }
69
70         String getResult() {
71             return sw.toString();
72         }
73
74         @Override
75         public void run() {
76             try {
77                 int c;
78                 while ( ( c = is.read() ) != -1 ) {
79                     sw.write( c );
80                 }
81             }
82             catch ( final IOException e ) {
83                 // Do nothing
84             }
85         }
86     }
87 }