You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/29 20:09:50 UTC

svn commit: r1097902 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/platform/windows/ java/org/apache/commons/runtime/util/ test/org/apache/commons/runtime/

Author: mturk
Date: Fri Apr 29 18:09:49 2011
New Revision: 1097902

URL: http://svn.apache.org/viewvc?rev=1097902&view=rev
Log:
Add string block helpers

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExec.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java?rev=1097902&r1=1097901&r2=1097902&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/RegistryKey.java Fri Apr 29 18:09:49 2011
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.SyncFailedException;
 import java.util.EnumSet;
 import org.apache.commons.runtime.Status;
+import org.apache.commons.runtime.SystemException;
 
 /**
  * Registry key class.
@@ -197,19 +198,18 @@ public class RegistryKey implements Clos
     }
 
     private static native long open0(long key, String name, int sam)
-        throws IOException, SecurityException;
+        throws IOException, SystemException;
     private static native long create0(long key, String name, int sam)
-        throws IOException, SecurityException;
+        throws IOException, SystemException;
+    private static native int  info0(long key, String name, int[] info);
 
     public RegistryKey open(String name, EnumSet<RegistryKeyAccessRights> mode)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
-        if (name == null || mode == null) {
+        if (name == null || mode == null)
             throw new NullPointerException();
-        }
-        if (hKey == 0L || hKey == -1L) {
+        if (hKey == 0L || hKey == -1L)
             throw new InvalidHandleException();
-        }
         long sKey = open0(hKey, name, RegistryKeyAccessRights.bitmapOf(mode));
         if (sKey == 0L || sKey == -1L)
             return null;
@@ -218,26 +218,24 @@ public class RegistryKey implements Clos
     }
 
     public RegistryKey open(String name, RegistryKeyAccessRights... mode)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
         return open(name, RegistryKeyAccessRights.of(mode));
     }
 
     public RegistryKey open(String name)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
         return open(name, RegistryKeyAccessRights.READ);
     }
 
     public RegistryKey create(String name, EnumSet<RegistryKeyAccessRights> mode)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
-        if (name == null || mode == null) {
+        if (name == null || mode == null)
             throw new NullPointerException();
-        }
-        if (hKey == 0L || hKey == -1L) {
+        if (hKey == 0L || hKey == -1L)
             throw new InvalidHandleException();
-        }
         long sKey = create0(hKey, name, RegistryKeyAccessRights.bitmapOf(mode));
         if (sKey == 0L || sKey == -1L)
             return null;
@@ -246,15 +244,30 @@ public class RegistryKey implements Clos
     }
 
     public RegistryKey create(String name, RegistryKeyAccessRights... mode)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
         return create(name, RegistryKeyAccessRights.of(mode));
     }
 
     public RegistryKey create(String name)
-        throws NullPointerException, IOException, SecurityException
+        throws NullPointerException, IOException, SystemException
     {
         return create(name, RegistryKeyAccessRights.ALL_ACCESS);
     }
 
+    public RegistryValueType getValueType(String valueName)
+        throws NullPointerException, IOException
+    {
+        if (valueName == null)
+            throw new NullPointerException();
+        if (hKey == 0L || hKey == -1L)
+            throw new InvalidHandleException();
+        int[] info = new int[2];
+
+        int rc = info0(hKey, valueName, info);
+        if (rc != 0) {
+            throw new IOException(Status.describe(rc));
+        }
+        return RegistryValueType.valueOf(info[0]);
+    }
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExec.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExec.java?rev=1097902&r1=1097901&r2=1097902&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExec.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsExec.java Fri Apr 29 18:09:49 2011
@@ -21,6 +21,7 @@ import org.apache.commons.runtime.Errno;
 import org.apache.commons.runtime.Status;
 import org.apache.commons.runtime.InvalidArgumentException;
 import org.apache.commons.runtime.SystemException;
+import org.apache.commons.runtime.util.Utils;
 import java.io.ByteArrayOutputStream;
 import java.io.CharArrayWriter;
 import java.io.OutputStream;
@@ -83,21 +84,8 @@ final class WindowsExec extends Exec
         if (executable == null) {
             return Status.PARENT_ERROR;
         }
-        char [] envb = null;
-        if (env != null) {
-            CharArrayWriter ew = new CharArrayWriter();
-            for (int i = 0; i < env.length; i++) {
-                try {
-                    ew.write(env[i]);
-                    ew.append('\0');
-                } catch (Exception x) {
-                    //TODO: Handle OOM errors
-                }
-            }
-            ew.append('\0');
-            envb = ew.toCharArray();
-        }
-        long rv = run0(executable, args.toString(), envb, cwd, inp, out, err, timeout);
+        char[] eb = Utils.stringArrayToCharBlock(env);
+        long rv = run0(executable, args.toString(), eb, cwd, inp, out, err, timeout);
         exitval = (int)(rv & 0xffffffffL);
         int res = (int)(rv >>> 32);
 

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java?rev=1097902&r1=1097901&r2=1097902&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Utils.java Fri Apr 29 18:09:49 2011
@@ -16,9 +16,12 @@
 
 package org.apache.commons.runtime.util;
 
-import java.io.IOException;
+import java.io.CharArrayWriter;
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Random;
 import java.util.concurrent.atomic.AtomicLong;
 import org.apache.commons.runtime.SystemId;
@@ -308,5 +311,95 @@ public final class Utils
         return false;
     }
 
+    public static char[] stringArrayToCharBlock(String[] sa)
+    {
+        if (sa == null)
+            return null;
+        CharArrayWriter ca = new CharArrayWriter();
+        for (int i = 0; i < sa.length; i++) {
+            try {
+                ca.write(sa[i]);
+                ca.append('\u0000');
+            } catch (Exception x) {
+                //TODO: Handle OOM errors
+            }
+        }
+        if (sa.length == 0)
+            ca.append('\u0000');
+        ca.append('\u0000');
+        return ca.toCharArray();
+    }
+
+    public static char[] stringArrayToCharBlock(List<String> sa)
+    {
+        if (sa == null)
+            return null;
+        CharArrayWriter ca = new CharArrayWriter();
+        for (String s : sa) {
+            try {
+                ca.write(s);
+                ca.append('\u0000');
+            } catch (Exception x) {
+                //TODO: Handle OOM errors
+            }
+        }
+        if (sa.isEmpty())
+            ca.append('\u0000');
+        ca.append('\u0000');
+        return ca.toCharArray();
+    }
+
+    public static List<String> charBlockToStringList(char[] ca)
+    {
+        ArrayList<String> sa = new ArrayList<String>();
+        int sp = 0;
+        int ep = 0;
+
+        while (ca[sp] != '\u0000') {
+            while (ca[ep] != '\u0000') {
+                ep++;
+            }
+            try {
+                String s = new String(ca, sp, ep - sp);
+                sa.add(s);
+            } catch (Exception x) {
+                break;
+            }
+            sp = ++ep;
+        }
+        return sa;
+    }
+
+    public static String[] charBlockToStringArray(char[] ca)
+    {
+        int sp = 0;
+        int ep = 0;
+        int nc = 0;
+
+        while (ca[sp] != '\u0000') {
+            while (ca[ep] != '\u0000') {
+                ep++;
+            }
+            sp = ++ep;
+            nc++;
+        }
+        String[] sa = new String[nc];
+        nc = 0;
+        sp = 0;
+        ep = 0;
+        while (ca[sp] != '\u0000') {
+            while (ca[ep] != '\u0000') {
+                ep++;
+            }
+            try {
+                sa[nc++] = new String(ca, sp, ep - sp);
+            } catch (Exception x) {
+                break;
+            }
+            sp = ++ep;
+        }
+        return sa;
+    }
+
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java?rev=1097902&r1=1097901&r2=1097902&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java (original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestUtils.java Fri Apr 29 18:09:49 2011
@@ -77,4 +77,14 @@ public class TestUtils extends Assert
         assertEquals(asc, acs);
     }
 
+    @Test(groups = { "core" })
+    public void strBlock()
+        throws Exception
+    {
+        char[] ca = { 'a', '\u0000', 'b', 'c', 'd', '\u0000', '\u0000'};
+        String[] s = Utils.charBlockToStringArray(ca);
+        assertEquals(s[0], "a");
+        assertEquals(s[1], "bcd");
+    }
+
 }