J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @summary Test file mapping with FileChannel |
| 26 | */ |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.nio.MappedByteBuffer; |
| 30 | import java.nio.channels.*; |
| 31 | import java.nio.channels.FileChannel; |
| 32 | import java.util.Random; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Testing FileChannel's mapping capabilities. |
| 37 | */ |
| 38 | |
| 39 | public class MapTest { |
| 40 | |
| 41 | private static PrintStream err = System.err; |
| 42 | |
| 43 | private static Random generator = new Random(); |
| 44 | |
| 45 | private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6; |
| 46 | |
| 47 | private static File blah; |
| 48 | |
| 49 | public static void main(String[] args) throws Exception { |
| 50 | blah = File.createTempFile("blah", null); |
| 51 | blah.deleteOnExit(); |
| 52 | initTestFile(blah); |
| 53 | err.println("Test file " + blah + " initialized"); |
| 54 | testZero(); |
| 55 | err.println("Zero size: OK"); |
| 56 | testRead(); |
| 57 | err.println("Read: OK"); |
| 58 | testWrite(); |
| 59 | err.println("Write: OK"); |
| 60 | testHighOffset(); |
| 61 | err.println("High offset: OK"); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates file blah: |
| 66 | * 0000 |
| 67 | * 0001 |
| 68 | * 0002 |
| 69 | * 0003 |
| 70 | * . |
| 71 | * . |
| 72 | * . |
| 73 | * 3999 |
| 74 | * |
| 75 | * Blah extends beyond a single page of memory so that the |
| 76 | * ability to index into a file of multiple pages is tested. |
| 77 | */ |
| 78 | private static void initTestFile(File blah) throws Exception { |
| 79 | FileOutputStream fos = new FileOutputStream(blah); |
| 80 | BufferedWriter awriter |
| 81 | = new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); |
| 82 | |
| 83 | for(int i=0; i<4000; i++) { |
| 84 | String number = new Integer(i).toString(); |
| 85 | for (int h=0; h<4-number.length(); h++) |
| 86 | awriter.write("0"); |
| 87 | awriter.write(""+i); |
| 88 | awriter.newLine(); |
| 89 | } |
| 90 | awriter.flush(); |
| 91 | awriter.close(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests zero size file mapping |
| 96 | */ |
| 97 | private static void testZero() throws Exception { |
| 98 | FileInputStream fis = new FileInputStream(blah); |
| 99 | FileChannel c = fis.getChannel(); |
| 100 | MappedByteBuffer b = c.map(FileChannel.MapMode.READ_ONLY, 0, 0); |
| 101 | c.close(); |
| 102 | fis.close(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Maps blah file with a random offset and checks to see if read |
| 107 | * from the ByteBuffer gets the right line number |
| 108 | */ |
| 109 | private static void testRead() throws Exception { |
| 110 | StringBuffer sb = new StringBuffer(); |
| 111 | sb.setLength(4); |
| 112 | |
| 113 | for (int x=0; x<1000; x++) { |
| 114 | FileInputStream fis = new FileInputStream(blah); |
| 115 | FileChannel c = fis.getChannel(); |
| 116 | |
| 117 | long offset = generator.nextInt(10000); |
| 118 | long expectedResult = offset / CHARS_PER_LINE; |
| 119 | offset = expectedResult * CHARS_PER_LINE; |
| 120 | |
| 121 | MappedByteBuffer b = c.map(FileChannel.MapMode.READ_ONLY, |
| 122 | offset, 100); |
| 123 | |
| 124 | for (int i=0; i<4; i++) { |
| 125 | byte aByte = b.get(i); |
| 126 | sb.setCharAt(i, (char)aByte); |
| 127 | } |
| 128 | |
| 129 | int result = Integer.parseInt(sb.toString()); |
| 130 | if (result != expectedResult) { |
| 131 | err.println("I expected "+expectedResult); |
| 132 | err.println("I got "+result); |
| 133 | throw new Exception("Read test failed"); |
| 134 | } |
| 135 | c.close(); |
| 136 | fis.close(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Maps blah file with a random offset and checks to see if data |
| 142 | * written out to the file can be read back in |
| 143 | */ |
| 144 | private static void testWrite() throws Exception { |
| 145 | StringBuffer sb = new StringBuffer(); |
| 146 | sb.setLength(4); |
| 147 | |
| 148 | for (int x=0; x<1000; x++) { |
| 149 | RandomAccessFile raf = new RandomAccessFile(blah, "rw"); |
| 150 | FileChannel c = raf.getChannel(); |
| 151 | |
| 152 | long offset = generator.nextInt(1000); |
| 153 | MappedByteBuffer b = c.map(FileChannel.MapMode.READ_WRITE, |
| 154 | offset, 100); |
| 155 | |
| 156 | for (int i=0; i<4; i++) { |
| 157 | b.put(i, (byte)('0' + i)); |
| 158 | } |
| 159 | |
| 160 | for (int i=0; i<4; i++) { |
| 161 | byte aByte = b.get(i); |
| 162 | sb.setCharAt(i, (char)aByte); |
| 163 | } |
| 164 | if (!sb.toString().equals("0123")) |
| 165 | throw new Exception("Write test failed"); |
| 166 | c.close(); |
| 167 | raf.close(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | private static void testHighOffset() throws Exception { |
| 172 | StringBuffer sb = new StringBuffer(); |
| 173 | sb.setLength(4); |
| 174 | |
| 175 | for (int x=0; x<1000; x++) { |
| 176 | RandomAccessFile raf = new RandomAccessFile(blah, "rw"); |
| 177 | FileChannel fc = raf.getChannel(); |
| 178 | long offset = 66000; |
| 179 | MappedByteBuffer b = fc.map(FileChannel.MapMode.READ_WRITE, |
| 180 | offset, 100); |
| 181 | |
| 182 | fc.close(); |
| 183 | raf.close(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } |