2
|
1 |
/*
|
|
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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4475533 4698138 4638365 4796221
|
|
27 |
* @summary Test FileChannel write
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.nio.channels.*;
|
|
31 |
import java.nio.*;
|
|
32 |
import java.io.*;
|
|
33 |
|
|
34 |
public class Write {
|
|
35 |
|
|
36 |
public static void main(String[] args) throws Exception {
|
|
37 |
test1(); // for bug 4475533
|
|
38 |
test2();
|
|
39 |
test3(); // for bug 4698138
|
|
40 |
|
|
41 |
// This test is not suitable for automated testing at this time.
|
|
42 |
// I am commenting it out but it will be easy to manually
|
|
43 |
// test for a regression in this area. See also 4796221.
|
|
44 |
//test4(); // for bug 4638365
|
|
45 |
}
|
|
46 |
|
|
47 |
// Test to see that offset > length does not throw exception
|
|
48 |
static void test1() throws Exception {
|
|
49 |
File testFile = File.createTempFile("test1", null);
|
|
50 |
testFile.deleteOnExit();
|
|
51 |
|
|
52 |
ByteBuffer[] dsts = new ByteBuffer[4];
|
|
53 |
for (int i=0; i<4; i++)
|
|
54 |
dsts[i] = ByteBuffer.allocateDirect(10);
|
|
55 |
|
|
56 |
FileOutputStream fos = new FileOutputStream(testFile);
|
|
57 |
FileChannel fc = fos.getChannel();
|
|
58 |
fc.write(dsts, 2, 1);
|
|
59 |
fos.close();
|
|
60 |
}
|
|
61 |
|
|
62 |
// Test to see that the appropriate buffers are updated
|
|
63 |
static void test2() throws Exception {
|
|
64 |
File testFile = File.createTempFile("test2", null);
|
|
65 |
testFile.deleteOnExit();
|
|
66 |
ByteBuffer[] srcs = new ByteBuffer[4];
|
|
67 |
for (int i=0; i<4; i++)
|
|
68 |
srcs[i] = ByteBuffer.allocateDirect(10);
|
|
69 |
|
|
70 |
srcs[0].put((byte)1); srcs[0].flip();
|
|
71 |
srcs[1].put((byte)2); srcs[1].flip();
|
|
72 |
srcs[2].put((byte)3); srcs[2].flip();
|
|
73 |
srcs[3].put((byte)4); srcs[3].flip();
|
|
74 |
|
|
75 |
FileOutputStream fos = new FileOutputStream(testFile);
|
|
76 |
FileChannel fc = fos.getChannel();
|
|
77 |
fc.write(srcs, 1, 2);
|
|
78 |
fos.close();
|
|
79 |
|
|
80 |
FileInputStream fis = new FileInputStream(testFile);
|
|
81 |
fc = fis.getChannel();
|
|
82 |
ByteBuffer bb = ByteBuffer.allocateDirect(10);
|
|
83 |
fc.read(bb);
|
|
84 |
bb.flip();
|
|
85 |
if (bb.get() != 2)
|
|
86 |
throw new RuntimeException("Write failure");
|
|
87 |
if (bb.get() != 3)
|
|
88 |
throw new RuntimeException("Write failure");
|
|
89 |
try {
|
|
90 |
bb.get();
|
|
91 |
throw new RuntimeException("Write failure");
|
|
92 |
} catch (BufferUnderflowException bufe) {
|
|
93 |
// correct result
|
|
94 |
}
|
|
95 |
fis.close();
|
|
96 |
}
|
|
97 |
|
|
98 |
// Test write to a negative position (bug 4698138).
|
|
99 |
static void test3() throws Exception {
|
|
100 |
File testFile = File.createTempFile("test1", null);
|
|
101 |
testFile.deleteOnExit();
|
|
102 |
ByteBuffer dst = ByteBuffer.allocate(10);
|
|
103 |
FileOutputStream fos = new FileOutputStream(testFile);
|
|
104 |
FileChannel fc = fos.getChannel();
|
|
105 |
try {
|
|
106 |
fc.write(dst, -1);
|
|
107 |
throw new RuntimeException("Expected IAE not thrown");
|
|
108 |
} catch (IllegalArgumentException iae) {
|
|
109 |
// Correct result
|
|
110 |
} finally {
|
|
111 |
fos.close();
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
private static final int TEST4_NUM_BUFFERS = 3;
|
|
116 |
|
|
117 |
private static final int TEST4_BUF_CAP = Integer.MAX_VALUE / 2;
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Test to see that vector write can return > Integer.MAX_VALUE
|
|
121 |
*
|
|
122 |
* Note that under certain circumstances disk space problems occur
|
|
123 |
* with this test. It typically relies upon adequate disk space and/or
|
|
124 |
* a Solaris disk space optimization where empty files take up less
|
|
125 |
* space than their logical size.
|
|
126 |
*
|
|
127 |
* Note that if this test fails it is not necessarily a violation of
|
|
128 |
* spec: the value returned by fc.write can be smaller than the number
|
|
129 |
* of bytes requested to write. It is testing an optimization that allows
|
|
130 |
* for larger return values.
|
|
131 |
*/
|
|
132 |
static void test4() throws Exception {
|
|
133 |
// Only works on 64 bit Solaris
|
|
134 |
String osName = System.getProperty("os.name");
|
|
135 |
if (!osName.startsWith("SunOS"))
|
|
136 |
return;
|
|
137 |
String dataModel = System.getProperty("sun.arch.data.model");
|
|
138 |
if (!dataModel.startsWith("64"))
|
|
139 |
return;
|
|
140 |
|
|
141 |
File testFile = File.createTempFile("test4", null);
|
|
142 |
testFile.deleteOnExit();
|
|
143 |
|
|
144 |
FileChannel[] fcs = new FileChannel[TEST4_NUM_BUFFERS];
|
|
145 |
|
|
146 |
ByteBuffer[] dsts = new ByteBuffer[TEST4_NUM_BUFFERS];
|
|
147 |
// Map these buffers from a file so we don't run out of memory
|
|
148 |
for (int i=0; i<TEST4_NUM_BUFFERS; i++) {
|
|
149 |
File f = File.createTempFile("test4." + i, null);
|
|
150 |
f.deleteOnExit();
|
|
151 |
prepTest4File(f);
|
|
152 |
FileInputStream fis = new FileInputStream(f);
|
|
153 |
FileChannel fc = fis.getChannel();
|
|
154 |
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
|
|
155 |
TEST4_BUF_CAP);
|
|
156 |
dsts[i] = mbb;
|
|
157 |
}
|
|
158 |
|
|
159 |
FileOutputStream fos = new FileOutputStream(testFile);
|
|
160 |
FileChannel fc = fos.getChannel();
|
|
161 |
try {
|
|
162 |
long bytesWritten = fc.write(dsts);
|
|
163 |
if (bytesWritten < Integer.MAX_VALUE) {
|
|
164 |
// Note: this is not a violation of the spec
|
|
165 |
throw new RuntimeException("Test 4 failed but wrote " +
|
|
166 |
bytesWritten);
|
|
167 |
}
|
|
168 |
} finally {
|
|
169 |
fc.close();
|
|
170 |
fos.close();
|
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
static void prepTest4File(File blah) throws Exception {
|
|
175 |
RandomAccessFile raf = new RandomAccessFile(blah, "rw");
|
|
176 |
FileChannel fc = raf.getChannel();
|
|
177 |
fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
|
|
178 |
TEST4_BUF_CAP);
|
|
179 |
fc.close();
|
|
180 |
raf.close();
|
|
181 |
}
|
|
182 |
|
|
183 |
}
|