Path: blob/master/test/jdk/java/nio/channels/FileChannel/Pwrite.java
41154 views
/*1* Copyright (c) 2000, 2010, 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/* @test24* @bug 486241125* @summary Test positional write method of FileChannel26* @key randomness27*/2829import java.io.*;30import java.nio.ByteBuffer;31import java.nio.CharBuffer;32import java.nio.channels.*;33import java.nio.channels.FileChannel;34import java.util.Random;353637/**38* Testing FileChannel's positional write method.39*/40public class Pwrite {4142private static Random generator = new Random();4344private static File blah;4546public static void main(String[] args) throws Exception {47genericTest();48testUnwritableChannel();49}5051// This test for bug 486241152private static void testUnwritableChannel() throws Exception {53File blah = File.createTempFile("blah2", null);54blah.deleteOnExit();55FileOutputStream fos = new FileOutputStream(blah);56fos.write(new byte[128]);57fos.close();58FileInputStream fis = new FileInputStream(blah);59FileChannel fc = fis.getChannel();60try {61fc.write(ByteBuffer.allocate(256),1);62throw new RuntimeException("Expected exception not thrown");63} catch(NonWritableChannelException e) {64// Correct result65} finally {66fc.close();67blah.delete();68}69}7071private static void genericTest() throws Exception {72StringBuffer sb = new StringBuffer();73sb.setLength(4);7475blah = File.createTempFile("blah", null);76blah.deleteOnExit();77initTestFile(blah);7879RandomAccessFile raf = new RandomAccessFile(blah, "rw");80FileChannel c = raf.getChannel();8182for (int x=0; x<100; x++) {83long offset = generator.nextInt(1000);84ByteBuffer bleck = ByteBuffer.allocateDirect(4);8586// Write known sequence out87for (byte i=0; i<4; i++) {88bleck.put(i);89}90bleck.flip();91long originalPosition = c.position();92int totalWritten = 0;93while (totalWritten < 4) {94int written = c.write(bleck, offset);95if (written < 0)96throw new Exception("Read failed");97totalWritten += written;98}99100long newPosition = c.position();101102// Ensure that file pointer position has not changed103if (originalPosition != newPosition)104throw new Exception("File position modified");105106// Attempt to read sequence back in107bleck = ByteBuffer.allocateDirect(4);108originalPosition = c.position();109int totalRead = 0;110while (totalRead < 4) {111int read = c.read(bleck, offset);112if (read < 0)113throw new Exception("Read failed");114totalRead += read;115}116newPosition = c.position();117118// Ensure that file pointer position has not changed119if (originalPosition != newPosition)120throw new Exception("File position modified");121122for (byte i=0; i<4; i++) {123if (bleck.get(i) != i)124throw new Exception("Write test failed");125}126}127c.close();128raf.close();129blah.delete();130}131132/**133* Creates file blah:134* 0000135* 0001136* 0002137* 0003138* .139* .140* .141* 3999142*143* Blah extends beyond a single page of memory so that the144* ability to index into a file of multiple pages is tested.145*/146private static void initTestFile(File blah) throws Exception {147FileOutputStream fos = new FileOutputStream(blah);148BufferedWriter awriter149= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));150151for(int i=0; i<4000; i++) {152String number = new Integer(i).toString();153for (int h=0; h<4-number.length(); h++)154awriter.write("0");155awriter.write(""+i);156awriter.newLine();157}158awriter.flush();159awriter.close();160}161}162163164