-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathIOUtil.java
More file actions
112 lines (109 loc) · 3.91 KB
/
IOUtil.java
File metadata and controls
112 lines (109 loc) · 3.91 KB
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
package common.base.utils;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 针对IO流的工具类
* <br/>
* 2015年12月23日-上午11:02:33
* @author lifei
*/
public class IOUtil {
/**
* 关闭输入输出流
* @param io
* @return
*/
public static boolean safeCloseIO(Closeable io){
if(io != null){
try {
io.close();
return true;
} catch (Exception e) {
CommonLog.e("info",io +" --> close occur " + e);
}
}
return false;
}
/**
* 从输入流中读取必要长度的字节
* 作用为:读取一段命令(数据)时,本来完整的数据为N个字节,但是并不是一次读取正好能读取到N个字节,
* 因此用该方法规避一次读取不完整的问题
* @param needLen 需要读取完整的字节数
* @param curInputStream
* @return 正确读取出的必须长度的字节数组 或者指定长度的空字节数据
* @throws IOException
*/
public static byte[] readNeedLenByte(int needLen,InputStream curInputStream) throws IOException{
byte [] back = new byte [needLen];
int pos = 0;
while( needLen > 0){
byte [] temp = new byte [needLen];
int len = curInputStream.read(temp);
if (len < 0) {
throw new IOException("inputStream read to the end");
}
needLen = needLen -len;
System.arraycopy(temp, 0, back, pos, len);
pos += len;
}
return back;
}
/**
* 从输入流中读取必要长度的字节
* 作用为:读取一段命令(数据)时,本来完整的数据为N个字节,但是并不是一次读取正好能读取到N个字节,
* 因此用该方法规避一次读取不完整的问题
* @param needLen 需要读取完整的字节数
* @param curInputStream
* @return 正确读取出的必须长度的字节数组 或者指定长度的空字节数据
* @throws IOException
*/
public static byte[] readNeedLenByte(int needLen,InputStream curInputStream,String tagOwner)
throws IOException{
byte [] back = new byte [needLen];
int pos = 0;
while( needLen > 0){
byte [] temp = new byte [needLen];
int len = curInputStream.read(temp);
CommonLog.w("info",tagOwner +"--> readNeedLenByte() needLen = " +needLen + " temp " +
"bytes = " + ByteUtil
.bytes2HexStr(temp) +" read len = " + len);
if (len < 0) {
throw new IOException("inputStream read to the end");
}
needLen = needLen -len;
System.arraycopy(temp, 0, back, pos, len);
pos += len;
}
return back;
}
public static void letCurThreadSleep(long timeMills){
try {
Thread.sleep(timeMills);
} catch (InterruptedException e) {
}
}
public static String readFile2String(File theFile,String charset) {
String result = "";
if (theFile != null && theFile.exists() && theFile.length() > 0) {
FileInputStream fis = null;
try {
fis = new FileInputStream(theFile);
int avalidSize = fis.available();
byte[] contentBytes = readNeedLenByte(avalidSize, fis);
if (charset == null) {
charset = "UTF-8";
}
result = new String(contentBytes, charset);
} catch (Exception e) {
e.printStackTrace();
}
finally {
safeCloseIO(fis);
}
}
return result;
}
}