1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8164900
* @summary Test read method of FileChannel with DirectIO
* (use -Dseed=X to set PRNG seed)
* @library .. /test/lib
* @build jdk.test.lib.RandomFactory
* DirectIOTest
* @run main/othervm ReadDirect
* @key randomness
*/
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import com.sun.nio.file.ExtendedOpenOption;
import jdk.test.lib.RandomFactory;
public class ReadDirect {
private static PrintStream err = System.err;
private static Random generator = RandomFactory.getRandom();
private static int charsPerGroup = -1;
private static int alignment = -1;
private static boolean initTests() throws Exception {
Path p = DirectIOTest.createTempFile();
if (!DirectIOTest.isDirectIOSupportedByFS(p)) {
Files.delete(p);
return false;
}
try {
FileStore fs = Files.getFileStore(p);
alignment = (int)fs.getBlockSize();
charsPerGroup = alignment;
} finally {
Files.delete(p);
}
return true;
}
private static void testWithSingleBuffer() throws Exception {
StringBuffer sb = new StringBuffer();
sb.setLength(2);
Path p = DirectIOTest.createTempFile();
initTestFile(p);
try (FileChannel fc = FileChannel.open(p,
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
ExtendedOpenOption.DIRECT)) {
ByteBuffer block = ByteBuffer.allocateDirect(charsPerGroup
+ alignment - 1).alignedSlice(alignment);
for (int x = 0; x < 100; x++) {
block.clear();
long offset = x * charsPerGroup;
long expectedResult = offset / charsPerGroup;
fc.read(block);
for (int i = 0; i < 2; i++) {
byte aByte = block.get(i);
sb.setCharAt(i, (char)aByte);
}
int result = Integer.parseInt(sb.toString());
if (result != expectedResult) {
err.println("I expected " + expectedResult);
err.println("I got " + result);
throw new Exception("Read test failed");
}
}
}
}
private static void testWithNotAlignedBufferSize() throws Exception {
int bufferSize = charsPerGroup - 1;
Path p = DirectIOTest.createTempFile();
try (OutputStream fos = Files.newOutputStream(p)) {
fos.write(new byte[bufferSize]);
}
try (FileChannel fc = FileChannel.open(p,
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
ExtendedOpenOption.DIRECT)) {
ByteBuffer block = ByteBuffer.allocate(bufferSize);
try {
fc.read(block);
throw new RuntimeException("Expected exception not thrown");
} catch (IOException e) {
if (!e.getMessage().contains("Number of remaining bytes ("
+ bufferSize + ") is not a multiple of the block size ("
+ alignment + ")"))
throw new Exception("Read test failed");
}
}
}
private static void testWithNotAlignedBufferOffset() throws Exception {
int bufferSize = charsPerGroup * 2;
int pos = alignment - 1;
Path p = DirectIOTest.createTempFile();
try (OutputStream fos = Files.newOutputStream(p)) {
fos.write(new byte[bufferSize]);
}
try (FileChannel fc = FileChannel.open(p,
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
ExtendedOpenOption.DIRECT)) {
ByteBuffer block = ByteBuffer.allocateDirect(bufferSize);
block.position(pos);
block.limit(bufferSize - 1);
try {
fc.read(block);
throw new RuntimeException("Expected exception not thrown");
} catch (IOException e) {
if (!e.getMessage().contains("Current location of the bytebuffer "
+ "(" + pos + ") is not a multiple of the block size ("
+ alignment + ")"))
throw new Exception("Read test failed");
}
}
}
private static void testWithArrayOfBuffer() throws Exception {
StringBuffer sb = new StringBuffer();
sb.setLength(2);
ByteBuffer[] dests = new ByteBuffer[4];
Path p = DirectIOTest.createTempFile();
initTestFile(p);
try (FileChannel fc = FileChannel.open(p,
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
ExtendedOpenOption.DIRECT)) {
int randomNumber = -1;
for (int i = 0; i < 4; i++) {
dests[i] = ByteBuffer.allocateDirect
(charsPerGroup + alignment - 1).alignedSlice(alignment);
for (int j = 0; j < charsPerGroup; j++) {
dests[i].put(j, (byte)'a');
}
}
// The size of the test FileChannel is 100*charsPerGroup.
// As the channel bytes will be scattered into two buffers
// each of size charsPerGroup, the offset cannot be greater
// than 98*charsPerGroup, so the value of randomNumber must
// be in the range [0,98], i.e., 0 <= randomNumber < 99.
randomNumber = generator.nextInt(99);
long offset = randomNumber * charsPerGroup;
fc.position(offset);
fc.read(dests, 1, 2);
for (int i = 0; i < 4; i++) {
if (i == 1 || i == 2) {
for (int j = 0; j < 2; j++) {
byte aByte = dests[i].get(j);
sb.setCharAt(j, (char)aByte);
}
int result = Integer.parseInt(sb.toString());
int expectedResult = randomNumber + i - 1;
if (result != expectedResult) {
err.println("I expected " + expectedResult);
err.println("I got " + result);
throw new Exception("Read test failed");
}
} else {
for (int k = 0; k < charsPerGroup; k++) {
if (dests[i].get(k) != (byte)'a')
throw new RuntimeException("Read test failed");
}
}
}
}
}
public static void testOnEOF() throws Exception {
int bufferSize = charsPerGroup / 2;
Path p = DirectIOTest.createTempFile();
try (OutputStream fos = Files.newOutputStream(p)) {
byte[] writeBlock = new byte[bufferSize];
for (int i = 0; i < bufferSize; i++) {
writeBlock[i] = ((byte)'a');
}
fos.write(writeBlock);
}
try (FileChannel fc = FileChannel.open(p,
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE,
ExtendedOpenOption.DIRECT)) {
ByteBuffer block = ByteBuffer.allocateDirect(
(bufferSize / alignment + 1) * alignment + alignment - 1)
.alignedSlice(alignment);
int result = fc.read(block);
if (result != bufferSize) {
err.println("Number of bytes to read " + bufferSize);
err.println("I read " + result);
throw new Exception("Read test failed");
}
for (int j = 0; j < bufferSize; j++) {
if (block.get(j) != (byte)'a')
throw new RuntimeException("Read test failed");
}
}
}
public static void main(String[] args) throws Exception {
if (initTests()) {
testWithSingleBuffer();
testWithNotAlignedBufferSize();
testWithNotAlignedBufferOffset();
testWithArrayOfBuffer();
testOnEOF();
}
}
private static void initTestFile(Path p)
throws Exception {
try (OutputStream fos = Files.newOutputStream(p)) {
try (BufferedWriter awriter
= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"))) {
for (int i = 0; i < 100; i++) {
String number = new Integer(i).toString();
for (int h = 0; h < 2 - number.length(); h++)
awriter.write("0");
awriter.write("" + i);
for (int j = 0; j < (charsPerGroup - 2); j++)
awriter.write("0");
}
awriter.flush();
}
}
}
}
|