Skip to content

Commit

Permalink
新增 IO类和Toast 工具类
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenCoder committed Apr 13, 2017
1 parent 4d279b9 commit a00269a
Show file tree
Hide file tree
Showing 4 changed files with 514 additions and 4 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ allprojects {

```
dependencies {
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.3'
compile 'com.github.AllenCoder.SuperUtils:apputils:1.0.4'
}
```

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



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

```
dependencies {
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.3'
compile 'com.github.AllenCoder.SuperUtils:dbutils:1.0.4'
}
```

Expand All @@ -76,7 +79,7 @@ dependencies {

```
dependencies {
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.3'
compile 'com.github.AllenCoder.SuperUtils:mediautil:1.0.4'
}
```
Expand Down Expand Up @@ -124,4 +127,6 @@ dependencies {
[21]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ImageTakerHelper.java
[22]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/Utils.java
[23]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/WeakRefHander.java
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java
[24]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/SecretUtils.java
[25]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/ToastUtils.java
[26]: https://github.com/AllenCoder/SuperUtils/blob/master/mediautil/src/main/java/com/allen/mediautil/IOUtil.java
155 changes: 155 additions & 0 deletions apputils/src/main/java/com/allen/apputils/IOUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright 2017 [AllenCoderr]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.allen.apputils;

import android.database.Cursor;
import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

public class IOUtil {

private IOUtil() {
}

public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Throwable ignored) {
MLog.d(ignored.getMessage(), ignored);
}
}
}

public static void closeQuietly(Cursor cursor) {
if (cursor != null) {
try {
cursor.close();
} catch (Throwable ignored) {
MLog.d(ignored.getMessage(), ignored);
}
}
}

public static byte[] readBytes(InputStream in) throws IOException {
if (!(in instanceof BufferedInputStream)) {
in = new BufferedInputStream(in);
}
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
return out.toByteArray();
} finally {
closeQuietly(out);
}
}

public static byte[] readBytes(InputStream in, long skip, int size) throws IOException {
byte[] result = null;
if (skip > 0) {
long skipped = 0;
while (skip > 0 && (skipped = in.skip(skip)) > 0) {
skip -= skipped;
}
}
result = new byte[size];
for (int i = 0; i < size; i++) {
result[i] = (byte) in.read();
}
return result;
}

public static String readStr(InputStream in) throws IOException {
return readStr(in, "UTF-8");
}

public static String readStr(InputStream in, String charset) throws IOException {
if (TextUtils.isEmpty(charset)) charset = "UTF-8";

if (!(in instanceof BufferedInputStream)) {
in = new BufferedInputStream(in);
}
Reader reader = new InputStreamReader(in, charset);
StringBuilder sb = new StringBuilder();
char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) >= 0) {
sb.append(buf, 0, len);
}
return sb.toString();
}

public static void writeStr(OutputStream out, String str) throws IOException {
writeStr(out, str, "UTF-8");
}

public static void writeStr(OutputStream out, String str, String charset) throws IOException {
if (TextUtils.isEmpty(charset)) charset = "UTF-8";

Writer writer = new OutputStreamWriter(out, charset);
writer.write(str);
writer.flush();
}

public static void copy(InputStream in, OutputStream out) throws IOException {
if (!(in instanceof BufferedInputStream)) {
in = new BufferedInputStream(in);
}
if (!(out instanceof BufferedOutputStream)) {
out = new BufferedOutputStream(out);
}
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
}

public static boolean deleteFileOrDir(File path) {
if (path == null || !path.exists()) {
return true;
}
if (path.isFile()) {
return path.delete();
}
File[] files = path.listFiles();
if (files != null) {
for (File file : files) {
deleteFileOrDir(file);
}
}
return path.delete();
}
}
Loading

0 comments on commit a00269a

Please sign in to comment.