Skip to content

Commit a00269a

Browse files
committed
新增 IO类和Toast 工具类
1 parent 4d279b9 commit a00269a

File tree

4 files changed

+514
-4
lines changed

4 files changed

+514
-4
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ allprojects {
2323

2424
```
2525
dependencies {
26-
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.3'
26+
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.4'
2727
}
2828
```
2929

@@ -51,6 +51,9 @@ dependencies {
5151
| TouchEventUtil | Android Touch事件打印辅助工具类 | [TouchEventUtil][19] | |
5252
| WeakRefHander | 弱引用 handler 防止内存泄露 | [WeakRefHander][23] | |
5353
| SecretUtils | 3DES 加密/解密 | [SecretUtils][24] | |
54+
| ToastUtils | Toast工具类(需要Utils.init(context)) | [ToastUtils][25] | |
55+
| IOUtil | IOUtil (文件操作工具) | [IOUtil][26] | |
56+
5457

5558

5659
### 2.Android 数据库处理工具类
@@ -62,7 +65,7 @@ dependencies {
6265

6366
```
6467
dependencies {
65-
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.3'
68+
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.4'
6669
}
6770
```
6871

@@ -76,7 +79,7 @@ dependencies {
7679

7780
```
7881
dependencies {
79-
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.3'
82+
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.4'
8083
}
8184
8285
```
@@ -124,4 +127,6 @@ dependencies {
124127
[21]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ImageTakerHelper.java
125128
[22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java
126129
[23]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/WeakRefHander.java
127-
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java
130+
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java
131+
[25]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ToastUtils.java
132+
[26]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/IOUtil.java
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright 2017 [AllenCoderr]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.allen.apputils;
18+
19+
import android.database.Cursor;
20+
import android.text.TextUtils;
21+
22+
import java.io.BufferedInputStream;
23+
import java.io.BufferedOutputStream;
24+
import java.io.ByteArrayOutputStream;
25+
import java.io.Closeable;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.io.InputStreamReader;
30+
import java.io.OutputStream;
31+
import java.io.OutputStreamWriter;
32+
import java.io.Reader;
33+
import java.io.Writer;
34+
35+
public class IOUtil {
36+
37+
private IOUtil() {
38+
}
39+
40+
public static void closeQuietly(Closeable closeable) {
41+
if (closeable != null) {
42+
try {
43+
closeable.close();
44+
} catch (Throwable ignored) {
45+
MLog.d(ignored.getMessage(), ignored);
46+
}
47+
}
48+
}
49+
50+
public static void closeQuietly(Cursor cursor) {
51+
if (cursor != null) {
52+
try {
53+
cursor.close();
54+
} catch (Throwable ignored) {
55+
MLog.d(ignored.getMessage(), ignored);
56+
}
57+
}
58+
}
59+
60+
public static byte[] readBytes(InputStream in) throws IOException {
61+
if (!(in instanceof BufferedInputStream)) {
62+
in = new BufferedInputStream(in);
63+
}
64+
ByteArrayOutputStream out = null;
65+
try {
66+
out = new ByteArrayOutputStream();
67+
byte[] buf = new byte[1024];
68+
int len;
69+
while ((len = in.read(buf)) != -1) {
70+
out.write(buf, 0, len);
71+
}
72+
return out.toByteArray();
73+
} finally {
74+
closeQuietly(out);
75+
}
76+
}
77+
78+
public static byte[] readBytes(InputStream in, long skip, int size) throws IOException {
79+
byte[] result = null;
80+
if (skip > 0) {
81+
long skipped = 0;
82+
while (skip > 0 && (skipped = in.skip(skip)) > 0) {
83+
skip -= skipped;
84+
}
85+
}
86+
result = new byte[size];
87+
for (int i = 0; i < size; i++) {
88+
result[i] = (byte) in.read();
89+
}
90+
return result;
91+
}
92+
93+
public static String readStr(InputStream in) throws IOException {
94+
return readStr(in, "UTF-8");
95+
}
96+
97+
public static String readStr(InputStream in, String charset) throws IOException {
98+
if (TextUtils.isEmpty(charset)) charset = "UTF-8";
99+
100+
if (!(in instanceof BufferedInputStream)) {
101+
in = new BufferedInputStream(in);
102+
}
103+
Reader reader = new InputStreamReader(in, charset);
104+
StringBuilder sb = new StringBuilder();
105+
char[] buf = new char[1024];
106+
int len;
107+
while ((len = reader.read(buf)) >= 0) {
108+
sb.append(buf, 0, len);
109+
}
110+
return sb.toString();
111+
}
112+
113+
public static void writeStr(OutputStream out, String str) throws IOException {
114+
writeStr(out, str, "UTF-8");
115+
}
116+
117+
public static void writeStr(OutputStream out, String str, String charset) throws IOException {
118+
if (TextUtils.isEmpty(charset)) charset = "UTF-8";
119+
120+
Writer writer = new OutputStreamWriter(out, charset);
121+
writer.write(str);
122+
writer.flush();
123+
}
124+
125+
public static void copy(InputStream in, OutputStream out) throws IOException {
126+
if (!(in instanceof BufferedInputStream)) {
127+
in = new BufferedInputStream(in);
128+
}
129+
if (!(out instanceof BufferedOutputStream)) {
130+
out = new BufferedOutputStream(out);
131+
}
132+
int len = 0;
133+
byte[] buffer = new byte[1024];
134+
while ((len = in.read(buffer)) != -1) {
135+
out.write(buffer, 0, len);
136+
}
137+
out.flush();
138+
}
139+
140+
public static boolean deleteFileOrDir(File path) {
141+
if (path == null || !path.exists()) {
142+
return true;
143+
}
144+
if (path.isFile()) {
145+
return path.delete();
146+
}
147+
File[] files = path.listFiles();
148+
if (files != null) {
149+
for (File file : files) {
150+
deleteFileOrDir(file);
151+
}
152+
}
153+
return path.delete();
154+
}
155+
}

0 commit comments

Comments
 (0)