OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / javax / net / ssl / provider / Util.java
index 15790dd..ba8ea7d 100644 (file)
@@ -38,11 +38,16 @@ exception statement from your version.  */
 
 package gnu.javax.net.ssl.provider;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.math.BigInteger;
 
+import java.nio.ByteBuffer;
+
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.security.Security;
@@ -52,7 +57,7 @@ import java.security.Security;
  *
  * @author Casey Marshall (rsdio@metastatic.org)
  */
-final class Util
+public final class Util
 {
 
   // Constants.
@@ -66,13 +71,40 @@ final class Util
   // Class methods.
   // -------------------------------------------------------------------------
 
+  public static Object wrapBuffer(ByteBuffer buffer)
+  {
+    return wrapBuffer(buffer, "");
+  }
+  
+  public static Object wrapBuffer(ByteBuffer buffer, String prefix)
+  {
+    return new WrappedBuffer(buffer, prefix);
+  }
+  
+  private static class WrappedBuffer
+  {
+    private final ByteBuffer buffer;
+    private final String prefix;
+    
+    WrappedBuffer(ByteBuffer buffer, String prefix)
+    {
+      this.buffer = buffer;
+      this.prefix = prefix;
+    }
+    
+    public String toString()
+    {
+      return hexDump(buffer, prefix);
+    }
+  }
+  
   /**
    * Convert a hexadecimal string into its byte representation.
    *
    * @param hex The hexadecimal string.
    * @return The converted bytes.
    */
-  static byte[] toByteArray(String hex)
+  public static byte[] toByteArray(String hex)
   {
     hex = hex.toLowerCase();
     byte[] buf = new byte[hex.length() / 2];
@@ -94,7 +126,7 @@ final class Util
    * @param len The number of bytes to format.
    * @return A hexadecimal representation of the specified bytes.
    */
-  static String toHexString(byte[] buf, int off, int len)
+  public static String toHexString(byte[] buf, int off, int len)
   {
     StringBuffer str = new StringBuffer();
     for (int i = 0; i < len; i++)
@@ -108,7 +140,7 @@ final class Util
   /**
    * See {@link #toHexString(byte[],int,int)}.
    */
-  static String toHexString(byte[] buf)
+  public static String toHexString(byte[] buf)
   {
     return Util.toHexString(buf, 0, buf.length);
   }
@@ -123,7 +155,7 @@ final class Util
    * @param sep The character to insert between octets.
    * @return A hexadecimal representation of the specified bytes.
    */
-  static String toHexString(byte[] buf, int off, int len, char sep)
+  public static String toHexString(byte[] buf, int off, int len, char sep)
   {
     StringBuffer str = new StringBuffer();
     for (int i = 0; i < len; i++)
@@ -139,7 +171,7 @@ final class Util
   /**
    * See {@link #toHexString(byte[],int,int,char)}.
    */
-  static String toHexString(byte[] buf, char sep)
+  public static String toHexString(byte[] buf, char sep)
   {
     return Util.toHexString(buf, 0, buf.length, sep);
   }
@@ -159,7 +191,7 @@ final class Util
    * @param prefix A string to prepend to every line.
    * @return The formatted string.
    */
-  static String hexDump(byte[] buf, int off, int len, String prefix)
+  public static String hexDump(byte[] buf, int off, int len, String prefix)
   {
     String nl = getProperty("line.separator");
     StringBuffer str = new StringBuffer();
@@ -172,7 +204,7 @@ final class Util
         str.append("  ");
         String s = Util.toHexString(buf, i+off, Math.min(16, len-i), ' ');
         str.append(s);
-        for (int j = 56 - (56 - s.length()); j < 56; j++)
+        for (int j = s.length(); j < 49; j++)
           str.append(" ");
         for (int j = 0; j < Math.min(16, len - i); j++)
           {
@@ -187,10 +219,49 @@ final class Util
     return str.toString();
   }
 
+  public static String hexDump (ByteBuffer buf)
+  {
+    return hexDump (buf, null);
+  }
+
+  public static String hexDump (ByteBuffer buf, String prefix)
+  {
+    buf = buf.duplicate();
+    StringWriter str = new StringWriter ();
+    PrintWriter out = new PrintWriter (str);
+    int i = 0;
+    int len = buf.remaining();
+    byte[] line = new byte[16];
+    while (i < len)
+      {
+        if (prefix != null)
+          out.print(prefix);
+        out.print(Util.formatInt (i, 16, 8));
+        out.print("  ");
+        int l = Math.min(16, len - i);
+        buf.get(line, 0, l);
+        String s = Util.toHexString(line, 0, l, ' ');
+        out.print(s);
+        for (int j = s.length(); j < 49; j++)
+          out.print(' ');
+        for (int j = 0; j < l; j++)
+          {
+            int c = line[j] & 0xFF;
+            if (c < 0x20 || c > 0x7E)
+              out.print('.');
+            else
+              out.print((char) c);
+          }
+        out.println();
+        i += 16;
+      }
+    return str.toString();
+  }
+
   /**
    * See {@link #hexDump(byte[],int,int,String)}.
    */
-  static String hexDump(byte[] buf, int off, int len)
+  public static String hexDump(byte[] buf, int off, int len)
   {
     return hexDump(buf, off, len, "");
   }
@@ -198,7 +269,7 @@ final class Util
   /**
    * See {@link #hexDump(byte[],int,int,String)}.
    */
-  static String hexDump(byte[] buf, String prefix)
+  public static String hexDump(byte[] buf, String prefix)
   {
     return hexDump(buf, 0, buf.length, prefix);
   }
@@ -206,7 +277,7 @@ final class Util
   /**
    * See {@link #hexDump(byte[],int,int,String)}.
    */
-  static String hexDump(byte[] buf)
+  public static String hexDump(byte[] buf)
   {
     return hexDump(buf, 0, buf.length);
   }
@@ -220,7 +291,7 @@ final class Util
    *   zero-padded to this length, but may be longer.
    * @return The formatted integer.
    */
-  static String formatInt(int i, int radix, int len)
+  public static String formatInt(int i, int radix, int len)
   {
     String s = Integer.toString(i, radix);
     StringBuffer buf = new StringBuffer();
@@ -237,7 +308,7 @@ final class Util
    * @param b2 The second byte array.
    * @return The concatenation of b1 and b2.
    */
-  static byte[] concat(byte[] b1, byte[] b2)
+  public static byte[] concat(byte[] b1, byte[] b2)
   {
     byte[] b3 = new byte[b1.length+b2.length];
     System.arraycopy(b1, 0, b3, 0, b1.length);
@@ -248,7 +319,7 @@ final class Util
   /**
    * See {@link #trim(byte[],int,int)}.
    */
-  static byte[] trim(byte[] buffer, int len)
+  public static byte[] trim(byte[] buffer, int len)
   {
     return trim(buffer, 0, len);
   }
@@ -266,7 +337,7 @@ final class Util
    *         length.
    * @return The trimmed byte array.
    */
-  static byte[] trim(byte[] buffer, int off, int len)
+  public static byte[] trim(byte[] buffer, int off, int len)
   {
     if (off < 0 || len < 0 || off > buffer.length)
       throw new IndexOutOfBoundsException("max=" + buffer.length +
@@ -286,7 +357,7 @@ final class Util
    * @return The byte representation of the big integer, with any leading
    *   zero removed.
    */
-  static byte[] trim(BigInteger bi)
+  public static byte[] trim(BigInteger bi)
   {
     byte[] buf = bi.toByteArray();
     if (buf[0] == 0x00 && !bi.equals(BigInteger.ZERO))
@@ -305,7 +376,7 @@ final class Util
    *
    * @return The current time, in seconds.
    */
-  static int unixTime()
+  public static int unixTime()
   {
     return (int) (System.currentTimeMillis() / 1000L);
   }
@@ -385,7 +456,7 @@ final class Util
    * @throws SecurityException If the Jessie code still does not have
    *   permission to read the property.
    */
-  static String getProperty(final String name)
+  @Deprecated static String getProperty(final String name)
   {
     return (String) AccessController.doPrivileged(
       new PrivilegedAction()
@@ -407,7 +478,7 @@ final class Util
    * @throws SecurityException If the Jessie code still does not have
    *   permission to read the property.
    */
-  static String getSecurityProperty(final String name)
+  @Deprecated static String getSecurityProperty(final String name)
   {
     return (String) AccessController.doPrivileged(
       new PrivilegedAction()