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;
*
* @author Casey Marshall (rsdio@metastatic.org)
*/
-final class Util
+public final class Util
{
// Constants.
// 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];
* @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++)
/**
* See {@link #toHexString(byte[],int,int)}.
*/
- static String toHexString(byte[] buf)
+ public static String toHexString(byte[] buf)
{
return Util.toHexString(buf, 0, buf.length);
}
* @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++)
/**
* 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);
}
* @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();
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++)
{
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, "");
}
/**
* 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);
}
/**
* See {@link #hexDump(byte[],int,int,String)}.
*/
- static String hexDump(byte[] buf)
+ public static String hexDump(byte[] buf)
{
return hexDump(buf, 0, buf.length);
}
* 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();
* @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);
/**
* 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);
}
* 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 +
* @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))
*
* @return The current time, in seconds.
*/
- static int unixTime()
+ public static int unixTime()
{
return (int) (System.currentTimeMillis() / 1000L);
}
* @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()
* @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()