Imported GNU Classpath 0.92
2006-08-14 Mark Wielaard <mark@klomp.org> Imported GNU Classpath 0.92 * HACKING: Add more importing hints. Update automake version requirement. * configure.ac (gconf-peer): New enable AC argument. Add --disable-gconf-peer and --enable-default-preferences-peer to classpath configure when gconf is disabled. * scripts/makemake.tcl: Set gnu/java/util/prefs/gconf and gnu/java/awt/dnd/peer/gtk to bc. Classify gnu/java/security/Configuration.java as generated source file. * gnu/java/lang/management/VMGarbageCollectorMXBeanImpl.java, gnu/java/lang/management/VMMemoryPoolMXBeanImpl.java, gnu/java/lang/management/VMClassLoadingMXBeanImpl.java, gnu/java/lang/management/VMRuntimeMXBeanImpl.java, gnu/java/lang/management/VMMemoryManagerMXBeanImpl.java, gnu/java/lang/management/VMThreadMXBeanImpl.java, gnu/java/lang/management/VMMemoryMXBeanImpl.java, gnu/java/lang/management/VMCompilationMXBeanImpl.java: New VM stub classes. * java/lang/management/VMManagementFactory.java: Likewise. * java/net/VMURLConnection.java: Likewise. * gnu/java/nio/VMChannel.java: Likewise. * java/lang/Thread.java (getState): Add stub implementation. * java/lang/Class.java (isEnum): Likewise. * java/lang/Class.h (isEnum): Likewise. * gnu/awt/xlib/XToolkit.java (getClasspathTextLayoutPeer): Removed. * javax/naming/spi/NamingManager.java: New override for StackWalker functionality. * configure, sources.am, Makefile.in, gcj/Makefile.in, include/Makefile.in, testsuite/Makefile.in: Regenerated. From-SVN: r116139
This commit is contained in:
@@ -41,47 +41,37 @@ package gnu.java.security.util;
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* <p>A collection of utility methods used throughout this project.</p>
|
||||
* A collection of utility methods used throughout this project.
|
||||
*/
|
||||
public class Util
|
||||
{
|
||||
|
||||
// Constants and variables
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// Hex charset
|
||||
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
// Base-64 charset
|
||||
private static final String BASE64_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
|
||||
private static final String BASE64_CHARS =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
|
||||
|
||||
private static final char[] BASE64_CHARSET = BASE64_CHARS.toCharArray();
|
||||
|
||||
// Constructor(s)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/** Trivial constructor to enforce Singleton pattern. */
|
||||
private Util()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
// Class methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* <p>This method calls the method with same name and three arguments as:</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included.
|
||||
* <p>
|
||||
* This method calls the method with same name and three arguments as:
|
||||
* <pre>
|
||||
* toString(ba, 0, ba.length);
|
||||
* toString(ba, 0, ba.length);
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte array.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte array.
|
||||
*/
|
||||
public static String toString(byte[] ba)
|
||||
{
|
||||
@@ -89,17 +79,17 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each byte
|
||||
* is converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each
|
||||
* byte is converted to 2 hex symbols; zero(es) included.
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @param offset the index from which to start considering the bytes to
|
||||
* convert.
|
||||
* convert.
|
||||
* @param length the count of bytes, starting from the designated offset to
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte sub-array.
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte sub-array.
|
||||
*/
|
||||
public static final String toString(byte[] ba, int offset, int length)
|
||||
{
|
||||
@@ -108,26 +98,24 @@ public class Util
|
||||
{
|
||||
k = ba[offset + i++];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included. The argument is
|
||||
* treated as a large little-endian integer and is returned as a
|
||||
* large big-endian integer.</p>
|
||||
*
|
||||
* <p>This method calls the method with same name and three arguments as:</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array. Each byte is
|
||||
* converted to 2 hex symbols; zero(es) included. The argument is treated as a
|
||||
* large little-endian integer and is returned as a large big-endian integer.
|
||||
* <p>
|
||||
* This method calls the method with same name and three arguments as:
|
||||
* <pre>
|
||||
* toReversedString(ba, 0, ba.length);
|
||||
* toReversedString(ba, 0, ba.length);
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte array.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte array.
|
||||
*/
|
||||
public static String toReversedString(byte[] ba)
|
||||
{
|
||||
@@ -135,20 +123,20 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each byte
|
||||
* is converted to 2 hex symbols; zero(es) included.</p>
|
||||
*
|
||||
* <p>The byte array is treated as a large little-endian integer, and
|
||||
* is returned as a large big-endian integer.</p>
|
||||
*
|
||||
* Returns a string of hexadecimal digits from a byte array, starting at
|
||||
* <code>offset</code> and consisting of <code>length</code> bytes. Each
|
||||
* byte is converted to 2 hex symbols; zero(es) included.
|
||||
* <p>
|
||||
* The byte array is treated as a large little-endian integer, and is returned
|
||||
* as a large big-endian integer.
|
||||
*
|
||||
* @param ba the byte array to convert.
|
||||
* @param offset the index from which to start considering the bytes to
|
||||
* convert.
|
||||
* convert.
|
||||
* @param length the count of bytes, starting from the designated offset to
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte)
|
||||
* representing the designated input byte sub-array.
|
||||
* convert.
|
||||
* @return a string of hexadecimal characters (two for each byte) representing
|
||||
* the designated input byte sub-array.
|
||||
*/
|
||||
public static final String toReversedString(byte[] ba, int offset, int length)
|
||||
{
|
||||
@@ -157,14 +145,16 @@ public class Util
|
||||
{
|
||||
k = ba[offset + i--];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a byte array from a string of hexadecimal digits.</p>
|
||||
*
|
||||
* <p>
|
||||
* Returns a byte array from a string of hexadecimal digits.
|
||||
* </p>
|
||||
*
|
||||
* @param s a string of hexadecimal ASCII characters
|
||||
* @return the decoded byte array from the input hexadecimal string.
|
||||
*/
|
||||
@@ -174,22 +164,20 @@ public class Util
|
||||
byte[] result = new byte[((limit + 1) / 2)];
|
||||
int i = 0, j = 0;
|
||||
if ((limit % 2) == 1)
|
||||
{
|
||||
result[j++] = (byte) fromDigit(s.charAt(i++));
|
||||
}
|
||||
result[j++] = (byte) fromDigit(s.charAt(i++));
|
||||
while (i < limit)
|
||||
{
|
||||
result[j] = (byte) (fromDigit(s.charAt(i++)) << 4);
|
||||
result[j ] = (byte) (fromDigit(s.charAt(i++)) << 4);
|
||||
result[j++] |= (byte) fromDigit(s.charAt(i++));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a byte array from a string of hexadecimal digits, interpreting
|
||||
* them as a large big-endian integer and returning it as a large
|
||||
* little-endian integer.</p>
|
||||
*
|
||||
* Returns a byte array from a string of hexadecimal digits, interpreting them
|
||||
* as a large big-endian integer and returning it as a large little-endian
|
||||
* integer.
|
||||
*
|
||||
* @param s a string of hexadecimal ASCII characters
|
||||
* @return the decoded byte array from the input hexadecimal string.
|
||||
*/
|
||||
@@ -199,45 +187,37 @@ public class Util
|
||||
byte[] result = new byte[((limit + 1) / 2)];
|
||||
int i = 0;
|
||||
if ((limit % 2) == 1)
|
||||
{
|
||||
result[i++] = (byte) fromDigit(s.charAt(--limit));
|
||||
}
|
||||
result[i++] = (byte) fromDigit(s.charAt(--limit));
|
||||
while (limit > 0)
|
||||
{
|
||||
result[i] = (byte) fromDigit(s.charAt(--limit));
|
||||
result[i ] = (byte) fromDigit(s.charAt(--limit));
|
||||
result[i++] |= (byte) (fromDigit(s.charAt(--limit)) << 4);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a number from <code>0</code> to <code>15</code> corresponding
|
||||
* to the designated hexadecimal digit.</p>
|
||||
*
|
||||
* Returns a number from <code>0</code> to <code>15</code> corresponding
|
||||
* to the designated hexadecimal digit.
|
||||
*
|
||||
* @param c a hexadecimal ASCII symbol.
|
||||
*/
|
||||
public static int fromDigit(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
return c - '0';
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return c - 'A' + 10;
|
||||
}
|
||||
return c - 'A' + 10;
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return c - 'a' + 10;
|
||||
}
|
||||
return c - 'a' + 10;
|
||||
else
|
||||
throw new IllegalArgumentException("Invalid hexadecimal digit: " + c);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 8 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned integer <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 8 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned integer <code>n</code>.
|
||||
*
|
||||
* @param n the unsigned integer to convert.
|
||||
* @return a hexadecimal string 8-character long.
|
||||
*/
|
||||
@@ -253,8 +233,8 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of hexadecimal digits from an integer array. Each int
|
||||
* is converted to 4 hex symbols.</p>
|
||||
* Returns a string of hexadecimal digits from an integer array. Each int is
|
||||
* converted to 4 hex symbols.
|
||||
*/
|
||||
public static String toString(int[] ia)
|
||||
{
|
||||
@@ -268,17 +248,17 @@ public class Util
|
||||
buf[j++] = HEX_DIGITS[(k >>> 20) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 16) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 12) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[k & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 8) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
|
||||
buf[j++] = HEX_DIGITS[ k & 0x0F];
|
||||
}
|
||||
return new String(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 16 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned long <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 16 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the unsigned long <code>n</code>.
|
||||
*
|
||||
* @param n the unsigned long to convert.
|
||||
* @return a hexadecimal string 16-character long.
|
||||
*/
|
||||
@@ -287,18 +267,18 @@ public class Util
|
||||
char[] b = new char[16];
|
||||
for (int i = 15; i >= 0; i--)
|
||||
{
|
||||
b[i] = HEX_DIGITS[(int) (n & 0x0FL)];
|
||||
b[i] = HEX_DIGITS[(int)(n & 0x0FL)];
|
||||
n >>>= 4;
|
||||
}
|
||||
return new String(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise byte arrays that will be constructed later from such strings;
|
||||
* eg. s-box values.</p>
|
||||
*
|
||||
* eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is odd.
|
||||
*/
|
||||
public static String toUnicodeString(byte[] ba)
|
||||
@@ -307,11 +287,11 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise byte arrays that will be constructed later from such strings;
|
||||
* eg. s-box values.</p>
|
||||
*
|
||||
* eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is odd.
|
||||
*/
|
||||
public static final String toUnicodeString(byte[] ba, int offset, int length)
|
||||
@@ -324,31 +304,27 @@ public class Util
|
||||
while (i < length)
|
||||
{
|
||||
sb.append("\\u");
|
||||
|
||||
k = ba[offset + i++];
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
k = ba[offset + i++];
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
if ((++j % 8) == 0)
|
||||
{
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"").append('\n');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Similar to the <code>toString()</code> method except that the Unicode
|
||||
* Similar to the <code>toString()</code> method except that the Unicode
|
||||
* escape character is inserted before every pair of bytes. Useful to
|
||||
* externalise integer arrays that will be constructed later from such
|
||||
* strings; eg. s-box values.</p>
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is not a multiple of 4.
|
||||
* strings; eg. s-box values.
|
||||
*
|
||||
* @throws ArrayIndexOutOfBoundsException if the length is not a multiple of
|
||||
* 4.
|
||||
*/
|
||||
public static String toUnicodeString(int[] ia)
|
||||
{
|
||||
@@ -367,14 +343,11 @@ public class Util
|
||||
sb.append(HEX_DIGITS[(k >>> 16) & 0x0F]);
|
||||
sb.append("\\u");
|
||||
sb.append(HEX_DIGITS[(k >>> 12) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[k & 0x0F]);
|
||||
|
||||
sb.append(HEX_DIGITS[(k >>> 8) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[(k >>> 4) & 0x0F]);
|
||||
sb.append(HEX_DIGITS[ k & 0x0F]);
|
||||
if ((++j % 4) == 0)
|
||||
{
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"+").append('\n').append("\"");
|
||||
}
|
||||
sb.append("\"").append('\n');
|
||||
return sb.toString();
|
||||
@@ -388,20 +361,20 @@ public class Util
|
||||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
c = s.charAt(i >>> 1);
|
||||
result[i] = (byte) (((i & 1) == 0) ? c >>> 8 : c);
|
||||
result[i] = (byte)(((i & 1) == 0) ? c >>> 8 : c);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Dumps a byte array as a string, in a format that is easy to read for
|
||||
* Dumps a byte array as a string, in a format that is easy to read for
|
||||
* debugging. The string <code>m</code> is prepended to the start of each
|
||||
* line.</p>
|
||||
*
|
||||
* <p>If <code>offset</code> and <code>length</code> are omitted, the whole
|
||||
* line.
|
||||
* <p>
|
||||
* If <code>offset</code> and <code>length</code> are omitted, the whole
|
||||
* array is used. If <code>m</code> is omitted, nothing is prepended to each
|
||||
* line.</p>
|
||||
*
|
||||
* line.
|
||||
*
|
||||
* @param data the byte array to be dumped.
|
||||
* @param offset the offset within <i>data</i> to start from.
|
||||
* @param length the number of bytes to dump.
|
||||
@@ -411,23 +384,17 @@ public class Util
|
||||
public static String dumpString(byte[] data, int offset, int length, String m)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
return m + "null\n";
|
||||
}
|
||||
return m + "null\n";
|
||||
StringBuffer sb = new StringBuffer(length * 3);
|
||||
if (length > 32)
|
||||
{
|
||||
sb.append(m).append("Hexadecimal dump of ").append(length).append(
|
||||
" bytes...\n");
|
||||
}
|
||||
sb.append(m).append("Hexadecimal dump of ")
|
||||
.append(length).append(" bytes...\n");
|
||||
// each line will list 32 bytes in 4 groups of 8 each
|
||||
int end = offset + length;
|
||||
String s;
|
||||
int l = Integer.toString(length).length();
|
||||
if (l < 4)
|
||||
{
|
||||
l = 4;
|
||||
}
|
||||
l = 4;
|
||||
for (; offset < end; offset += 32)
|
||||
{
|
||||
if (length > 32)
|
||||
@@ -437,16 +404,10 @@ public class Util
|
||||
}
|
||||
int i = 0;
|
||||
for (; i < 32 && offset + i + 7 < end; i += 8)
|
||||
{
|
||||
sb.append(toString(data, offset + i, 8)).append(' ');
|
||||
}
|
||||
sb.append(toString(data, offset + i, 8)).append(' ');
|
||||
if (i < 32)
|
||||
{
|
||||
for (; i < 32 && offset + i < end; i++)
|
||||
{
|
||||
sb.append(byteToString(data[offset + i]));
|
||||
}
|
||||
}
|
||||
for (; i < 32 && offset + i < end; i++)
|
||||
sb.append(byteToString(data[offset + i]));
|
||||
sb.append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
@@ -468,9 +429,9 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a string of 2 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the lowest 8 bits of <code>n</code>.</p>
|
||||
*
|
||||
* Returns a string of 2 hexadecimal digits (most significant digit first)
|
||||
* corresponding to the lowest 8 bits of <code>n</code>.
|
||||
*
|
||||
* @param n the byte value to convert.
|
||||
* @return a string of 2 hex characters representing the input.
|
||||
*/
|
||||
@@ -481,15 +442,15 @@ public class Util
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Converts a designated byte array to a Base-64 representation, with the
|
||||
* Converts a designated byte array to a Base-64 representation, with the
|
||||
* exceptions that (a) leading 0-byte(s) are ignored, and (b) the character
|
||||
* '.' (dot) shall be used instead of "+' (plus).</p>
|
||||
*
|
||||
* <p>Used by SASL password file manipulation primitives.</p>
|
||||
*
|
||||
* '.' (dot) shall be used instead of "+' (plus).
|
||||
* <p>
|
||||
* Used by SASL password file manipulation primitives.
|
||||
*
|
||||
* @param buffer an arbitrary sequence of bytes to represent in Base-64.
|
||||
* @return unpadded (without the '=' character(s)) Base-64 representation of
|
||||
* the input.
|
||||
* the input.
|
||||
*/
|
||||
public static final String toBase64(byte[] buffer)
|
||||
{
|
||||
@@ -535,9 +496,7 @@ public class Util
|
||||
notleading = true;
|
||||
}
|
||||
if (pos >= len)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
else
|
||||
{
|
||||
try
|
||||
@@ -555,44 +514,38 @@ public class Util
|
||||
while (true);
|
||||
|
||||
if (notleading)
|
||||
{
|
||||
return sb.toString();
|
||||
}
|
||||
return sb.toString();
|
||||
return "0";
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>The inverse function of the above.</p>
|
||||
*
|
||||
* <p>Converts a string representing the encoding of some bytes in Base-64
|
||||
* to their original form.</p>
|
||||
*
|
||||
* The inverse function of the above.
|
||||
* <p>
|
||||
* Converts a string representing the encoding of some bytes in Base-64 to
|
||||
* their original form.
|
||||
*
|
||||
* @param str the Base-64 encoded representation of some byte(s).
|
||||
* @return the bytes represented by the <code>str</code>.
|
||||
* @throws NumberFormatException if <code>str</code> is <code>null</code>, or
|
||||
* <code>str</code> contains an illegal Base-64 character.
|
||||
* @throws NumberFormatException if <code>str</code> is <code>null</code>,
|
||||
* or <code>str</code> contains an illegal Base-64 character.
|
||||
* @see #toBase64(byte[])
|
||||
*/
|
||||
public static final byte[] fromBase64(String str)
|
||||
{
|
||||
int len = str.length();
|
||||
if (len == 0)
|
||||
{
|
||||
throw new NumberFormatException("Empty string");
|
||||
}
|
||||
throw new NumberFormatException("Empty string");
|
||||
byte[] a = new byte[len + 1];
|
||||
int i, j;
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException x)
|
||||
{
|
||||
throw new NumberFormatException("Illegal character at #" + i);
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
|
||||
}
|
||||
catch (ArrayIndexOutOfBoundsException x)
|
||||
{
|
||||
throw new NumberFormatException("Illegal character at #" + i);
|
||||
}
|
||||
i = len - 1;
|
||||
j = len;
|
||||
try
|
||||
@@ -601,42 +554,31 @@ public class Util
|
||||
{
|
||||
a[j] = a[i];
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] & 0x03) << 6;
|
||||
j--;
|
||||
a[j] = (byte) ((a[i] & 0x3C) >>> 2);
|
||||
a[j] = (byte)((a[i] & 0x3C) >>> 2);
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] & 0x0F) << 4;
|
||||
j--;
|
||||
a[j] = (byte) ((a[i] & 0x30) >>> 4);
|
||||
a[j] = (byte)((a[i] & 0x30) >>> 4);
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
a[j] |= (a[i] << 2);
|
||||
j--;
|
||||
a[j] = 0;
|
||||
if (--i < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{ // ignore leading 0-bytes
|
||||
while (a[j] == 0)
|
||||
{
|
||||
j++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
catch (Exception x)
|
||||
{
|
||||
@@ -650,36 +592,31 @@ public class Util
|
||||
// BigInteger utilities ----------------------------------------------------
|
||||
|
||||
/**
|
||||
* <p>Treats the input as the MSB representation of a number, and discards
|
||||
* Treats the input as the MSB representation of a number, and discards
|
||||
* leading zero elements. For efficiency, the input is simply returned if no
|
||||
* leading zeroes are found.</p>
|
||||
*
|
||||
* leading zeroes are found.
|
||||
*
|
||||
* @param n the {@link BigInteger} to trim.
|
||||
* @return the byte array representation of the designated {@link BigInteger}
|
||||
* with no leading 0-bytes.
|
||||
* with no leading 0-bytes.
|
||||
*/
|
||||
public static final byte[] trim(BigInteger n)
|
||||
{
|
||||
byte[] in = n.toByteArray();
|
||||
if (in.length == 0 || in[0] != 0)
|
||||
{
|
||||
return in;
|
||||
}
|
||||
return in;
|
||||
int len = in.length;
|
||||
int i = 1;
|
||||
while (in[i] == 0 && i < len)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
++i;
|
||||
byte[] result = new byte[len - i];
|
||||
System.arraycopy(in, i, result, 0, len - i);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Returns a hexadecimal dump of the trimmed bytes of a {@link BigInteger}.
|
||||
* </p>
|
||||
*
|
||||
* Returns a hexadecimal dump of the trimmed bytes of a {@link BigInteger}.
|
||||
*
|
||||
* @param x the {@link BigInteger} to display.
|
||||
* @return the string representation of the designated {@link BigInteger}.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user