1 /*
   2  * Copyright (c) 2010, 2011, 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  * @summary Check that appends are atomic
  27  */
  28 
  29 import java.io.File;
  30 import java.io.FileOutputStream;
  31 import java.io.OutputStream;
  32 import java.io.IOException;
  33 import java.util.Random;
  34 import java.util.concurrent.Executors;
  35 import java.util.concurrent.ExecutorService;
  36 import java.util.concurrent.TimeUnit;
  37 import java.nio.ByteBuffer;
  38 import java.nio.channels.FileChannel;
  39 import java.nio.file.Files;
  40 import static java.nio.file.StandardOpenOption.*;
  41 
  42 public class AtomicAppend {
  43     static final Random rand = new Random();
  44 
  45     // Open file for appending, returning FileChannel
  46     static FileChannel newFileChannel(File file) throws IOException {
  47         if (rand.nextBoolean()) {
  48             return new FileOutputStream(file, true).getChannel();
  49         } else {
  50             return FileChannel.open(file.toPath(), APPEND);
  51         }
  52     }
  53 
  54     // Open file for append, returning OutputStream
  55     static OutputStream newOutputStream(File file) throws IOException {
  56         if (rand.nextBoolean()) {
  57             return new FileOutputStream(file, true);
  58         } else {
  59             return Files.newOutputStream(file.toPath(), APPEND);
  60         }
  61     }
  62 
  63     // write a byte to the given channel
  64     static void write(FileChannel fc, int b) throws IOException {
  65         ByteBuffer buf = ByteBuffer.allocate(1);
  66         buf.put((byte)b);
  67         buf.flip();
  68         if (rand.nextBoolean()) {
  69             ByteBuffer[] bufs = new ByteBuffer[1];
  70             bufs[0] = buf;
  71             fc.write(bufs);
  72         } else {
  73             fc.write(buf);
  74         }
  75     }
  76 
  77     public static void main(String[] args) throws Throwable {
  78         final int nThreads = 16;
  79         final int writes = 1000;
  80         final File file = File.createTempFile("foo", null);
  81         try {
  82             ExecutorService pool = Executors.newFixedThreadPool(nThreads);
  83             for (int i = 0; i < nThreads; i++)
  84                 pool.execute(new Runnable() { public void run() {
  85                     try {
  86                         // randomly choose FileChannel or OutputStream
  87                         if (rand.nextBoolean()) {
  88                             try (FileChannel fc = newFileChannel(file)) {
  89                                 for (int j=0; j<writes; j++) write(fc, 'x');
  90                             }
  91                         } else {
  92                             try (OutputStream out = newOutputStream(file)) {
  93                                 for (int j = 0; j<writes; j++) out.write('x');
  94                             }
  95                         }
  96                     } catch (IOException ioe) {
  97                         ioe.printStackTrace();
  98                     }
  99                 }});
 100             pool.shutdown();
 101             pool.awaitTermination(1L, TimeUnit.MINUTES);
 102             if (file.length() != (long) (nThreads * writes))
 103                 throw new RuntimeException("File not expected length");
 104         } finally {
 105             file.delete();
 106         }
 107     }
 108 }