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