Path: blob/master/test/jdk/java/nio/channels/FileChannel/AtomicAppend.java
41154 views
/*1* Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Check that appends are atomic26* @key randomness27*/2829import java.io.File;30import java.io.FileOutputStream;31import java.io.OutputStream;32import java.io.IOException;33import java.util.Random;34import java.util.concurrent.Executors;35import java.util.concurrent.ExecutorService;36import java.util.concurrent.TimeUnit;37import java.nio.ByteBuffer;38import java.nio.channels.FileChannel;39import java.nio.file.Files;40import static java.nio.file.StandardOpenOption.*;4142public class AtomicAppend {43static final Random rand = new Random();4445// Open file for appending, returning FileChannel46static FileChannel newFileChannel(File file) throws IOException {47if (rand.nextBoolean()) {48return new FileOutputStream(file, true).getChannel();49} else {50return FileChannel.open(file.toPath(), APPEND);51}52}5354// Open file for append, returning OutputStream55static OutputStream newOutputStream(File file) throws IOException {56if (rand.nextBoolean()) {57return new FileOutputStream(file, true);58} else {59return Files.newOutputStream(file.toPath(), APPEND);60}61}6263// write a byte to the given channel64static void write(FileChannel fc, int b) throws IOException {65ByteBuffer buf = ByteBuffer.allocate(1);66buf.put((byte)b);67buf.flip();68if (rand.nextBoolean()) {69ByteBuffer[] bufs = new ByteBuffer[1];70bufs[0] = buf;71fc.write(bufs);72} else {73fc.write(buf);74}75}7677public static void main(String[] args) throws Throwable {78final int nThreads = 16;79final int writes = 1000;80final File file = File.createTempFile("foo", null);81try {82ExecutorService pool = Executors.newFixedThreadPool(nThreads);83for (int i = 0; i < nThreads; i++)84pool.execute(new Runnable() { public void run() {85try {86// randomly choose FileChannel or OutputStream87if (rand.nextBoolean()) {88try (FileChannel fc = newFileChannel(file)) {89for (int j=0; j<writes; j++) write(fc, 'x');90}91} else {92try (OutputStream out = newOutputStream(file)) {93for (int j = 0; j<writes; j++) out.write('x');94}95}96} catch (IOException ioe) {97ioe.printStackTrace();98}99}});100pool.shutdown();101pool.awaitTermination(1L, TimeUnit.MINUTES);102if (file.length() != (long) (nThreads * writes))103throw new RuntimeException("File not expected length");104} finally {105file.delete();106}107}108}109110111