Skip to content

Commit b57cb41

Browse files
committed
see 08/24 log
1 parent d3221c2 commit b57cb41

File tree

5 files changed

+138
-51
lines changed

5 files changed

+138
-51
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/ConvertUtils.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.blankj.utilcode.utils;
22

3+
import java.io.BufferedReader;
34
import java.io.ByteArrayInputStream;
45
import java.io.ByteArrayOutputStream;
56
import java.io.IOException;
67
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.io.UnsupportedEncodingException;
710

811
/**
912
* <pre>
@@ -134,4 +137,52 @@ public static byte[] inputStream2Bytes(InputStream is) {
134137
public static InputStream bytes2InputStream(byte[] bytes) {
135138
return new ByteArrayInputStream(bytes);
136139
}
140+
141+
/**
142+
* 指定编码将输入流转为字符串
143+
*
144+
* @param is 输入流
145+
* @param charsetName 编码格式
146+
* @return 字符串
147+
*/
148+
public static String inputStream2String(InputStream is, String charsetName) {
149+
if (is == null) return null;
150+
BufferedReader reader;
151+
try {
152+
StringBuilder sb = new StringBuilder();
153+
if (StringUtils.isSpace(charsetName)) {
154+
reader = new BufferedReader(new InputStreamReader(is));
155+
} else {
156+
reader = new BufferedReader(new InputStreamReader(is, charsetName));
157+
}
158+
String line;
159+
while ((line = reader.readLine()) != null) {
160+
sb.append(line).append("\r\n");// windows系统换行为\r\n,Linux为\n
161+
}
162+
// 要去除最后的换行符
163+
return sb.delete(sb.length() - 2, sb.length()).toString();
164+
} catch (IOException e) {
165+
e.printStackTrace();
166+
return null;
167+
} finally {
168+
FileUtils.closeIO(is);
169+
}
170+
}
171+
172+
/**
173+
* 指定编码将字符串转为输入流
174+
*
175+
* @param string 字符串
176+
* @param charsetName 编码格式
177+
* @return 输入流
178+
*/
179+
public static InputStream string2InputStream(String string, String charsetName) {
180+
if (string == null || StringUtils.isSpace(charsetName)) return null;
181+
try {
182+
return new ByteArrayInputStream(string.getBytes(charsetName));
183+
} catch (UnsupportedEncodingException e) {
184+
e.printStackTrace();
185+
return null;
186+
}
187+
}
137188
}

utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -796,93 +796,104 @@ public static List<String> readFile2List(File file, String charsetName) {
796796
* 指定编码按行读取文件到List
797797
*
798798
* @param filePath 文件路径
799-
* @param start 需要读取的开始行数
799+
* @param st 需要读取的开始行数
800800
* @param end 需要读取的结束行数
801801
* @param charsetName 编码格式
802802
* @return 包含制定行的list
803803
*/
804-
public static List<String> readFile2List(String filePath, int start, int end, String
804+
public static List<String> readFile2List(String filePath, int st, int end, String
805805
charsetName) {
806-
return readFile2List(getFileByPath(filePath), start, end, charsetName);
806+
return readFile2List(getFileByPath(filePath), st, end, charsetName);
807807
}
808808

809809
/**
810810
* 指定编码按行读取文件到List
811811
*
812812
* @param file 文件
813-
* @param start 需要读取的开始行数
813+
* @param st 需要读取的开始行数
814814
* @param end 需要读取的结束行数
815815
* @param charsetName 编码格式
816-
* @return 包含制定行的list
816+
* @return 包含从start行到end行的list
817817
*/
818-
public static List<String> readFile2List(File file, int start, int end, String charsetName) {
818+
public static List<String> readFile2List(File file, int st, int end, String charsetName) {
819819
if (file == null) return null;
820-
if (start > end) return null;
821-
List<String> list = null;
820+
if (st > end) return null;
822821
BufferedReader reader = null;
823822
try {
824823
String line;
825824
int curLine = 1;
826-
list = new ArrayList<>();
825+
List<String> list = new ArrayList<>();
827826
if (StringUtils.isSpace(charsetName)) {
828827
reader = new BufferedReader(new FileReader(file));
829828
} else {
830829
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName));
831830
}
832831
while ((line = reader.readLine()) != null) {
833832
if (curLine > end) break;
834-
if (start <= curLine && curLine <= end) list.add(line);
833+
if (st <= curLine && curLine <= end) list.add(line);
835834
++curLine;
836835
}
836+
return list;
837837
} catch (IOException e) {
838838
e.printStackTrace();
839+
return null;
839840
} finally {
840841
closeIO(reader);
841842
}
842-
return list;
843843
}
844844

845845
/**
846-
* 指定编码按行读取文件到StringBuilder中
846+
* 指定编码按行读取文件到字符串中
847847
*
848848
* @param filePath 文件路径
849849
* @param charsetName 编码格式
850-
* @return StringBuilder对象
850+
* @return 字符串
851851
*/
852-
public static StringBuilder readFile2SB(String filePath, String charsetName) {
853-
return readFile2SB(getFileByPath(filePath), charsetName);
852+
public static String readFile2String(String filePath, String charsetName) {
853+
return readFile2String(getFileByPath(filePath), charsetName);
854854
}
855855

856856
/**
857-
* 指定编码按行读取文件到StringBuilder中
857+
* 指定编码按行读取文件到字符串中
858858
*
859859
* @param file 文件
860860
* @param charsetName 编码格式
861+
* @return 字符串
862+
*/
863+
public static String readFile2String(File file, String charsetName) {
864+
if (file == null) return null;
865+
try {
866+
return ConvertUtils.inputStream2String(new FileInputStream(file), charsetName);
867+
} catch (FileNotFoundException e) {
868+
e.printStackTrace();
869+
return null;
870+
}
871+
}
872+
873+
/**
874+
* 指定编码按行读取文件到字符串中
875+
*
876+
* @param filePath 文件路径
861877
* @return StringBuilder对象
862878
*/
863-
public static StringBuilder readFile2SB(File file, String charsetName) {
879+
public static byte[] readFile2Bytes(String filePath) {
880+
return readFile2Bytes(getFileByPath(filePath));
881+
}
882+
883+
/**
884+
* 指定编码按行读取文件到字符串中
885+
*
886+
* @param file 文件
887+
* @return StringBuilder对象
888+
*/
889+
public static byte[] readFile2Bytes(File file) {
864890
if (file == null) return null;
865-
StringBuilder sb = null;
866-
BufferedReader reader = null;
867891
try {
868-
sb = new StringBuilder();
869-
if (StringUtils.isSpace(charsetName)) {
870-
reader = new BufferedReader(new FileReader(file));
871-
} else {
872-
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
873-
charsetName));
874-
}
875-
String line;
876-
while ((line = reader.readLine()) != null) {
877-
sb.append(line);
878-
sb.append("\r\n");// windows系统换行为\r\n,Linux为\n
879-
}
880-
} catch (IOException e) {
892+
return ConvertUtils.inputStream2Bytes(new FileInputStream(file));
893+
} catch (FileNotFoundException e) {
881894
e.printStackTrace();
882-
} finally {
883-
closeIO(reader);
895+
return null;
884896
}
885-
return sb;
886897
}
887898

888899
/**

utilcode/src/test/java/com/blankj/utilcode/utils/ConvertUtilsTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import org.junit.Test;
55

6+
import static com.blankj.utilcode.utils.ConvertUtils.*;
7+
import static com.blankj.utilcode.utils.TestUtils.BASEPATH;
8+
import static com.blankj.utilcode.utils.TestUtils.SEP;
69
import static com.google.common.truth.Truth.assertThat;
710

811

@@ -16,31 +19,47 @@
1619
*/
1720
public class ConvertUtilsTest {
1821

22+
23+
String path = BASEPATH + "convert" + SEP;
1924
byte[] mBytes = new byte[]{0x00, 0x08, (byte) 0xdb, 0x33, 0x45, (byte) 0xab, 0x02, 0x23};
2025
String hexString = "0008DB3345AB0223";
2126

2227
@Test
2328
public void testBytes2HexString() throws Exception {
24-
assertThat(ConvertUtils.bytes2HexString(mBytes)).isEqualTo(hexString);
29+
assertThat(bytes2HexString(mBytes)).isEqualTo(hexString);
2530
}
2631

2732
@Test
2833
public void testHexString2Bytes() throws Exception {
29-
assertThat(ConvertUtils.hexString2Bytes(hexString)).isEqualTo(mBytes);
34+
assertThat(hexString2Bytes(hexString)).isEqualTo(mBytes);
3035
}
3136

3237
char[] mChars1 = new char[]{'0', '1', '2'};
3338
byte[] mBytes1 = new byte[]{48, 49, 50};
3439

3540
@Test
3641
public void testChars2Bytes() throws Exception {
37-
assertThat(ConvertUtils.chars2Bytes(mChars1)).isEqualTo(mBytes1);
42+
assertThat(chars2Bytes(mChars1)).isEqualTo(mBytes1);
3843
}
3944

4045
@Test
4146
public void testBytes2Chars() throws Exception {
42-
assertThat(ConvertUtils.bytes2Chars(mBytes1)).isEqualTo(mChars1);
47+
assertThat(bytes2Chars(mBytes1)).isEqualTo(mChars1);
4348
}
4449

50+
@Test
51+
public void testInputStream2BytesAndBytes2InputStream() throws Exception {
52+
String string = "this is test string";
53+
assertThat(new String(inputStream2Bytes(
54+
bytes2InputStream(string.getBytes("UTF-8")))))
55+
.isEqualTo(string);
56+
}
4557

58+
@Test
59+
public void testInputStream2StringAndString2InputStream() throws Exception {
60+
String string = "this is test string";
61+
assertThat(inputStream2String(
62+
string2InputStream(string, "UTF-8")
63+
, "UTF-8")).isEqualTo(string);
64+
}
4665
}

utilcode/src/test/java/com/blankj/utilcode/utils/FileUtilsTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,16 @@ public void testReadFile2List() throws Exception {
172172
}
173173

174174
@Test
175-
public void testReadFile2SB() throws Exception {
176-
System.out.println(readFile2SB(path + "UTF8.txt", "").toString());
177-
System.out.println(readFile2SB(path + "UTF8.txt", "UTF-8").toString());
178-
System.out.println(readFile2SB(path + "UTF8.txt", "GBK").toString());
175+
public void testReadFile2String() throws Exception {
176+
System.out.println(readFile2String(path + "UTF8.txt", ""));
177+
System.out.println(readFile2String(path + "UTF8.txt", "UTF-8"));
178+
System.out.println(readFile2String(path + "UTF8.txt", "GBK"));
179+
}
180+
181+
182+
@Test
183+
public void testReadFile2Bytes() throws Exception {
184+
System.out.println(new String(readFile2Bytes(path + "UTF8.txt")) );
179185
}
180186

181187
@Test

utilcode/utilcode.iml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@
6565
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6666
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
6767
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
72-
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
73-
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
74-
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
75-
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
7668
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
7769
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
7870
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
@@ -81,6 +73,14 @@
8173
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
8274
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
8375
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
79+
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
80+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
81+
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
82+
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
83+
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
8484
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />

0 commit comments

Comments
 (0)