1 /*
   2  * Copyright (c) 2012, 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 import java.io.File;
  24 import java.io.FileNotFoundException;
  25 import java.io.IOException;
  26 import java.nio.ByteBuffer;
  27 import java.nio.channels.FileChannel;
  28 import java.nio.file.StandardOpenOption;
  29 
  30 /*
  31  * @test
  32  * @run shell ioTraceTest.sh IoTraceFileChannelReadWrite
  33  */
  34 public class IoTraceFileChannelReadWrite extends IoTraceBase {
  35 
  36     private void testWrite(File f) throws IOException, FileNotFoundException,
  37             Exception {
  38         try (FileChannel fc = FileChannel.open(f.toPath(),
  39                 StandardOpenOption.WRITE)) {
  40             ByteBuffer bb = ByteBuffer.allocate(1);
  41             bb.put((byte) 11);
  42             bb.flip();
  43             fc.write(bb);
  44         }
  45         expectFile(1, 0, f);
  46     }
  47 
  48     private void testRead(File f) throws IOException, FileNotFoundException,
  49             Exception {
  50         try (FileChannel fc = FileChannel.open(f.toPath(),
  51                 StandardOpenOption.READ)) {
  52             ByteBuffer bb = ByteBuffer.allocate(1);
  53             fc.read(bb);
  54         }
  55         expectFile(0, 1, f);
  56     }
  57 
  58     public void test() throws Exception {
  59         IoTraceAgent.setListener(this);
  60         File f = File.createTempFile("IoTraceFileChannelReadWrite", ".bin");
  61         try {
  62             clear();
  63             testWrite(f);
  64             clear();
  65             testRead(f);
  66         } finally {
  67             f.delete();
  68         }
  69     }
  70 
  71     public static void main(String... args) throws Exception {
  72         IoTraceFileChannelReadWrite t = new IoTraceFileChannelReadWrite();
  73         t.test();
  74     }
  75 }