Skip to content

Commit 6ad35e5

Browse files
committed
添加io内容,ByteArrayInputStream
1 parent e4ab057 commit 6ad35e5

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.byhieg.iotutorial.bytestreamio;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.File;
5+
6+
/**
7+
* Created by byhieg on 17/2/21.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class ByteArrayInputStreamExample {
11+
12+
public String readFromArray(byte[] bytes) {
13+
StringBuffer sb = new StringBuffer();
14+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
15+
int tmp;
16+
while ((tmp = bais.read()) != -1) {
17+
sb.append(Integer.toHexString(tmp));
18+
}
19+
return sb.toString();
20+
}
21+
22+
public void readMarkAndReset(byte[] bytes,int mark) {
23+
StringBuffer sb = new StringBuffer();
24+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
25+
bais.mark(mark);
26+
27+
bais.skip(mark + 1);
28+
int tmp;
29+
while ((tmp = bais.read()) != -1) {
30+
sb.append(Integer.toHexString(tmp));
31+
}
32+
33+
System.out.println("越过标记后的字符串");
34+
System.out.println(sb.toString());
35+
36+
bais.reset();
37+
sb.setLength(0);
38+
39+
int m;
40+
while ((m = bais.read()) != -1) {
41+
sb.append(Integer.toHexString(m));
42+
}
43+
44+
System.out.println("重置之后的字符串");
45+
System.out.println(sb.toString());
46+
}
47+
48+
49+
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I don't know what I do now is right, those are wrong, and when I finally die then I know these.
2+
So what I can do now is to try to do everything well, and then wait to die .
3+
Sometimes I can be very happy to talk to everyone, can be very presumptuous, but no one knows,
4+
it is but very deliberately camouflage, camouflage; I can make him very happy very happy,
5+
but couldn't find the source of happiness, just giggle.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.byhieg.iotutorialtest;
2+
3+
import cn.byhieg.iotutorial.bytestreamio.ByteArrayInputStreamExample;
4+
import junit.framework.TestCase;
5+
6+
/**
7+
* Created by byhieg on 17/2/21.
8+
* Mail to byhieg@gmail.com
9+
*/
10+
public class ByteArrayInputStreamExampleTest extends TestCase {
11+
public void testReadFromArray() throws Exception {
12+
byte[] bytes = new byte[]{0x1,0x2,0x3,0x4,0x5,0x6,0x7};
13+
ByteArrayInputStreamExample example = new ByteArrayInputStreamExample();
14+
System.out.println(example.readFromArray(bytes));
15+
}
16+
17+
18+
public void testReadMarkAndReset() throws Exception {
19+
byte[] bytes = new byte[]{0x1,0x2,0x3,0x4,0x5,0x6,0x7};
20+
ByteArrayInputStreamExample example = new ByteArrayInputStreamExample();
21+
example.readMarkAndReset(bytes,2);
22+
System.out.println("++++++++++++++++++++");
23+
}
24+
25+
}

0 commit comments

Comments
 (0)