blob: d064aee3a7974913d5c46c11d700dcd9a185c558 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2003 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 @bug 4452020 4629048 4638365 4869859
26 * @summary Test FileChannel scattering reads
27 */
28
29import java.nio.channels.*;
30import java.nio.*;
31import java.io.*;
32
33public class ScatteringRead {
34
35 private static final int NUM_BUFFERS = 3;
36
37 private static final int BUFFER_CAP = 3;
38
39 private static final int BIG_BUFFER_CAP = Integer.MAX_VALUE / 3 + 10;
40
41 public static void main(String[] args) throws Exception {
42 test1(); // for bug 4452020
43 test2(); // for bug 4629048
44 System.gc();
45
46 // Test 3 proves that the system is capable of reading
47 // more than MAX_INT bytes in one shot. But it is unsuitable
48 // for automated testing because oftentimes less bytes are
49 // read for various reasons, and this is allowed by the spec.
50 // test3(); // for bug 4638365
51 }
52
53 private static void test1() throws Exception {
54 ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];
55 for (int i=0; i<NUM_BUFFERS; i++)
56 dstBuffers[i] = ByteBuffer.allocateDirect(BUFFER_CAP);
57 File blah = File.createTempFile("blah1", null);
58 blah.deleteOnExit();
59 createTestFile(blah);
60
61 FileInputStream fis = new FileInputStream(blah);
62 FileChannel fc = fis.getChannel();
63
64 byte expectedResult = -128;
65 for (int k=0; k<20; k++) {
66 long bytesRead = fc.read(dstBuffers);
67 for (int i=0; i<NUM_BUFFERS; i++) {
68 for (int j=0; j<BUFFER_CAP; j++) {
69 byte b = dstBuffers[i].get(j);
70 if (b != expectedResult++)
71 throw new RuntimeException("Test failed");
72 }
73 dstBuffers[i].flip();
74 }
75 }
76 fis.close();
77 }
78
79 private static void createTestFile(File blah) throws Exception {
80 FileOutputStream fos = new FileOutputStream(blah);
81 for(int i=-128; i<128; i++)
82 fos.write((byte)i);
83 fos.flush();
84 fos.close();
85 }
86
87 private static void test2() throws Exception {
88 ByteBuffer dstBuffers[] = new ByteBuffer[2];
89 for (int i=0; i<2; i++)
90 dstBuffers[i] = ByteBuffer.allocateDirect(10);
91 File blah = File.createTempFile("blah2", null);
92 blah.deleteOnExit();
93 FileOutputStream fos = new FileOutputStream(blah);
94 for(int i=0; i<15; i++)
95 fos.write((byte)92);
96 fos.flush();
97 fos.close();
98
99 FileInputStream fis = new FileInputStream(blah);
100 FileChannel fc = fis.getChannel();
101
102 long bytesRead = fc.read(dstBuffers);
103 if (dstBuffers[1].limit() != 10)
104 throw new Exception("Scattering read changed buf limit.");
105 fis.close();
106 }
107
108 private static void test3() throws Exception {
109 // Only works on 64 bit Solaris
110 String osName = System.getProperty("os.name");
111 if (!osName.startsWith("SunOS"))
112 return;
113 String dataModel = System.getProperty("sun.arch.data.model");
114 if (!dataModel.startsWith("64"))
115 return;
116
117 ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];
118 File f = File.createTempFile("test3", null);
119 f.deleteOnExit();
120 prepTest3File(f, (long)BIG_BUFFER_CAP);
121 RandomAccessFile raf = new RandomAccessFile(f, "rw");
122 FileChannel fc = raf.getChannel();
123 MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0,
124 BIG_BUFFER_CAP);
125 for (int i=0; i<NUM_BUFFERS; i++) {
126 dstBuffers[i] = mbb;
127 }
128 fc.close();
129 raf.close();
130
131 // Source must be large
132 FileInputStream fis = new FileInputStream("/dev/zero");
133 fc = fis.getChannel();
134
135 long bytesRead = fc.read(dstBuffers);
136 if (bytesRead <= Integer.MAX_VALUE)
137 throw new RuntimeException("Test 3 failed "+bytesRead+" < "+Integer.MAX_VALUE);
138
139 fc.close();
140 fis.close();
141 }
142
143 static void prepTest3File(File blah, long testSize) throws Exception {
144 RandomAccessFile raf = new RandomAccessFile(blah, "rw");
145 FileChannel fc = raf.getChannel();
146 fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
147 fc.close();
148 raf.close();
149 }
150
151}