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 /* @test
  25  * @bug 8164900
  26  * @summary Test positional write method of FileChannel with DirectIO
  27  * (use -Dseed=X to set PRNG seed)
  28  * @library .. /test/lib
  29  * @build jdk.test.lib.RandomFactory
  30  * @run main PwriteDirect
  31  * @key randomness
  32  */
  33 
  34 import java.io.*;
  35 import java.nio.ByteBuffer;
  36 import java.nio.CharBuffer;
  37 import java.nio.channels.*;
  38 import java.nio.channels.FileChannel;
  39 import java.util.Random;
  40 import java.nio.file.Paths;
  41 import java.nio.file.Path;
  42 import java.nio.file.Files;
  43 import java.nio.file.FileStore;
  44 import java.nio.file.StandardOpenOption;
  45 import com.sun.nio.file.ExtendedOpenOption;
  46 
  47 import jdk.test.lib.RandomFactory;
  48 
  49 /**
  50  * Testing FileChannel's positional write method.
  51  */
  52 public class PwriteDirect {
  53 
  54     private static Random generator = RandomFactory.getRandom();
  55 
  56     public static void main(String[] args) throws Exception {
  57         genericTest();
  58         testUnwritableChannel();
  59     }
  60 
  61     private static void testUnwritableChannel() throws Exception {
  62         Path p = Files.createTempFile(Paths.get(
  63                 System.getProperty("test.dir", ".")), "test", null);
  64         OutputStream fos = Files.newOutputStream(p);
  65         fos.write(new byte[4096]);
  66         fos.close();
  67 
  68         FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT);
  69 
  70         try {
  71             fc.write(ByteBuffer.allocate(4096),0);
  72             throw new RuntimeException("Expected exception not thrown");
  73         } catch(NonWritableChannelException e) {
  74             // Correct result
  75         } finally {
  76             fc.close();
  77             Files.delete(p);
  78         }
  79     }
  80 
  81     private static void genericTest() throws Exception {
  82         Path p = Files.createTempFile(Paths.get(
  83                 System.getProperty("test.dir", ".")), "test", null);
  84         initTestFile(p);
  85 
  86         FileChannel c = FileChannel.open(p, StandardOpenOption.WRITE,
  87                 StandardOpenOption.READ, ExtendedOpenOption.DIRECT);
  88         FileStore fs = Files.getFileStore(p);
  89         int alignment = (int)fs.getBlockSize();
  90 
  91         for (int x=0; x<100; x++) {
  92             long offset = generator.nextInt(100) * 4096;
  93             ByteBuffer block = ByteBuffer.allocateDirect(4096 + alignment - 1)
  94                                          .alignedSlice(alignment);
  95 
  96             // Write known sequence out
  97             for (byte i=0; i<4; i++) {
  98                 block.put(i);
  99             }
 100             block.position(0);
 101             long originalPosition = c.position();
 102             int totalWritten = 0;
 103             while (totalWritten < 4096) {
 104                 int written = c.write(block, offset);
 105                 if (written < 0)
 106                     throw new Exception("Write failed");
 107                 totalWritten += written;
 108             }
 109 
 110             long newPosition = c.position();
 111 
 112             // Ensure that file pointer position has not changed
 113             if (originalPosition != newPosition)
 114                 throw new Exception("File position modified");
 115 
 116             // Attempt to read sequence back in
 117             block = ByteBuffer.allocateDirect(4096 + alignment - 1)
 118                               .alignedSlice(alignment);
 119             originalPosition = c.position();
 120             int totalRead = 0;
 121             while (totalRead < 4096) {
 122                 int read = c.read(block, offset);
 123                 if (read < 0)
 124                     throw new Exception("Read failed");
 125                 totalRead += read;
 126             }
 127             newPosition = c.position();
 128 
 129             // Ensure that file pointer position has not changed
 130             if (originalPosition != newPosition)
 131                 throw new Exception("File position modified");
 132 
 133             for (byte i=0; i<4; i++) {
 134                 if (block.get(i) != i)
 135                     throw new Exception("Write test failed");
 136             }
 137         }
 138         c.close();
 139         Files.delete(p);
 140     }
 141 
 142     private static void initTestFile(Path p) throws Exception {
 143         OutputStream fos = Files.newOutputStream(p);
 144         BufferedWriter awriter
 145             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 146 
 147         for(int i=0; i<100; i++) {
 148             String number = new Integer(i).toString();
 149             for (int h=0; h<4-number.length(); h++)
 150                 awriter.write("0");
 151             awriter.write(""+i);
 152             for (int j=0; j < 4092; j++)
 153                 awriter.write("0");
 154         }
 155        awriter.flush();
 156        awriter.close();
 157     }
 158 }