47428
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 8164900
|
|
27 |
* @summary Test FileChannel write with DirectIO
|
|
28 |
* @library .. /test/lib
|
|
29 |
* @build DirectIOTest
|
|
30 |
* @run main/othervm WriteDirect
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.*;
|
|
34 |
import java.nio.*;
|
|
35 |
import java.nio.channels.*;
|
|
36 |
import java.nio.file.Files;
|
|
37 |
import java.nio.file.FileStore;
|
|
38 |
import java.nio.file.Path;
|
|
39 |
import java.nio.file.Paths;
|
|
40 |
import java.nio.file.StandardOpenOption;
|
|
41 |
import com.sun.nio.file.ExtendedOpenOption;
|
|
42 |
|
|
43 |
public class WriteDirect {
|
|
44 |
|
|
45 |
private static int charsPerGroup = -1;
|
|
46 |
|
|
47 |
private static int alignment = -1;
|
|
48 |
|
|
49 |
private static boolean initTests() throws Exception {
|
|
50 |
Path p = DirectIOTest.createTempFile();
|
|
51 |
if (!DirectIOTest.isDirectIOSupportedByFS(p)) {
|
|
52 |
Files.delete(p);
|
|
53 |
return false;
|
|
54 |
}
|
|
55 |
try {
|
|
56 |
FileStore fs = Files.getFileStore(p);
|
|
57 |
alignment = (int)fs.getBlockSize();
|
|
58 |
charsPerGroup = alignment;
|
|
59 |
} finally {
|
|
60 |
Files.delete(p);
|
|
61 |
}
|
|
62 |
return true;
|
|
63 |
}
|
|
64 |
|
|
65 |
public static void main(String[] args) throws Exception {
|
|
66 |
if (initTests()) {
|
|
67 |
testWithNotAlignedBuffer();
|
|
68 |
testWithNotAlignedBufferOffset();
|
|
69 |
testWithArrayOfBuffer();
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
static void testWithNotAlignedBuffer() throws Exception {
|
|
74 |
Path p = DirectIOTest.createTempFile();
|
|
75 |
try (FileChannel fc = FileChannel.open(p,
|
|
76 |
StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,
|
|
77 |
ExtendedOpenOption.DIRECT)) {
|
|
78 |
int bufferSize = charsPerGroup - 1;
|
|
79 |
ByteBuffer src = ByteBuffer.allocate(bufferSize);
|
|
80 |
try {
|
|
81 |
fc.write(src);
|
|
82 |
throw new RuntimeException("Expected exception not thrown");
|
|
83 |
} catch (IOException e) {
|
|
84 |
if (!e.getMessage().contains("Number of remaining bytes ("
|
|
85 |
+ bufferSize + ") is not a multiple of the block size ("
|
|
86 |
+ alignment + ")"))
|
|
87 |
throw new Exception("Write failure");
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
private static void testWithNotAlignedBufferOffset() throws Exception {
|
|
93 |
int bufferSize = charsPerGroup * 2;
|
|
94 |
int pos = alignment - 1;
|
|
95 |
|
|
96 |
Path p = DirectIOTest.createTempFile();
|
|
97 |
|
|
98 |
try (FileChannel fc = FileChannel.open(p,
|
|
99 |
StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,
|
|
100 |
ExtendedOpenOption.DIRECT)) {
|
|
101 |
ByteBuffer block = ByteBuffer.allocateDirect(bufferSize);
|
|
102 |
block.position(pos);
|
|
103 |
block.limit(bufferSize - 1);
|
|
104 |
try {
|
|
105 |
fc.write(block);
|
|
106 |
throw new RuntimeException("Expected exception not thrown");
|
|
107 |
} catch (IOException e) {
|
|
108 |
if (!e.getMessage().contains("Current location of the bytebuffer "
|
|
109 |
+ "(" + pos + ") is not a multiple of the block size ("
|
|
110 |
+ alignment + ")"))
|
|
111 |
throw new Exception("Write test failed");
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
static void testWithArrayOfBuffer() throws Exception {
|
|
117 |
Path p = DirectIOTest.createTempFile();
|
|
118 |
ByteBuffer[] srcs = new ByteBuffer[4];
|
|
119 |
try (FileChannel fc = FileChannel.open(p,
|
|
120 |
StandardOpenOption.WRITE, ExtendedOpenOption.DIRECT)) {
|
|
121 |
for (int i = 0; i < 4; i++) {
|
|
122 |
srcs[i] = ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)
|
|
123 |
.alignedSlice(alignment);
|
|
124 |
for (int j = 0; j < charsPerGroup; j++) {
|
|
125 |
srcs[i].put((byte)i);
|
|
126 |
}
|
|
127 |
srcs[i].flip();
|
|
128 |
}
|
|
129 |
|
|
130 |
fc.write(srcs, 1, 2);
|
|
131 |
}
|
|
132 |
|
|
133 |
try (FileChannel fc = FileChannel.open(p,
|
|
134 |
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE)) {
|
|
135 |
ByteBuffer bb = ByteBuffer.allocateDirect(charsPerGroup * 2);
|
|
136 |
fc.read(bb);
|
|
137 |
bb.flip();
|
|
138 |
for (int k = 0; k < charsPerGroup; k++) {
|
|
139 |
if (bb.get() != 1)
|
|
140 |
throw new RuntimeException("Write failure");
|
|
141 |
}
|
|
142 |
for (int m = 0; m < charsPerGroup; m++) {
|
|
143 |
if (bb.get() != 2)
|
|
144 |
throw new RuntimeException("Write failure");
|
|
145 |
}
|
|
146 |
try {
|
|
147 |
bb.get();
|
|
148 |
throw new RuntimeException("Write failure");
|
|
149 |
} catch (BufferUnderflowException bufe) {
|
|
150 |
// correct result
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|