-
Notifications
You must be signed in to change notification settings - Fork 7
/
MemoryMapTest.java
225 lines (192 loc) · 6.67 KB
/
MemoryMapTest.java
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.corejava.chapter1;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
import java.util.zip.*;
import static java.lang.System.*;
public class MemoryMapTest
{
private static String workDir = System.getProperty("user.dir") + File.separator + "src" + File.separator +
"com" + File.separator + "corejava" + File.separator + "chapter1" + File.separator;
private static int RECORD_SIZE = 2048;
public static void main2(String[] args) throws IOException
{
Path path = Paths.get(workDir, "test.txt");
// ByteBuffer.allocate 或 ByteBuffer.wrap : 要获得缓冲区,调用它们
ByteBuffer buffer = ByteBuffer.allocate(RECORD_SIZE);
try(FileChannel channel = FileChannel.open(path))
{
channel.read(buffer);
printByteBuffer("all content", buffer);// all content
// put方法:将值添加到缓冲区;
buffer.put("test for memory buffer map".getBytes());
printByteBuffer("after put", buffer);// after put
buffer.position(10);
// flip方法:将界限设置到当前位置,并把位置复位到0;
buffer.flip();
printByteBuffer("after flip", buffer);// after flip
buffer.position(5);
// buffer.remaining = limit - position = 10 - 5
// remaining方法: 现在在 remaining 方法返回整数时(它返回的值是 “界限—位置”)
out.println("\n buffer.remaining = " + buffer.remaining());
}
}
public static void printByteBuffer(String comment, ByteBuffer buffer)
{
out.println("\n === " + comment +" ===");
for (int i = 0; i < buffer.limit(); i++)
{
out.print((char)buffer.get(i));
}
}
public static long checksumInputStream(Path filename) throws IOException
{
try (InputStream in = Files.newInputStream(filename))
{
CRC32 crc = new CRC32();
int c;
while ((c = in.read()) != -1)
crc.update(c);
return crc.getValue();
}
}
public static long checksumBufferedInputStream(Path filename) throws IOException
{
try (InputStream in = new BufferedInputStream(Files.newInputStream(filename)))
{
CRC32 crc = new CRC32();
int c;
while ((c = in.read()) != -1)
crc.update(c);
return crc.getValue();
}
}
public static long checksumRandomAccessFile(Path filename) throws IOException
{
try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r"))
{
long length = file.length();
CRC32 crc = new CRC32();
for (long p = 0; p < length; p++)
{
file.seek(p);
int c = file.readByte();
crc.update(c);
}
return crc.getValue();
}
}
public static long checksumMappedFile(Path filename) throws IOException
{
try (FileChannel channel = FileChannel.open(filename))
{
CRC32 crc = new CRC32();
int length = (int) channel.size();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
for (int p = 0; p < length; p++)
{
int c = buffer.get(p);
crc.update(c);
}
return crc.getValue();
}
}
public static void main1(String[] args) throws IOException
{
System.out.println("Input Stream:");
long start = System.currentTimeMillis();
Path filename = Paths.get(args[0]);
long crcValue = checksumInputStream(filename);
long end = System.currentTimeMillis();
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start) + " milliseconds");
System.out.println("\nBuffered Input Stream:");
start = System.currentTimeMillis();
crcValue = checksumBufferedInputStream(filename);
end = System.currentTimeMillis();
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start) + " milliseconds");
System.out.println("\nRandom Access File:");
start = System.currentTimeMillis();
crcValue = checksumRandomAccessFile(filename);
end = System.currentTimeMillis();
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start) + " milliseconds");
System.out.println("\nMapped File:"); // 内存映射文件
start = System.currentTimeMillis();
crcValue = checksumMappedFile(filename);
end = System.currentTimeMillis();
System.out.println(Long.toHexString(crcValue));
System.out.println((end - start) + " milliseconds");
}
public static void main3(String[] args) throws IOException
{
Path path = Paths.get(workDir, "test.txt");
new Thread(new Runnable(){
@Override
public void run()
{
out.print("\n1st thread starts.\n");
try(FileChannel channel = FileChannel.open(path, StandardOpenOption.READ))
{
FileLock lock = channel.tryLock(0, 10, true);
out.println(lock.isShared() + " from 1st thread");
lock.release();
} catch (IOException e){e.printStackTrace();}
out.print("\n1st thread ends.\n");
}
}).start();
new Thread(new Runnable(){
@Override
public void run()
{
out.print("\n2nd thread starts.\n");
try(FileChannel channel = FileChannel.open(path, StandardOpenOption.READ))
{
FileLock lock = channel.tryLock(0, 10, true);
out.println(lock.isShared() + " from 2nd thread");
lock.release();
} catch (IOException e){e.printStackTrace();}
out.print("\n2nd thread ends.\n");
}
}).start();
}
public static void main(String[] args) throws IOException, InterruptedException
{
Path path = Paths.get(workDir, "test.txt");
new Thread(new Runnable(){
@Override
public void run()
{
out.print("\n1st thread starts.\n");
try(FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE))
{
// 锁定第1个到第10个字节(index from 0 to 9)
FileLock lock = channel.tryLock(0, 10, false);
Thread.sleep(3000);
out.println(lock.isShared() + " from 1st thread");
lock.release();
} catch (IOException | InterruptedException e){e.printStackTrace();}
out.print("\n1st thread ends.\n");
}
}).start();
new Thread(new Runnable(){
@Override
public void run()
{
try(FileChannel channel = FileChannel.open(path))
{
Thread.sleep(1000);
out.print("\n2nd thread starts.\n");
ByteBuffer buffer = ByteBuffer.allocate(10);
//channel.position(9); // read from index 9 ,throws exception
channel.position(10); // just substitute 9 with 10
channel.read(buffer);
printByteBuffer("2nd thread read buffer", buffer);
} catch (IOException | InterruptedException e){e.printStackTrace();}
out.print("\n2nd thread ends.\n");
}
}).start();
}
}