1 // Tags: JDK1.4 |
|
2 |
|
3 // Copyright (C) 2005 Jeroen Frijters |
|
4 |
|
5 // This file is part of Mauve. |
|
6 |
|
7 // Mauve is free software; you can redistribute it and/or modify |
|
8 // it under the terms of the GNU General Public License as published by |
|
9 // the Free Software Foundation; either version 2, or (at your option) |
|
10 // any later version. |
|
11 |
|
12 // Mauve is distributed in the hope that it will be useful, |
|
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 // GNU General Public License for more details. |
|
16 |
|
17 // You should have received a copy of the GNU General Public License |
|
18 // along with Mauve; see the file COPYING. If not, write to |
|
19 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
20 // Boston, MA 02111-1307, USA. */ |
|
21 |
|
22 package gnu.testlet.java.nio.channels.FileChannel; |
|
23 |
|
24 import java.io.*; |
|
25 import java.nio.*; |
|
26 import java.nio.channels.*; |
|
27 |
|
28 import gnu.testlet.Testlet; |
|
29 import gnu.testlet.TestHarness; |
|
30 |
|
31 public class map implements Testlet |
|
32 { |
|
33 private static final byte[] msg = "Hello, World!".getBytes(); |
|
34 |
|
35 public void test(TestHarness harness) |
|
36 { |
|
37 try |
|
38 { |
|
39 String filename = harness.getTempDirectory() + File.separator + "mauvemap.txt"; |
|
40 FileOutputStream fos = new FileOutputStream(filename); |
|
41 FileChannel chan = fos.getChannel(); |
|
42 try |
|
43 { |
|
44 chan.map(FileChannel.MapMode.READ_WRITE, 0, msg.length); |
|
45 harness.check(false); |
|
46 } |
|
47 catch(NonReadableChannelException x) |
|
48 { |
|
49 harness.check(true); |
|
50 } |
|
51 fos.close(); |
|
52 |
|
53 RandomAccessFile ras = new RandomAccessFile(filename, "rw"); |
|
54 chan = ras.getChannel(); |
|
55 MappedByteBuffer mbb = chan.map(FileChannel.MapMode.READ_WRITE, 0, msg.length); |
|
56 mbb.put(msg); |
|
57 mbb.force(); |
|
58 |
|
59 verifyContent(harness, filename); |
|
60 |
|
61 MappedByteBuffer mbb2 = chan.map(FileChannel.MapMode.PRIVATE, 0, msg.length); |
|
62 mbb2.put(new byte[msg.length]); |
|
63 boolean ok = true; |
|
64 for (int i = 0; i < msg.length; i++) |
|
65 ok &= mbb2.get(i) == 0; |
|
66 harness.check(ok); |
|
67 mbb.force(); |
|
68 ras.close(); |
|
69 |
|
70 verifyContent(harness, filename); |
|
71 } |
|
72 catch(Exception x) |
|
73 { |
|
74 harness.debug(x); |
|
75 harness.check(false); |
|
76 } |
|
77 } |
|
78 |
|
79 private void verifyContent(TestHarness harness, String filename) throws IOException |
|
80 { |
|
81 FileInputStream fis = new FileInputStream(filename); |
|
82 FileChannel chan = fis.getChannel(); |
|
83 MappedByteBuffer mbb = chan.map(FileChannel.MapMode.READ_ONLY, 0, msg.length); |
|
84 byte[] buf = new byte[msg.length]; |
|
85 mbb.get(buf); |
|
86 boolean ok = true; |
|
87 for (int i = 0; i < msg.length; i++) |
|
88 ok &= msg[i] == buf[i]; |
|
89 harness.check(ok); |
|
90 fis.close(); |
|
91 } |
|
92 } |
|