Skip to content

Commit 490a25f

Browse files
committed
[add] add some examples of nio
1 parent 5423ec5 commit 490a25f

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.brianway.learning.java.nio;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
9+
/**
10+
* Created by brian on 16/11/29.
11+
*/
12+
public class ChannelCopy {
13+
14+
private static final int BSIZE = 1024;
15+
16+
public static void main(String[] args) throws IOException {
17+
String parent = ChannelCopy.class.getResource("/").getPath();
18+
String infile = parent + "/infile.txt";
19+
String outCopy = parent + "/outCopy.txt";
20+
String outTransferTo = parent + "/outTransferTo.txt";
21+
copy(infile, outCopy);
22+
transferTo(infile, outTransferTo);
23+
}
24+
25+
private static void copy(String infile, String outfile) throws IOException {
26+
FileChannel
27+
in = new FileInputStream(infile).getChannel(),
28+
out = new FileOutputStream(outfile).getChannel();
29+
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
30+
while (in.read(buffer) != -1) {
31+
buffer.flip(); // prepare for writing
32+
out.write(buffer);
33+
buffer.clear(); // prepare for reading
34+
}
35+
}
36+
37+
private static void transferTo(String infile, String outfile) throws IOException {
38+
FileChannel
39+
in = new FileInputStream(infile).getChannel(),
40+
out = new FileOutputStream(outfile).getChannel();
41+
in.transferTo(0, in.size(), out);
42+
// Or:
43+
// out.transferFrom(in,0,in.size());
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.brianway.learning.java.nio;
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.ByteOrder;
5+
import java.util.Arrays;
6+
7+
/**
8+
* Created by brian on 16/11/29.
9+
*
10+
* "big endian"(高位优先),将最重要的字节放在地址最低的存储器单元;
11+
* "little endian"(低位优先),将最重要的字节放在地址最高的存储器单元;
12+
*
13+
* ByteBuffer 是以高位优先的形式存储数据的。
14+
*/
15+
public class Endians {
16+
public static void main(String[] args) {
17+
ByteBuffer bb = ByteBuffer.wrap(new byte[12]);
18+
bb.asCharBuffer().put("abcdef");
19+
System.out.println(Arrays.toString(bb.array()));
20+
21+
bb.rewind();
22+
bb.order(ByteOrder.BIG_ENDIAN);
23+
bb.asCharBuffer().put("abcdef");
24+
System.out.println(Arrays.toString(bb.array()));
25+
26+
bb.rewind();
27+
bb.order(ByteOrder.LITTLE_ENDIAN);
28+
bb.asCharBuffer().put("abcdef");
29+
System.out.println(Arrays.toString(bb.array()));
30+
}
31+
}
32+
/* Output:
33+
[0, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102]
34+
[0, 97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102]
35+
[97, 0, 98, 0, 99, 0, 100, 0, 101, 0, 102, 0]
36+
*///:~
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.brianway.learning.java.nio;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
import java.nio.ByteBuffer;
8+
import java.nio.channels.FileChannel;
9+
10+
/**
11+
* Created by brian on 16/11/29.
12+
*/
13+
public class GetChannel {
14+
private static final int BSIZE = 1024;
15+
16+
public static void main(String[] args) throws IOException {
17+
String parent = GetChannel.class.getResource("/").getPath();
18+
String filename = parent + "/data.txt";
19+
write(filename);
20+
addToEnd(filename);
21+
read(filename);
22+
}
23+
24+
private static void write(String filename) throws IOException {
25+
FileChannel fc = new FileOutputStream(filename).getChannel();
26+
fc.write(ByteBuffer.wrap("Some text ".getBytes()));
27+
fc.close();
28+
}
29+
30+
private static void addToEnd(String filename) throws IOException {
31+
FileChannel fc = new RandomAccessFile(filename, "rw").getChannel();
32+
fc.position(fc.size());
33+
fc.write(ByteBuffer.wrap("Some more ".getBytes()));
34+
fc.close();
35+
}
36+
37+
private static void read(String filename) throws IOException {
38+
FileChannel fc = new FileInputStream(filename).getChannel();
39+
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
40+
fc.read(buff);
41+
buff.flip();
42+
while (buff.hasRemaining()) {
43+
System.out.print((char) buff.get());
44+
}
45+
fc.close();
46+
}
47+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.brianway.learning.java.nio;//: io/ViewBuffers.java
2+
3+
import java.nio.ByteBuffer;
4+
import java.nio.CharBuffer;
5+
import java.nio.DoubleBuffer;
6+
import java.nio.FloatBuffer;
7+
import java.nio.IntBuffer;
8+
import java.nio.LongBuffer;
9+
import java.nio.ShortBuffer;
10+
11+
/**
12+
* Created by brian on 16/11/29.
13+
*
14+
* 通过在同一个 ByteBuffer 上建立不同的试图缓冲器,
15+
* 将同一字节序列翻译成short,int,float,long,double等类型的数据
16+
*/
17+
18+
public class ViewBuffers {
19+
public static void main(String[] args) {
20+
ByteBuffer bb = ByteBuffer.wrap(
21+
new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'});
22+
bb.rewind();
23+
System.out.print("Byte Buffer ");
24+
while (bb.hasRemaining())
25+
System.out.print(bb.position() + " -> " + bb.get() + ", ");
26+
System.out.println();
27+
28+
CharBuffer cb =
29+
((ByteBuffer) bb.rewind()).asCharBuffer();
30+
System.out.println("Char Buffer ");
31+
while (cb.hasRemaining())
32+
System.out.print(cb.position() + " -> " + cb.get() + ", ");
33+
System.out.println();
34+
35+
FloatBuffer fb =
36+
((ByteBuffer) bb.rewind()).asFloatBuffer();
37+
System.out.print("Float Buffer ");
38+
while (fb.hasRemaining())
39+
System.out.print(fb.position() + " -> " + fb.get() + ", ");
40+
System.out.println();
41+
42+
IntBuffer ib =
43+
((ByteBuffer) bb.rewind()).asIntBuffer();
44+
System.out.print("Int Buffer ");
45+
while (ib.hasRemaining())
46+
System.out.print(ib.position() + " -> " + ib.get() + ", ");
47+
System.out.println();
48+
49+
LongBuffer lb =
50+
((ByteBuffer) bb.rewind()).asLongBuffer();
51+
System.out.print("Long Buffer ");
52+
while (lb.hasRemaining())
53+
System.out.print(lb.position() + " -> " + lb.get() + ", ");
54+
System.out.println();
55+
56+
ShortBuffer sb =
57+
((ByteBuffer) bb.rewind()).asShortBuffer();
58+
System.out.print("Short Buffer ");
59+
while (sb.hasRemaining())
60+
System.out.print(sb.position() + " -> " + sb.get() + ", ");
61+
System.out.println();
62+
63+
DoubleBuffer db =
64+
((ByteBuffer) bb.rewind()).asDoubleBuffer();
65+
System.out.print("Double Buffer ");
66+
while (db.hasRemaining())
67+
System.out.print(db.position() + " -> " + db.get() + ", ");
68+
}
69+
}
70+
/* Output:
71+
Byte Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 0, 4 -> 0, 5 -> 0, 6 -> 0, 7 -> 97,
72+
Char Buffer 0 -> , 1 -> , 2 -> , 3 -> a,
73+
Float Buffer 0 -> 0.0, 1 -> 1.36E-43,
74+
Int Buffer 0 -> 0, 1 -> 97,
75+
Long Buffer 0 -> 97,
76+
Short Buffer 0 -> 0, 1 -> 0, 2 -> 0, 3 -> 97,
77+
Double Buffer 0 -> 4.8E-322,
78+
*///:~

0 commit comments

Comments
 (0)