Example usage for java.math BigInteger toString

List of usage examples for java.math BigInteger toString

Introduction

In this page you can find the example usage for java.math BigInteger toString.

Prototype

public String toString(int radix) 

Source Link

Document

Returns the String representation of this BigInteger in the given radix.

Usage

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Fetches and stores a .jar file from a repo url/file-url, doing an md5sum check too.
 *///from w w w .j  a  v  a 2s .c  o  m
private static void fetchJar(String url, String jarFile, String md5sum) throws SQLException {
    BufferedInputStream in = null;
    FileOutputStream out = null;
    try {
        String outfile = FarragoProperties.instance().expandProperties("${FARRAGO_HOME}/plugin/" + jarFile);
        in = new BufferedInputStream(new URL(url).openStream());
        out = new FileOutputStream(outfile);
        final int block_size = 1 << 18; // 256 kb
        byte data[] = new byte[block_size];
        int bytes;
        // for verifying md5
        MessageDigest dig = MessageDigest.getInstance("MD5");
        while ((bytes = in.read(data, 0, block_size)) != -1) {
            out.write(data, 0, bytes);
            dig.update(data, 0, bytes);
        }
        in.close();
        in = null;
        out.close();
        out = null;

        java.math.BigInteger biggy = new java.math.BigInteger(1, dig.digest());
        String md5check = biggy.toString(16);
        if (!md5sum.equals(md5check)) {
            throw new SQLException("Jar could not be fetched due to data mismatch.\n" + "Expected md5sum: "
                    + md5sum + "; got " + md5check);
        }
    } catch (Throwable e) {
        throw new SQLException(e);
    } finally {
        try {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        } catch (IOException e) {
            //pass
        }
    }
}

From source file:org.objectspace.rfid.elatec.ElatecRFID.java

public static String longToHex(long l) {
    BigInteger b = BigInteger.valueOf(l);
    if (b.signum() < 0)
        b = b.add(TWO_64);//ww w.ja  va 2s .  c  om
    String str = b.toString(16);
    if (str.length() == 16)
        str = str.substring(8);
    while (str.length() < 8)
        str = "0" + str;
    return str;
}

From source file:org.objectspace.rfid.elatec.ElatecRFID.java

public static String intToHex(int i) {
    BigInteger b = BigInteger.valueOf(i);
    if (b.signum() < 0)
        b = b.add(TWO_64);//w w  w  .j  a  v a2 s.co m
    String str = b.toString(16);
    if (str.length() == 16)
        str = str.substring(12);
    while (str.length() < 4)
        str = "0" + str;
    return str;
}

From source file:org.solmix.commons.util.Files.java

/**
 * ?MD5/* w ww  . j  a va 2 s .c  o m*/
 *
 * @param file
 * @return
 */
public static String getMd5ByFile(File file) {
    String value = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(byteBuffer);
        BigInteger bi = new BigInteger(1, md5.digest());
        value = bi.toString(16);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != in) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return value;
}

From source file:org.ccnx.ccn.impl.support.DataUtils.java

/**
 * Used to print non ASCII components for logging, etc.
 *
 * @param bytes/*from  w w  w.  jav a 2s  . c  om*/
 * @return the data as a BigInteger String
 */
public static String printBytes(byte[] bytes) {
    if (bytes == null) {
        return "";
    }
    BigInteger bi = new BigInteger(1, bytes);
    return bi.toString(SystemConfiguration.DEBUG_RADIX);
}

From source file:org.ccnx.ccn.impl.support.DataUtils.java

/**
 * Used to print components to be interpreted as hexadecimal such as segments
 * @param bytes//from w  ww  .  j a va2s.co  m
 * @return the data as a Hexadecimal String
 */
public static String printHexBytes(byte[] bytes) {
    if ((null == bytes) || (bytes.length == 0)) {
        return "<empty>";
    }
    BigInteger bi = new BigInteger(1, bytes);
    return bi.toString(16);
}

From source file:org.andrewberman.sync.InheritMe.java

public static String getMd5(File f) {
    try {//from   w w w  . ja  v a2  s  .  com
        MessageDigest complete = MessageDigest.getInstance("MD5");
        InputStream fis = new FileInputStream(f);
        byte[] buffer = new byte[1024];
        int numRead;
        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
        fis.close();
        byte[] digest = complete.digest();
        BigInteger bigInt = new BigInteger(1, digest);
        return new String(bigInt.toString(16));
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get the MD5 hash of a file as a 32 character hex string.
 *
 * @param inputStream The input stream/*w  ww . j a va 2s  .  c om*/
 * @return The MD5 hash of the file or null on failure
 */
@Nullable
private static String getMD5Hash(@NonNull InputStream inputStream) {
    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        final byte[] buffer = new byte[8192];
        int read;
        while ((read = inputStream.read(buffer)) > 0) {
            messageDigest.update(buffer, 0, read);
        }
        final BigInteger digest = new BigInteger(1, messageDigest.digest());
        return String.format("%32s", digest.toString(16)).replace(" ", "0");
    } catch (NoSuchAlgorithmException | IOException e) {
        Log.e(TAG, "Failed to generate MD5 hash", e);
    }

    return null;
}

From source file:com.livinglogic.ul4.FunctionFormat.java

public static String call(BigInteger obj, String formatString, Locale locale) {
    IntegerFormat format = new IntegerFormat(formatString);

    if (locale == null)
        locale = Locale.ENGLISH;/*from   w  ww. j  a  v a  2 s  . c o  m*/

    String output = null;

    boolean neg = obj.signum() < 0;
    if (neg)
        obj = obj.negate();

    switch (format.type) {
    case 'b':
        output = obj.toString(2);
        break;
    case 'c':
        if (neg || obj.compareTo(new BigInteger("65535")) > 0)
            throw new RuntimeException("value out of bounds for c format");
        output = Character.toString((char) obj.intValue());
        break;
    case 'd':
        output = obj.toString();
        break;
    case 'o':
        output = obj.toString(8);
        break;
    case 'x':
        output = obj.toString(16);
        break;
    case 'X':
        output = obj.toString(16).toUpperCase();
        break;
    case 'n':
        // FIXME: locale formatting
        output = obj.toString();
        break;
    }
    return formatIntegerString(output, neg, format);
}

From source file:com.jungle.base.utils.MiscUtils.java

public static String getHash(byte[] value) {
    try {/*from   w  w w.  j  a  va2 s .  com*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(value);
        BigInteger bi = new BigInteger(md5.digest()).abs();
        return bi.toString(Character.MAX_RADIX);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return serializationBytesToHex(value);
}