package com.fantasy.blogdemo.utils; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PixelFormat; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.annotation.IntDef; import android.view.View; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * 数据转换工具类 * <pre> * author : Fantasy * version : 1.0, 2019-07-11 * since : 1.0, 2019-07-11 * </pre> */ public class ConvertUtils { public static final int BYTE = 1; public static final int KB = 1024; public static final int MB = 1048576; public static final int GB = 1073741824; public static final int MSEC = 1; public static final int SEC = 1000; public static final int MIN = 60000; public static final int HOUR = 3600000; public static final int DAY = 86400000; @IntDef({BYTE, KB, MB, GB}) @Retention(RetentionPolicy.SOURCE) @interface memoryUnit { } @IntDef({MSEC, SEC, MIN, HOUR, DAY}) @Retention(RetentionPolicy.SOURCE) @interface timeUnit { } private ConvertUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; /** * Bytes to bits. * * @param bytes The bytes. * @return bits */ public static String bytesToBits(byte[] bytes) { if (bytes == null || bytes.length == 0) return ""; StringBuilder sb = new StringBuilder(); for (byte aByte : bytes) { for (int j = 7; j >= 0; --j) { sb.append(((aByte >> j) & 0x01) == 0 ? '0' : '1'); } } return sb.toString(); } /** * Bits to bytes. * * @param bits The bits. * @return bytes */ public static byte[] bitsToBytes(String bits) { int lenMod = bits.length() % 8; int byteLen = bits.length() / 8; // add "0" until length to 8 times if (lenMod != 0) { for (int i = lenMod; i < 8; i++) { bits = "0" + bits; } byteLen++; } byte[] bytes = new byte[byteLen]; for (int i = 0; i < byteLen; ++i) { for (int j = 0; j < 8; ++j) { bytes[i] <<= 1; bytes[i] |= bits.charAt(i * 8 + j) - '0'; } } return bytes; } /** * Bytes to chars. * * @param bytes The bytes. * @return chars */ public static char[] bytesToChars(byte[] bytes) { if (bytes == null) return null; int len = bytes.length; if (len <= 0) return null; char[] chars = new char[len]; for (int i = 0; i < len; i++) { chars[i] = (char) (bytes[i] & 0xff); } return chars; } /** * Chars to bytes. * * @param chars The chars. * @return bytes */ public static byte[] charsToBytes(char[] chars) { if (chars == null || chars.length <= 0) return null; int len = chars.length; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = (byte) (chars[i]); } return bytes; } /** * Bytes to hex string. * <p>e.g. bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns "00A8"</p> * * @param bytes The bytes. * @return hex string */ public static String bytesToHexString(byte[] bytes) { if (bytes == null) return ""; int len = bytes.length; if (len <= 0) return ""; char[] ret = new char[len << 1]; for (int i = 0, j = 0; i < len; i++) { ret[j++] = HEX_DIGITS[bytes[i] >> 4 & 0x0f]; ret[j++] = HEX_DIGITS[bytes[i] & 0x0f]; } return new String(ret); } /** * Hex string to bytes. * <p>e.g. hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }</p> * * @param hexString The hex string. * @return the bytes */ public static byte[] hexStringToBytes(String hexString) { if (isSpace(hexString)) return null; int len = hexString.length(); if (len % 2 != 0) { hexString = "0" + hexString; len = len + 1; } char[] hexBytes = hexString.toUpperCase().toCharArray(); byte[] ret = new byte[len >> 1]; for (int i = 0; i < len; i += 2) { ret[i >> 1] = (byte) (hexToDec(hexBytes[i]) << 4 | hexToDec(hexBytes[i + 1])); } return ret; } /** * Hex to dec. * <p>e.g. hex2Dec('F') returns 16</p> * * @param hexChar The hex char. * @return the int */ private static int hexToDec(char hexChar) { if (hexChar >= '0' && hexChar <= '9') { return hexChar - '0'; } else if (hexChar >= 'A' && hexChar <= 'F') { return hexChar - 'A' + 10; } else { throw new IllegalArgumentException(); } } /** * Size of memory in unit to size of byte. * * @param memorySize Size of memory. * @param unit The unit of memory size. * <ul> * <li>{@link #BYTE}</li> * <li>{@link #KB}</li> * <li>{@link #MB}</li> * <li>{@link #GB}</li> * </ul> * @return size of byte */ public static long memorySizeToByte(long memorySize, @memoryUnit int unit) { if (memorySize < 0) return -1; return memorySize * unit; } /** * Size of byte to size of memory in unit. * * @param byteSize Size of byte. * @param unit The unit of memory size. * <ul> * <li>{@link #BYTE}</li> * <li>{@link #KB}</li> * <li>{@link #MB}</li> * <li>{@link #GB}</li> * </ul> * @return size of memory in unit */ public static double byteToMemorySize(long byteSize, @memoryUnit int unit) { if (byteSize < 0) return -1; return (double) byteSize / unit; } /** * Size of byte to fit size of memory. * <p>to three decimal places</p> * * @param byteSize Size of byte. * @return fit size of memory */ @SuppressLint("DefaultLocale") public static String byteToFitMemorySize(long byteSize) { if (byteSize < 0) { return "shouldn't be less than zero!"; } else if (byteSize < KB) { return String.format("%.3fB", (double) byteSize); } else if (byteSize < MB) { return String.format("%.3fKB", (double) byteSize / KB); } else if (byteSize < GB) { return String.format("%.3fMB", (double) byteSize / MB); } else { return String.format("%.3fGB", (double) byteSize / GB); } } /** * Time span in unit to milliseconds. * * @param timeSpan The time span. * @param unit The unit of time span. * <ul> * <li>{@link #MSEC}</li> * <li>{@link #SEC }</li> * <li>{@link #MIN }</li> * <li>{@link #HOUR}</li> * <li>{@link #DAY }</li> * </ul> * @return milliseconds */ public static long timeSpanToMillis(long timeSpan, @timeUnit int unit) { return timeSpan * unit; } /** * Milliseconds to time span in unit. * * @param millis The milliseconds. * @param unit The unit of time span. * <ul> * <li>{@link #MSEC}</li> * <li>{@link #SEC }</li> * <li>{@link #MIN }</li> * <li>{@link #HOUR}</li> * <li>{@link #DAY }</li> * </ul> * @return time span in unit */ public static long millisToTimeSpan(long millis, @timeUnit int unit) { return millis / unit; } /** * Milliseconds to fit time span. * * @param millis The milliseconds. * <p>millis <= 0, return null</p> * @param precision The precision of time span. * <ul> * <li>precision = 0, return null</li> * <li>precision = 1, return 天</li> * <li>precision = 2, return 天, 小时</li> * <li>precision = 3, return 天, 小时, 分钟</li> * <li>precision = 4, return 天, 小时, 分钟, 秒</li> * <li>precision >= 5,return 天, 小时, 分钟, 秒, 毫秒</li> * </ul> * @return fit time span */ @SuppressLint("DefaultLocale") public static String millisToFitTimeSpan(long millis, int precision) { if (millis <= 0 || precision <= 0) return null; StringBuilder sb = new StringBuilder(); String[] units = {"天", "小时", "分钟", "秒", "毫秒"}; int[] unitLen = {86400000, 3600000, 60000, 1000, 1}; precision = Math.min(precision, 5); for (int i = 0; i < precision; i++) { if (millis >= unitLen[i]) { long mode = millis / unitLen[i]; millis -= mode * unitLen[i]; sb.append(mode).append(units[i]); } } return sb.toString(); } /** * Input stream to output stream. * * @param is The input stream. * @return output stream */ public static ByteArrayOutputStream inputToOutputStream(InputStream is) { if (is == null) return null; try { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] b = new byte[KB]; int len; while ((len = is.read(b, 0, KB)) != -1) { os.write(b, 0, len); } return os; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Output stream to input stream. * * @param out The output stream. * @return input stream */ public ByteArrayInputStream outputToInputStream(OutputStream out) { if (out == null) return null; return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()); } /** * Input stream to bytes. * * @param is The input stream. * @return bytes */ public static byte[] inputStreamToBytes(InputStream is) { if (is == null) return null; return inputToOutputStream(is).toByteArray(); } /** * Bytes to input stream. * * @param bytes The bytes. * @return input stream */ public static InputStream bytesToInputStream(byte[] bytes) { if (bytes == null || bytes.length <= 0) return null; return new ByteArrayInputStream(bytes); } /** * Output stream to bytes. * * @param out The output stream. * @return bytes */ public static byte[] outputStreamToBytes(OutputStream out) { if (out == null) return null; return ((ByteArrayOutputStream) out).toByteArray(); } /** * Bytes to output stream. * * @param bytes The bytes. * @return output stream */ public static OutputStream bytesToOutputStream(byte[] bytes) { if (bytes == null || bytes.length <= 0) return null; ByteArrayOutputStream os = null; try { os = new ByteArrayOutputStream(); os.write(bytes); return os; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * Input stream to string. * * @param is The input stream. * @param charsetName The name of charset. * @return string */ public static String inputStreamToString(InputStream is, String charsetName) { if (is == null || isSpace(charsetName)) return ""; try { ByteArrayOutputStream baos = inputToOutputStream(is); if (baos == null) return ""; return baos.toString(charsetName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } /** * String to input stream. * * @param string The string. * @param charsetName The name of charset. * @return input stream */ public static InputStream stringToInputStream(String string, String charsetName) { if (string == null || isSpace(charsetName)) return null; try { return new ByteArrayInputStream(string.getBytes(charsetName)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } /** * Output stream to string. * * @param out The output stream. * @param charsetName The name of charset. * @return string */ public static String outputStreamToString(OutputStream out, String charsetName) { if (out == null || isSpace(charsetName)) return ""; try { return new String(outputStreamToBytes(out), charsetName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } /** * String to output stream. * * @param string The string. * @param charsetName The name of charset. * @return output stream */ public static OutputStream stringToOutputStream(String string, String charsetName) { if (string == null || isSpace(charsetName)) return null; try { return bytesToOutputStream(string.getBytes(charsetName)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } /** * Bitmap to bytes. * * @param bitmap The bitmap. * @param format The format of bitmap. * @return bytes */ public static byte[] bitmapToBytes(Bitmap bitmap, Bitmap.CompressFormat format) { if (bitmap == null) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(format, 100, baos); return baos.toByteArray(); } /** * Bytes to bitmap. * * @param bytes The bytes. * @return bitmap */ public static Bitmap bytesToBitmap(byte[] bytes) { return (bytes == null || bytes.length == 0) ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } /** * Drawable to bitmap. * * @param drawable The drawable. * @return bitmap */ public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } Bitmap bitmap; if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } /** * Bitmap to drawable. * * @param context The context. * @param bitmap The bitmap. * @return drawable */ public static Drawable bitmapToDrawable(Context context, Bitmap bitmap) { return bitmap == null ? null : new BitmapDrawable(context.getResources(), bitmap); } /** * Drawable to bytes. * * @param drawable The drawable. * @param format The format of bitmap. * @return bytes */ public static byte[] drawableToBytes(Drawable drawable, Bitmap.CompressFormat format) { return drawable == null ? null : bitmapToBytes(drawableToBitmap(drawable), format); } /** * Bytes to drawable. * * @param context The context. * @param bytes The bytes. * @return drawable */ public static Drawable bytesToDrawable(Context context, byte[] bytes) { return bytes == null ? null : bitmapToDrawable(context, bytesToBitmap(bytes)); } /** * View to bitmap. * * @param view The view. * @return bitmap */ public static Bitmap viewToBitmap(View view) { if (view == null) return null; Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(ret); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(canvas); } else { canvas.drawColor(Color.WHITE); } view.draw(canvas); return ret; } /** * Value of dp to value of px. * * @param dpValue The value of dp. * @return value of px */ public static int dpToPx(float dpValue) { float scale = Resources.getSystem().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * Value of px to value of dp. * * @param pxValue The value of px. * @return value of dp */ public static int pxToDp(float pxValue) { float scale = Resources.getSystem().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * Value of sp to value of px. * * @param spValue The value of sp. * @return value of px */ public static int spToPx(float spValue) { float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * Value of px to value of sp. * * @param pxValue The value of px. * @return value of sp */ public static int pxToSp(float pxValue) { float fontScale = Resources.getSystem().getDisplayMetrics().scaledDensity; return (int) (pxValue / fontScale + 0.5f); } private static boolean isSpace(String s) { if (s == null) return true; for (int i = 0, len = s.length(); i < len; ++i) { if (!Character.isWhitespace(s.charAt(i))) { return false; } } return true; } }