0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4862411
0N/A * @summary Test positional write method of FileChannel
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/Aimport java.nio.channels.*;
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport java.util.Random;
0N/A
0N/A
0N/A/**
0N/A * Testing FileChannel's positional write method.
0N/A */
0N/Apublic class Pwrite {
0N/A
0N/A private static Random generator = new Random();
0N/A
0N/A private static File blah;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A genericTest();
0N/A testUnwritableChannel();
0N/A }
0N/A
0N/A // This test for bug 4862411
0N/A private static void testUnwritableChannel() throws Exception {
0N/A File blah = File.createTempFile("blah2", null);
0N/A blah.deleteOnExit();
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A fos.write(new byte[128]);
0N/A fos.close();
0N/A FileInputStream fis = new FileInputStream(blah);
0N/A FileChannel fc = fis.getChannel();
0N/A try {
0N/A fc.write(ByteBuffer.allocate(256),1);
0N/A throw new RuntimeException("Expected exception not thrown");
0N/A } catch(NonWritableChannelException e) {
0N/A // Correct result
2546N/A } finally {
2546N/A fc.close();
2546N/A blah.delete();
0N/A }
0N/A }
0N/A
0N/A private static void genericTest() throws Exception {
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.setLength(4);
0N/A
0N/A blah = File.createTempFile("blah", null);
0N/A blah.deleteOnExit();
0N/A initTestFile(blah);
0N/A
0N/A RandomAccessFile raf = new RandomAccessFile(blah, "rw");
0N/A FileChannel c = raf.getChannel();
0N/A
0N/A for (int x=0; x<100; x++) {
0N/A long offset = generator.nextInt(1000);
0N/A ByteBuffer bleck = ByteBuffer.allocateDirect(4);
0N/A
0N/A // Write known sequence out
0N/A for (byte i=0; i<4; i++) {
0N/A bleck.put(i);
0N/A }
0N/A bleck.flip();
0N/A long originalPosition = c.position();
0N/A int totalWritten = 0;
0N/A while (totalWritten < 4) {
0N/A int written = c.write(bleck, offset);
0N/A if (written < 0)
0N/A throw new Exception("Read failed");
0N/A totalWritten += written;
0N/A }
0N/A
0N/A long newPosition = c.position();
0N/A
0N/A // Ensure that file pointer position has not changed
0N/A if (originalPosition != newPosition)
0N/A throw new Exception("File position modified");
0N/A
0N/A // Attempt to read sequence back in
0N/A bleck = ByteBuffer.allocateDirect(4);
0N/A originalPosition = c.position();
0N/A int totalRead = 0;
0N/A while (totalRead < 4) {
0N/A int read = c.read(bleck, offset);
0N/A if (read < 0)
0N/A throw new Exception("Read failed");
0N/A totalRead += read;
0N/A }
0N/A newPosition = c.position();
0N/A
0N/A // Ensure that file pointer position has not changed
0N/A if (originalPosition != newPosition)
0N/A throw new Exception("File position modified");
0N/A
0N/A for (byte i=0; i<4; i++) {
0N/A if (bleck.get(i) != i)
0N/A throw new Exception("Write test failed");
0N/A }
0N/A }
0N/A c.close();
0N/A raf.close();
2546N/A blah.delete();
0N/A }
0N/A
0N/A /**
0N/A * Creates file blah:
0N/A * 0000
0N/A * 0001
0N/A * 0002
0N/A * 0003
0N/A * .
0N/A * .
0N/A * .
0N/A * 3999
0N/A *
0N/A * Blah extends beyond a single page of memory so that the
0N/A * ability to index into a file of multiple pages is tested.
0N/A */
0N/A private static void initTestFile(File blah) throws Exception {
0N/A FileOutputStream fos = new FileOutputStream(blah);
0N/A BufferedWriter awriter
0N/A = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
0N/A
0N/A for(int i=0; i<4000; i++) {
0N/A String number = new Integer(i).toString();
0N/A for (int h=0; h<4-number.length(); h++)
0N/A awriter.write("0");
0N/A awriter.write(""+i);
0N/A awriter.newLine();
0N/A }
0N/A awriter.flush();
0N/A awriter.close();
0N/A }
0N/A}