Android 工具类

该文为个人学习笔记,如有错误,烦请大佬指出~~~
说明:进行文件UUID对比时,请注意大小写问题。

一: Android 获取 Assets文件,并转换Json

/**
     * 获取asset 中文件
     *
     * @param context
     * @param fileName
     * @return
     */
    public static  T getAssetObject(Context context, String fileName, Class t) {
        String result = "";
        String line = "";
        try {
            final InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            final BufferedReader bufReader = new BufferedReader(inputReader);
            while ((line = bufReader.readLine()) != null)
                result += line;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Gson().fromJson(result, t);
    }

二: Kotlin之获取文件UUID(SHA+MD5)

     class FileUUIDUtils {
        companion object {
                /**
                 * 获取文件的UUID
                 */
                public fun _getFileUUID(file: File?): String {
                    var uuidStrBuff: StringBuffer = StringBuffer()
                    if (null != file) {
                        uuidStrBuff.append(_getSHAOrMD5(file, UUIDConstants.UUID_MD5))
                        uuidStrBuff.append("%mic%")
                        uuidStrBuff.append(_getSHAOrMD5(file, UUIDConstants.UUID_SHA))
                    }
                    return uuidStrBuff.toString()
                }

                /**
                 * 获取文件的SHA
                 */
                private fun _getSHAOrMD5(file: File, @UUIDConstants.UUID_TAG tag: Int): String {
                    var shaOrMd5: String = ""
                    var fis: FileInputStream? = null
                    val buffer = ByteArray(8192)
                    var length: Int = -1
                    var md: MessageDigest
                    var algorithm: String = "MD5"
                    try {
                        when (tag) {
                            UUIDConstants.UUID_MD5 -> {
                                algorithm = "MD5"
                            }
                            UUIDConstants.UUID_SHA -> {
                                algorithm = "SHA"
                            }
                        }
                        md = MessageDigest.getInstance(algorithm)
                        fis = FileInputStream(file)
                        while (true) {
                            length = fis.read(buffer)
                            if (length == -1) {
                                break
                            } else {
                                md.update(buffer, 0, length)
                            }
                        }
                        //digest() 只能调用一次~~~ 相关数据会被重置成初始状态
                        shaOrMd5 = BigInteger(1, md.digest()).toString(16)
                    } catch (e: NoSuchAlgorithmException) {

                    } catch (e: FileNotFoundException) {
        
                    } finally {
                        try {
                            fis?.close()
                        } catch (e: IOException) {

                        }
                    }
                    return shaOrMd5
                }
            }
        }

注解类代码:

class UUIDConstants {

    companion object {
        const val UUID_SHA = 1L

        const val UUID_MD5 = 2L
    }

    @IntDef(UUID_SHA, UUID_MD5)
    annotation class ServiceType

}

你可能感兴趣的:(Android 工具类)