Skip to content

Commit bf42978

Browse files
committed
Refactor/streamify/fix exceptions
1 parent f7e0a74 commit bf42978

15 files changed

+280
-56
lines changed

newio/BufferToText.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,43 @@
1010

1111
public class BufferToText {
1212
private static final int BSIZE = 1024;
13-
public static void
14-
main(String[] args) throws Exception {
13+
public static void main(String[] args) {
1514
try(
1615
FileChannel fc = new FileOutputStream(
1716
"data2.txt").getChannel()
1817
) {
1918
fc.write(ByteBuffer.wrap("Some text".getBytes()));
19+
} catch(IOException e) {
20+
throw new RuntimeException(e);
2021
}
2122
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
2223
try(
2324
FileChannel fc = new FileInputStream(
2425
"data2.txt").getChannel()
2526
) {
2627
fc.read(buff);
28+
} catch(IOException e) {
29+
throw new RuntimeException(e);
2730
}
2831
buff.flip();
2932
// Doesn't work:
3033
System.out.println(buff.asCharBuffer());
3134
// Decode using this system's default Charset:
3235
buff.rewind();
33-
String encoding = System.getProperty("file.encoding");
34-
System.out.println("Decoded using " + encoding + ": "
36+
String encoding =
37+
System.getProperty("file.encoding");
38+
System.out.println("Decoded using " +
39+
encoding + ": "
3540
+ Charset.forName(encoding).decode(buff));
36-
// Or, we could encode with something that prints:
41+
// Encode with something that prints:
3742
try(
3843
FileChannel fc = new FileOutputStream(
3944
"data2.txt").getChannel()
4045
) {
4146
fc.write(ByteBuffer.wrap(
4247
"Some text".getBytes("UTF-16BE")));
48+
} catch(IOException e) {
49+
throw new RuntimeException(e);
4350
}
4451
// Now try reading again:
4552
buff.clear();
@@ -48,17 +55,21 @@ public class BufferToText {
4855
"data2.txt").getChannel()
4956
) {
5057
fc.read(buff);
58+
} catch(IOException e) {
59+
throw new RuntimeException(e);
5160
}
5261
buff.flip();
5362
System.out.println(buff.asCharBuffer());
5463
// Use a CharBuffer to write through:
55-
buff = ByteBuffer.allocate(24); // More than needed
64+
buff = ByteBuffer.allocate(24);
5665
buff.asCharBuffer().put("Some text");
5766
try(
5867
FileChannel fc = new FileOutputStream(
5968
"data2.txt").getChannel()
6069
) {
6170
fc.write(buff);
71+
} catch(IOException e) {
72+
throw new RuntimeException(e);
6273
}
6374
// Read and display:
6475
buff.clear();
@@ -67,6 +78,8 @@ public class BufferToText {
6778
"data2.txt").getChannel()
6879
) {
6980
fc.read(buff);
81+
} catch(IOException e) {
82+
throw new RuntimeException(e);
7083
}
7184
buff.flip();
7285
System.out.println(buff.asCharBuffer());

newio/ChannelCopy.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
public class ChannelCopy {
1212
private static final int BSIZE = 1024;
13-
public static void
14-
main(String[] args) throws Exception {
13+
public static void main(String[] args) {
1514
if(args.length != 2) {
1615
System.out.println(
1716
"arguments: sourcefile destfile");
@@ -29,6 +28,8 @@ public class ChannelCopy {
2928
out.write(buffer);
3029
buffer.clear(); // Prepare for reading
3130
}
31+
} catch(IOException e) {
32+
throw new RuntimeException(e);
3233
}
3334
}
3435
}

newio/FileLocking.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import java.io.*;
88

99
public class FileLocking {
10-
public static void
11-
main(String[] args) throws Exception {
10+
public static void main(String[] args) {
1211
try(
1312
FileOutputStream fos =
1413
new FileOutputStream("file.txt");
@@ -20,6 +19,8 @@ public class FileLocking {
2019
fl.release();
2120
System.out.println("Released Lock");
2221
}
22+
} catch(IOException | InterruptedException e) {
23+
throw new RuntimeException(e);
2324
}
2425
}
2526
}

newio/GetChannel.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// newio/GetChannel.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
// Getting channels from streams
6+
import java.nio.*;
7+
import java.nio.channels.*;
8+
import java.io.*;
9+
10+
public class GetChannel {
11+
private static String name = "data.txt";
12+
private static final int BSIZE = 1024;
13+
public static void main(String[] args) {
14+
// Write a file:
15+
try(
16+
FileChannel fc = new FileOutputStream(name)
17+
.getChannel()
18+
) {
19+
fc.write(ByteBuffer
20+
.wrap("Some text ".getBytes()));
21+
} catch(IOException e) {
22+
throw new RuntimeException(e);
23+
}
24+
// Add to the end of the file:
25+
try(
26+
FileChannel fc = new RandomAccessFile(
27+
name, "rw").getChannel()
28+
) {
29+
fc.position(fc.size()); // Move to the end
30+
fc.write(ByteBuffer
31+
.wrap("Some more".getBytes()));
32+
} catch(IOException e) {
33+
throw new RuntimeException(e);
34+
}
35+
// Read the file:
36+
try(
37+
FileChannel fc = new FileInputStream(name)
38+
.getChannel()
39+
) {
40+
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
41+
fc.read(buff);
42+
buff.flip();
43+
while(buff.hasRemaining())
44+
System.out.write(buff.get());
45+
} catch(IOException e) {
46+
throw new RuntimeException(e);
47+
}
48+
System.out.flush();
49+
}
50+
}
51+
/* Output:
52+
Some text Some more
53+
*/

newio/LargeMappedFiles.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class LargeMappedFiles {
1515
RandomAccessFile tdat =
1616
new RandomAccessFile("test.dat", "rw")
1717
) {
18-
MappedByteBuffer out = tdat.getChannel()
19-
.map(FileChannel.MapMode.READ_WRITE, 0, length);
18+
MappedByteBuffer out = tdat.getChannel().map(
19+
FileChannel.MapMode.READ_WRITE, 0, length);
2020
for(int i = 0; i < length; i++)
2121
out.put((byte)'x');
2222
System.out.println("Finished writing");

newio/LockingMappedFiles.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ public class LockingMappedFiles {
1212
static FileChannel fc;
1313
public static void
1414
main(String[] args) throws Exception {
15-
fc =
16-
new RandomAccessFile("test.dat", "rw").getChannel();
17-
MappedByteBuffer out =
18-
fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH);
15+
fc = new RandomAccessFile("test.dat", "rw")
16+
.getChannel();
17+
MappedByteBuffer out = fc.map(
18+
FileChannel.MapMode.READ_WRITE, 0, LENGTH);
1919
for(int i = 0; i < LENGTH; i++)
2020
out.put((byte)'x');
2121
new LockAndModify(out, 0, 0 + LENGTH/3);
22-
new LockAndModify(out, LENGTH/2, LENGTH/2 + LENGTH/4);
22+
new LockAndModify(
23+
out, LENGTH/2, LENGTH/2 + LENGTH/4);
2324
}
2425
private static class LockAndModify extends Thread {
2526
private ByteBuffer buff;

newio/MappedIO.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// newio/MappedIO.java
2+
// (c)2017 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
import java.util.*;
6+
import java.nio.*;
7+
import java.nio.channels.*;
8+
import java.io.*;
9+
10+
public class MappedIO {
11+
private static int numOfInts = 4_000_000;
12+
private static int numOfUbuffInts = 100_000;
13+
private abstract static class Tester {
14+
private String name;
15+
public Tester(String name) {
16+
this.name = name;
17+
}
18+
public void runTest() {
19+
System.out.print(name + ": ");
20+
long start = System.nanoTime();
21+
test();
22+
double duration = System.nanoTime() - start;
23+
System.out.format("%.3f%n", duration/1.0e9);
24+
}
25+
public abstract void test();
26+
}
27+
private static Tester[] tests = {
28+
new Tester("Stream Write") {
29+
@Override
30+
public void test() {
31+
try(
32+
DataOutputStream dos =
33+
new DataOutputStream(
34+
new BufferedOutputStream(
35+
new FileOutputStream(
36+
new File("temp.tmp"))))
37+
) {
38+
for(int i = 0; i < numOfInts; i++)
39+
dos.writeInt(i);
40+
} catch(IOException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
},
45+
new Tester("Mapped Write") {
46+
@Override
47+
public void test() {
48+
try(
49+
FileChannel fc =
50+
new RandomAccessFile("temp.tmp", "rw")
51+
.getChannel()
52+
) {
53+
IntBuffer ib =
54+
fc.map(FileChannel.MapMode.READ_WRITE,
55+
0, fc.size()).asIntBuffer();
56+
for(int i = 0; i < numOfInts; i++)
57+
ib.put(i);
58+
} catch(IOException e) {
59+
throw new RuntimeException(e);
60+
}
61+
}
62+
},
63+
new Tester("Stream Read") {
64+
@Override
65+
public void test() {
66+
try(
67+
DataInputStream dis =
68+
new DataInputStream(
69+
new BufferedInputStream(
70+
new FileInputStream("temp.tmp")))
71+
) {
72+
for(int i = 0; i < numOfInts; i++)
73+
dis.readInt();
74+
} catch(IOException e) {
75+
throw new RuntimeException(e);
76+
}
77+
}
78+
},
79+
new Tester("Mapped Read") {
80+
@Override
81+
public void test() {
82+
try(
83+
FileChannel fc = new FileInputStream(
84+
new File("temp.tmp")).getChannel()
85+
) {
86+
IntBuffer ib =
87+
fc.map(FileChannel.MapMode.READ_ONLY,
88+
0, fc.size()).asIntBuffer();
89+
while(ib.hasRemaining())
90+
ib.get();
91+
} catch(IOException e) {
92+
throw new RuntimeException(e);
93+
}
94+
}
95+
},
96+
new Tester("Stream Read/Write") {
97+
@Override
98+
public void test() {
99+
try(
100+
RandomAccessFile raf =
101+
new RandomAccessFile(
102+
new File("temp.tmp"), "rw")
103+
) {
104+
raf.writeInt(1);
105+
for(int i = 0; i < numOfUbuffInts; i++) {
106+
raf.seek(raf.length() - 4);
107+
raf.writeInt(raf.readInt());
108+
}
109+
} catch(IOException e) {
110+
throw new RuntimeException(e);
111+
}
112+
}
113+
},
114+
new Tester("Mapped Read/Write") {
115+
@Override
116+
public void test() {
117+
try(
118+
FileChannel fc = new RandomAccessFile(
119+
new File("temp.tmp"), "rw").getChannel()
120+
) {
121+
IntBuffer ib =
122+
fc.map(FileChannel.MapMode.READ_WRITE,
123+
0, fc.size()).asIntBuffer();
124+
ib.put(0);
125+
for(int i = 1; i < numOfUbuffInts; i++)
126+
ib.put(ib.get(i - 1));
127+
} catch(IOException e) {
128+
throw new RuntimeException(e);
129+
}
130+
}
131+
}
132+
};
133+
public static void main(String[] args) {
134+
Arrays.stream(tests).forEach(Tester::runTest);
135+
}
136+
}
137+
/* Output:
138+
Stream Write: 0.269
139+
Mapped Write: 0.022
140+
Stream Read: 0.262
141+
Mapped Read: 0.010
142+
Stream Read/Write: 1.620
143+
Mapped Read/Write: 0.007
144+
*/

newio/TransferTo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import java.io.*;
99

1010
public class TransferTo {
11-
public static void
12-
main(String[] args) throws Exception {
11+
public static void main(String[] args) {
1312
if(args.length != 2) {
1413
System.out.println(
1514
"arguments: sourcefile destfile");
@@ -24,6 +23,8 @@ public class TransferTo {
2423
in.transferTo(0, in.size(), out);
2524
// Or:
2625
// out.transferFrom(in, 0, in.size());
26+
} catch(IOException e) {
27+
throw new RuntimeException(e);
2728
}
2829
}
2930
}

newio/UsingBuffers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ void symmetricScramble(CharBuffer buffer) {
1717
}
1818
public static void main(String[] args) {
1919
char[] data = "UsingBuffers".toCharArray();
20-
ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
20+
ByteBuffer bb =
21+
ByteBuffer.allocate(data.length * 2);
2122
CharBuffer cb = bb.asCharBuffer();
2223
cb.put(data);
2324
System.out.println(cb.rewind());

0 commit comments

Comments
 (0)