提交 0b85765f 编写于 作者: C cmj

see 08/27 log

上级 3b9326b2
......@@ -18,14 +18,18 @@
> - 正则相关常量
> - **转换相关→[ConvertUtils.java][convert.java]→[Test][convert.test]**
> - 每1个byte转为2个hex字符 *bytes2HexString*
> - 每2个hex字符转为1个byte *hexString2Bytes*
> - byteArr转hexString *bytes2HexString*
> - hexString转byteArr *hexString2Bytes*
> - charArr转byteArr *chars2Bytes*
> - byteArr转charArr *bytes2Chars*
> - 将输入流转为字节数组 *inputStream2Bytes*
> - 将字节数组转为输入流 *bytes2InputStream*
> - 指定编码将输入流转为字符串 *inputStream2String*
> - 指定编码将字符串转为输入流 *string2InputStream*
> - inputStream转byteArr *inputStream2Bytes*
> - byteArr转inputStream *bytes2InputStream*
> - inputStream转string按编码 *inputStream2String*
> - string转inputStream按编码 *string2InputStream*
> - bitmap转byteArr *bitmap2Bytes*
> - byteArr转bitmap *bytes2Bitmap*
> - drawable转bitmap *drawable2Bitmap*
> - bitmap转drawable *bitmap2Drawable*
> - **设备相关→[DeviceUtils.java][device.java]**
> - 获取设备MAC地址 *getMacAddress*
......
package com.blankj.utilcode.utils;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
......@@ -21,24 +27,24 @@ public class ConvertUtils {
static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
/**
* 每1个byte转为2个hex字符
* byteArr转hexString
* <p>例如:</p>
* bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8
*
* @param src byte数组
* @param bytes byte数组
* @return 16进制大写字符串
*/
public static String bytes2HexString(byte[] src) {
char[] res = new char[src.length << 1];
for (int i = 0, j = 0; i < src.length; i++) {
res[j++] = hexDigits[src[i] >>> 4 & 0x0f];
res[j++] = hexDigits[src[i] & 0x0f];
public static String bytes2HexString(byte[] bytes) {
char[] res = new char[bytes.length << 1];
for (int i = 0, j = 0; i < bytes.length; i++) {
res[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
res[j++] = hexDigits[bytes[i] & 0x0f];
}
return new String(res);
}
/**
* 每2个hex字符转为1个byte
* hexString转byteArr
* <p>例如:</p>
* hexString2Bytes("00A8") returns { 0, (byte) 0xA8 }
*
......@@ -59,7 +65,7 @@ public class ConvertUtils {
}
/**
* 单个hex字符转为10进制
* hexChar转int
*
* @param hexChar hex单个字节
* @return 0..15
......@@ -105,7 +111,7 @@ public class ConvertUtils {
}
/**
* 将输入流转为字节数组
* inputStream转byteArr
*
* @param is 输入流
* @return 字节数组
......@@ -129,7 +135,7 @@ public class ConvertUtils {
}
/**
* 将字节数组转为输入流
* byteArr转inputStream
*
* @param bytes 字节数组
* @return 输入流
......@@ -139,7 +145,7 @@ public class ConvertUtils {
}
/**
* 指定编码将输入流转为字符串
* inputStream转string按编码
*
* @param is 输入流
* @param charsetName 编码格式
......@@ -170,7 +176,7 @@ public class ConvertUtils {
}
/**
* 指定编码将字符串转为输入流
* string转inputStream按编码
*
* @param string 字符串
* @param charsetName 编码格式
......@@ -185,4 +191,49 @@ public class ConvertUtils {
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 resources resources对象
* @param bitmap bitmap对象
* @return drawable对象
*/
public static Drawable bitmap2Drawable(Resources resources, Bitmap bitmap) {
return bitmap == null ? null : new BitmapDrawable(resources, bitmap);
}
}
......@@ -655,8 +655,7 @@ public class FileUtils {
e.printStackTrace();
return false;
} finally {
closeIO(is);
closeIO(os);
closeIO(is, os);
}
}
......@@ -961,12 +960,16 @@ public class FileUtils {
/**
* 关闭IO
*
* @param closeable closeable
* @param closeables closeable
*/
public static void closeIO(Closeable closeable) {
if (closeable == null) return;
public static void closeIO(Closeable... closeables) {
if (closeables == null) return;
try {
closeable.close();
for (Closeable closeable : closeables) {
if (closeable != null) {
closeable.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -5,6 +5,8 @@ import android.os.StatFs;
import java.io.File;
import static com.blankj.utilcode.utils.ConstUtils.*;
/**
* <pre>
* author: Blankj
......@@ -61,14 +63,14 @@ public class SDCardUtils {
* 计算SD卡的剩余空间
*
* @param unit <ul>
* <li>{@link ConstUtils#BYTE}: 字节</li>
* <li>{@link ConstUtils#KB} : 千字节</li>
* <li>{@link ConstUtils#MB} : 兆</li>
* <li>{@link ConstUtils#GB} : GB</li>
* <li>{@link MemoryUnit#BYTE}: 字节</li>
* <li>{@link MemoryUnit#KB} : 千字节</li>
* <li>{@link MemoryUnit#MB} : 兆</li>
* <li>{@link MemoryUnit#GB} : GB</li>
* </ul>
* @return 返回-1,说明SD卡不可用,否则返回SD卡剩余空间
*/
public static double getFreeSpace(int unit) {
public static double getFreeSpace(MemoryUnit unit) {
if (isSDCardEnable()) {
try {
StatFs stat = new StatFs(getSDCardPath());
......
......@@ -76,6 +76,7 @@ public class ThreadPoolUtils {
/**
* 在未来某个时间执行给定的命令链表
* <p>该命令可能在新的线程、已入池的线程或者正调用的线程中执行,这由 Executor 实现决定。</p>
*
* @param commands 命令链表
*/
......
package com.blankj.utilcode.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/8/27
* desc : 压缩相关工具类
* </pre>
*/
public class ZipUtils {
private ZipUtils() {
throw new UnsupportedOperationException("u can't fuck me...");
}
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), ConstUtils.MB));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.close();
}
/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(
zipFile), ConstUtils.MB));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.setComment(comment);
zipout.close();
}
/**
* 压缩文件
*
* @param resFile 需要压缩的文件(夹)
* @param zipout 压缩的目的文件
* @param rootpath 压缩的文件路径
* @throws FileNotFoundException 找不到文件时抛出
* @throws IOException 当压缩过程出错时抛出
*/
private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
throws FileNotFoundException, IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[ConstUtils.MB];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), ConstUtils.MB);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
/**
* 解压缩一个文件
*
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException 当解压缩过程出错时抛出
*/
public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[ConstUtils.MB];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}
/**
* 解压文件名包含传入文字的文件
*
* @param zipFile 压缩文件
* @param folderPath 目标文件夹
* @param nameContains 传入的文件匹配名
* @throws ZipException 压缩格式有误时抛出
* @throws IOException IO错误时抛出
*/
public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
String nameContains) throws ZipException, IOException {
ArrayList<File> fileList = new ArrayList<File>();
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdir();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.getName().contains(nameContains)) {
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "GB2312");
// str.getBytes("GB2312"),"8859_1" 输出
// str.getBytes("8859_1"),"GB2312" 输入
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[ConstUtils.MB];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
return fileList;
}
/**
* 获得压缩文件内文件列表
*
* @param zipFile 压缩文件
* @return 压缩文件内文件名称
* @throws ZipException 压缩文件格式有误时抛出
* @throws IOException 当解压缩过程出错时抛出
*/
public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException {
ArrayList<String> entryNames = new ArrayList<String>();
Enumeration<?> entries = getEntriesEnumeration(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
}
return entryNames;
}
/**
* 获得压缩文件内压缩文件对象以取得其属性
*
* @param zipFile 压缩文件
* @return 返回一个压缩文件列表
* @throws ZipException 压缩文件格式有误时抛出
* @throws IOException IO操作有误时抛出
*/
public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException,
IOException {
ZipFile zf = new ZipFile(zipFile);
return zf.entries();
}
/**
* 取得压缩文件对象的注释
*
* @param entry 压缩文件对象
* @return 压缩文件对象的注释
* @throws UnsupportedEncodingException
*/
public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {
return new String(entry.getComment().getBytes("GB2312"), "8859_1");
}
/**
* 取得压缩文件对象的名称
*
* @param entry 压缩文件对象
* @return 压缩文件对象的名称
* @throws UnsupportedEncodingException
*/
public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
return new String(entry.getName().getBytes("GB2312"), "8859_1");
}
}
......@@ -101,24 +101,24 @@
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform (2)" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="TEST" name="maven-ant-tasks-2.1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="backport-util-concurrent-3.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-profile-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xercesMinimal-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="ant-1.8.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xercesMinimal-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-settings-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-library-1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="ant-launcher-1.8.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-commons-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="accessibility-test-framework-2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-commons-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="ant-launcher-1.8.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xpp3_min-1.1.4c" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="nekohtml-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-http-lightweight-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="nekohtml-1.9.6.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-file-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-error-diagnostics-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="asm-5.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-repository-metadata-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="wagon-provider-api-1.0-beta-6" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="xstream-1.4.8" level="project" />
......@@ -143,13 +143,13 @@
<orderEntry type="library" exported="" scope="TEST" name="robolectric-resources-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="protobuf-java-2.6.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-project-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="vtd-xml-2.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="hamcrest-core-1.3" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-container-default-1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="vtd-xml-2.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="junit-4.12" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="classworlds-1.1-alpha-2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-container-default-1.0-alpha-9-stable-1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="shadows-core-v23-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="maven-artifact-manager-2.2.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="plexus-interpolation-1.11" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="shadows-core-v23-3.1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="classworlds-1.1-alpha-2" level="project" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册