1 /*
   2  * Copyright (c) 2000, 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 /* @test
  25  * @bug 4862382 4862408
  26  * @summary Test positional read method of FileChannel
  27  * @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.ByteBuffer;
  32 import java.nio.CharBuffer;
  33 import java.nio.channels.*;
  34 import java.nio.channels.FileChannel;
  35 import java.util.Random;
  36 
  37 
  38 /**
  39  * Testing FileChannel's positional read method.
  40  */
  41 
  42 public class Pread {
  43 
  44     private static PrintStream err = System.err;
  45 
  46     private static Random generator = new Random();
  47 
  48     private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;
  49 
  50     public static void main(String[] args) throws Exception {
  51         genericTest();
  52         testNegativePosition(); // This test for bug 4862382
  53         testUnreadableChannel();// This test for bug 4862408
  54     }
  55 
  56     // This test for bug 4862382
  57     private static void testNegativePosition() throws Exception {
  58         File blah = File.createTempFile("blah1", null);
  59         blah.deleteOnExit();
  60         FileOutputStream fos = new FileOutputStream(blah);
  61         fos.write(new byte[128]);
  62         fos.close();
  63         FileChannel fc = (new FileInputStream(blah)).getChannel();
  64         try {
  65             fc.read(ByteBuffer.allocate(256), -1L);
  66             throw new RuntimeException("Expected exception not thrown");
  67         } catch(IllegalArgumentException e) {
  68             // Correct result
  69         } finally {
  70             fc.close();
  71             blah.delete();
  72         }
  73     }
  74 
  75     // This test for bug 4862408
  76     private static void testUnreadableChannel() throws Exception {
  77         File blah = File.createTempFile("blah2", null);
  78         blah.deleteOnExit();
  79         FileOutputStream fos = new FileOutputStream(blah);
  80         try {
  81             fos.write(new byte[128]);
  82             FileChannel fc = fos.getChannel();
  83             try {
  84                 fc.read(ByteBuffer.allocate(256),1);
  85                 throw new RuntimeException("Expected exception not thrown");
  86             } catch(NonReadableChannelException e) {
  87                 // Correct result
  88             }
  89         } finally {
  90             fos.close();
  91             blah.delete();
  92         }
  93     }
  94 
  95     private static void genericTest() throws Exception {
  96         StringBuffer sb = new StringBuffer();
  97         sb.setLength(4);
  98 
  99         File blah = File.createTempFile("blah3", null);
 100         blah.deleteOnExit();
 101         initTestFile(blah);
 102 
 103         FileInputStream fis = new FileInputStream(blah);
 104         FileChannel c = fis.getChannel();
 105 
 106         for (int x=0; x<100; x++) {
 107             long offset = generator.nextInt(1000);
 108             long expectedResult = offset / CHARS_PER_LINE;
 109             offset = expectedResult * CHARS_PER_LINE;
 110             ByteBuffer bleck = ByteBuffer.allocateDirect(4);
 111 
 112             long originalPosition = c.position();
 113 
 114             int totalRead = 0;
 115             while (totalRead < 4) {
 116                 int read = c.read(bleck, offset);
 117                 if (read < 0)
 118                     throw new Exception("Read failed");
 119                 totalRead += read;
 120             }
 121 
 122             long newPosition = c.position();
 123 
 124             for (int i=0; i<4; i++) {
 125                 byte aByte = bleck.get(i);
 126                 sb.setCharAt(i, (char)aByte);
 127             }
 128             int result = Integer.parseInt(sb.toString());
 129             if (result != expectedResult) {
 130                 err.println("I expected "+ expectedResult);
 131                 err.println("I got "+ result);
 132                 throw new Exception("Read test failed");
 133             }
 134 
 135             // Ensure that file pointer position has not changed
 136             if (originalPosition != newPosition)
 137                 throw new Exception("File position modified");
 138         }
 139 
 140         c.close();
 141         fis.close();
 142         blah.delete();
 143     }
 144 
 145     /**
 146      * Creates file blah:
 147      * 0000
 148      * 0001
 149      * 0002
 150      * 0003
 151      * .
 152      * .
 153      * .
 154      * 3999
 155      *
 156      * Blah extends beyond a single page of memory so that the
 157      * ability to index into a file of multiple pages is tested.
 158      */
 159     private static void initTestFile(File blah) throws Exception {
 160         FileOutputStream fos = new FileOutputStream(blah);
 161         BufferedWriter awriter
 162             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 163 
 164         for(int i=0; i<4000; i++) {
 165             String number = new Integer(i).toString();
 166             for (int h=0; h<4-number.length(); h++)
 167                 awriter.write("0");
 168             awriter.write(""+i);
 169             awriter.newLine();
 170         }
 171        awriter.flush();
 172        awriter.close();
 173     }
 174 }