-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConvertUtils.java
634 lines (592 loc) · 18.8 KB
/
ConvertUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package com.dds.common.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.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.dds.common.io.CloseUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
/**
* <pre>
* time : 2016/8/13
* desc : 转换相关工具类
* </pre>
*/
public class ConvertUtils {
private ConvertUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* byteArr转hexString
* <p>例如:</p>
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
*
* @param bytes 字节数组
* @return 16进制大写字符串
*/
public static String bytes2HexString(byte[] bytes) {
if (bytes == null) return null;
int len = bytes.length;
if (len <= 0) return null;
char[] ret = new char[len << 1];
for (int i = 0, j = 0; i < len; i++) {
ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
ret[j++] = hexDigits[bytes[i] & 0x0f];
}
return new String(ret);
}
/**
* hexString转byteArr
* <p>例如:</p>
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
*
* @param hexString 十六进制字符串
* @return 字节数组
*/
public static byte[] hexString2Bytes(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) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1]));
}
return ret;
}
/**
* hexChar转int
*
* @param hexChar hex单个字节
* @return 0..15
*/
private static int hex2Dec(char hexChar) {
if (hexChar >= '0' && hexChar <= '9') {
return hexChar - '0';
} else if (hexChar >= 'A' && hexChar <= 'F') {
return hexChar - 'A' + 10;
} else {
throw new IllegalArgumentException();
}
}
/**
* charArr转byteArr
*
* @param chars 字符数组
* @return 字节数组
*/
public static byte[] chars2Bytes(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;
}
/**
* byteArr转charArr
*
* @param bytes 字节数组
* @return 字符数组
*/
public static char[] bytes2Chars(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;
}
/**
* 以unit为单位的内存大小转字节数
*
* @param memorySize 大小
* @param unit 单位类型
* <ul>
* <li>{@link ConstUtils.MemoryUnit#BYTE}: 字节</li>
* <li>{@link ConstUtils.MemoryUnit#KB} : 千字节</li>
* <li>{@link ConstUtils.MemoryUnit#MB} : 兆</li>
* <li>{@link ConstUtils.MemoryUnit#GB} : GB</li>
* </ul>
* @return 字节数
*/
public static long memorySize2Byte(long memorySize, ConstUtils.MemoryUnit unit) {
if (memorySize < 0) return -1;
switch (unit) {
default:
case BYTE:
return memorySize;
case KB:
return memorySize * ConstUtils.KB;
case MB:
return memorySize * ConstUtils.MB;
case GB:
return memorySize * ConstUtils.GB;
}
}
/**
* 字节数转以unit为单位的内存大小
*
* @param byteNum 字节数
* @param unit 单位类型
* <ul>
* <li>{@link ConstUtils.MemoryUnit#BYTE}: 字节</li>
* <li>{@link ConstUtils.MemoryUnit#KB} : 千字节</li>
* <li>{@link ConstUtils.MemoryUnit#MB} : 兆</li>
* <li>{@link ConstUtils.MemoryUnit#GB} : GB</li>
* </ul>
* @return 以unit为单位的size
*/
public static double byte2MemorySize(long byteNum, ConstUtils.MemoryUnit unit) {
if (byteNum < 0) return -1;
switch (unit) {
default:
case BYTE:
return (double) byteNum;
case KB:
return (double) byteNum / ConstUtils.KB;
case MB:
return (double) byteNum / ConstUtils.MB;
case GB:
return (double) byteNum / ConstUtils.GB;
}
}
/**
* 字节数转合适内存大小
* <p>保留3位小数</p>
*
* @param byteNum 字节数
* @return 合适内存大小
*/
@SuppressLint("DefaultLocale")
public static String byte2FitMemorySize(long byteNum) {
if (byteNum < 0) {
return "shouldn't be less than zero!";
} else if (byteNum < ConstUtils.KB) {
return String.format("%.3fB", byteNum + 0.0005);
} else if (byteNum < ConstUtils.MB) {
return String.format("%.3fKB", byteNum / ConstUtils.KB + 0.0005);
} else if (byteNum < ConstUtils.GB) {
return String.format("%.3fMB", byteNum / ConstUtils.MB + 0.0005);
} else {
return String.format("%.3fGB", byteNum / ConstUtils.GB + 0.0005);
}
}
/**
* 以unit为单位的时间长度转毫秒时间戳
*
* @param timeSpan 毫秒时间戳
* @param unit 单位类型
* <ul>
* <li>{@link ConstUtils.TimeUnit#MSEC}: 毫秒</li>
* <li>{@link ConstUtils.TimeUnit#SEC }: 秒</li>
* <li>{@link ConstUtils.TimeUnit#MIN }: 分</li>
* <li>{@link ConstUtils.TimeUnit#HOUR}: 小时</li>
* <li>{@link ConstUtils.TimeUnit#DAY }: 天</li>
* </ul>
* @return 毫秒时间戳
*/
public static long timeSpan2Millis(long timeSpan, ConstUtils.TimeUnit unit) {
switch (unit) {
default:
case MSEC:
return timeSpan;
case SEC:
return timeSpan * ConstUtils.SEC;
case MIN:
return timeSpan * ConstUtils.MIN;
case HOUR:
return timeSpan * ConstUtils.HOUR;
case DAY:
return timeSpan * ConstUtils.DAY;
}
}
/**
* 毫秒时间戳转以unit为单位的时间长度
*
* @param millis 毫秒时间戳
* @param unit 单位类型
* <ul>
* <li>{@link ConstUtils.TimeUnit#MSEC}: 毫秒</li>
* <li>{@link ConstUtils.TimeUnit#SEC }: 秒</li>
* <li>{@link ConstUtils.TimeUnit#MIN }: 分</li>
* <li>{@link ConstUtils.TimeUnit#HOUR}: 小时</li>
* <li>{@link ConstUtils.TimeUnit#DAY }: 天</li>
* </ul>
* @return 以unit为单位的时间长度
*/
public static long millis2TimeSpan(long millis, ConstUtils.TimeUnit unit) {
switch (unit) {
default:
case MSEC:
return millis;
case SEC:
return millis / ConstUtils.SEC;
case MIN:
return millis / ConstUtils.MIN;
case HOUR:
return millis / ConstUtils.HOUR;
case DAY:
return millis / ConstUtils.DAY;
}
}
/**
* 毫秒时间戳转合适时间长度
*
* @param millis 毫秒时间戳
* <p>小于等于0,返回null</p>
* @param precision 精度
* <ul>
* <li>precision = 0,返回null</li>
* <li>precision = 1,返回天</li>
* <li>precision = 2,返回天和小时</li>
* <li>precision = 3,返回天、小时和分钟</li>
* <li>precision = 4,返回天、小时、分钟和秒</li>
* <li>precision >= 5,返回天、小时、分钟、秒和毫秒</li>
* </ul>
* @return 合适时间长度
*/
@SuppressLint("DefaultLocale")
public static String millis2FitTimeSpan(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();
}
/**
* bytes转bits
*
* @param bytes 字节数组
* @return bits
*/
public static String bytes2Bits(byte[] bytes) {
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转bytes
*
* @param bits 二进制
* @return bytes
*/
public static byte[] bits2Bytes(String bits) {
int lenMod = bits.length() % 8;
int byteLen = bits.length() / 8;
// 不是8的倍数前面补0
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;
}
/**
* inputStream转outputStream
*
* @param is 输入流
* @return outputStream子类
*/
public static ByteArrayOutputStream input2OutputStream(InputStream is) {
if (is == null) return null;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] b = new byte[ConstUtils.KB];
int len;
while ((len = is.read(b, 0, ConstUtils.KB)) != -1) {
os.write(b, 0, len);
}
return os;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
CloseUtils.closeIO(is);
}
}
/**
* outputStream转inputStream
*
* @param out 输出流
* @return inputStream子类
*/
public ByteArrayInputStream output2InputStream(OutputStream out) {
if (out == null) return null;
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
}
/**
* inputStream转byteArr
*
* @param is 输入流
* @return 字节数组
*/
public static byte[] inputStream2Bytes(InputStream is) {
if (is == null) return null;
return input2OutputStream(is).toByteArray();
}
/**
* byteArr转inputStream
*
* @param bytes 字节数组
* @return 输入流
*/
public static InputStream bytes2InputStream(byte[] bytes) {
if (bytes == null || bytes.length <= 0) return null;
return new ByteArrayInputStream(bytes);
}
/**
* outputStream转byteArr
*
* @param out 输出流
* @return 字节数组
*/
public static byte[] outputStream2Bytes(OutputStream out) {
if (out == null) return null;
return ((ByteArrayOutputStream) out).toByteArray();
}
/**
* outputStream转byteArr
*
* @param bytes 字节数组
* @return 字节数组
*/
public static OutputStream bytes2OutputStream(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 {
CloseUtils.closeIO(os);
}
}
/**
* inputStream转string按编码
*
* @param is 输入流
* @param charsetName 编码格式
* @return 字符串
*/
public static String inputStream2String(InputStream is, String charsetName) {
if (is == null || isSpace(charsetName)) return null;
try {
return new String(inputStream2Bytes(is), charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* string转inputStream按编码
*
* @param string 字符串
* @param charsetName 编码格式
* @return 输入流
*/
public static InputStream string2InputStream(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;
}
}
/**
* outputStream转string按编码
*
* @param out 输出流
* @param charsetName 编码格式
* @return 字符串
*/
public static String outputStream2String(OutputStream out, String charsetName) {
if (out == null || isSpace(charsetName)) return null;
try {
return new String(outputStream2Bytes(out), charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* string转outputStream按编码
*
* @param string 字符串
* @param charsetName 编码格式
* @return 输入流
*/
public static OutputStream string2OutputStream(String string, String charsetName) {
if (string == null || isSpace(charsetName)) return null;
try {
return bytes2OutputStream(string.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
/**
* bitmap转byteArr
*
* @param bitmap bitmap对象
* @param format 格式
* @return 字节数组
*/
public static byte[] bitmap2Bytes(Bitmap bitmap, Bitmap.CompressFormat format) {
if (bitmap == null) return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(format, 100, baos);
return baos.toByteArray();
}
/**
* byteArr转bitmap
*
* @param bytes 字节数组
* @return bitmap
*/
public static Bitmap bytes2Bitmap(byte[] bytes) {
return (bytes == null || bytes.length == 0) ? null : BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
/**
* drawable转bitmap
*
* @param drawable drawable对象
* @return bitmap
*/
public static Bitmap drawable2Bitmap(Drawable drawable) {
return drawable == null ? null : ((BitmapDrawable) drawable).getBitmap();
}
/**
* bitmap转drawable
*
* @param res resources对象
* @param bitmap bitmap对象
* @return drawable
*/
public static Drawable bitmap2Drawable(Resources res, Bitmap bitmap) {
return bitmap == null ? null : new BitmapDrawable(res, bitmap);
}
/**
* drawable转byteArr
*
* @param drawable drawable对象
* @param format 格式
* @return 字节数组
*/
public static byte[] drawable2Bytes(Drawable drawable, Bitmap.CompressFormat format) {
return drawable == null ? null : bitmap2Bytes(drawable2Bitmap(drawable), format);
}
/**
* byteArr转drawable
*
* @param res resources对象
* @param bytes 字节数组
* @return drawable
*/
public static Drawable bytes2Drawable(Resources res, byte[] bytes) {
return res == null ? null : bitmap2Drawable(res, bytes2Bitmap(bytes));
}
/**
* view转Bitmap
*
* @param view 视图
* @return bitmap
*/
public static Bitmap view2Bitmap(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;
}
/**
* dp转px
*
* @param dpValue dp值
* @return px值
*/
public static int dp2px(Context context,float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px转dp
*
* @param pxValue px值
* @return dp值
*/
public static int px2dp(Context context,float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* sp转px
*
* @param spValue sp值
* @return px值
*/
public static int sp2px(Context context,float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
/**
* px转sp
*
* @param pxValue px值
* @return sp值
*/
public static int px2sp(Context context,float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
private static boolean isSpace(String s) {
return (s == null || s.trim().length() == 0);
}
}