1 /*
   2  * Copyright (c) 2000, 2011, 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 4429043 6526860
  26  * @summary Test position method of FileChannel
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.file.*;
  33 import static java.nio.file.StandardOpenOption.*;
  34 import java.nio.charset.Charset;
  35 import java.util.Random;
  36 
  37 
  38 /**
  39  * Testing FileChannel's position method.
  40  */
  41 
  42 public class Position {
  43 
  44     private static final Charset ISO8859_1 = Charset.forName("8859_1");
  45 
  46     private static final Random generator = new Random();
  47 
  48     public static void main(String[] args) throws Exception {
  49         Path blah = Files.createTempFile("blah", null);
  50         blah.toFile().deleteOnExit();
  51         initTestFile(blah);
  52 
  53         for (int i=0; i<10; i++) {
  54             try (FileChannel fc = (generator.nextBoolean()) ?
  55                     FileChannel.open(blah, READ) :
  56                     new FileInputStream(blah.toFile()).getChannel()) {
  57                 for (int j=0; j<100; j++) {
  58                     long newPos = generator.nextInt(1000);
  59                     fc.position(newPos);
  60                     if (fc.position() != newPos)
  61                         throw new RuntimeException("Position failed");
  62                 }
  63             }
  64         }
  65 
  66         for (int i=0; i<10; i++) {
  67             try (FileChannel fc = (generator.nextBoolean()) ?
  68                      FileChannel.open(blah, APPEND) :
  69                      new FileOutputStream(blah.toFile(), true).getChannel()) {
  70                 for (int j=0; j<10; j++) {
  71                     if (fc.position() != fc.size())
  72                         throw new RuntimeException("Position expected to be size");
  73                     byte[] buf = new byte[generator.nextInt(100)];
  74                     fc.write(ByteBuffer.wrap(buf));
  75                 }
  76             }
  77         }
  78 
  79         Files.delete(blah);
  80     }
  81 
  82     /**
  83      * Creates file blah:
  84      * 0000
  85      * 0001
  86      * 0002
  87      * 0003
  88      * .
  89      * .
  90      * .
  91      * 3999
  92      *
  93      */
  94     private static void initTestFile(Path blah) throws IOException {
  95         try (BufferedWriter awriter = Files.newBufferedWriter(blah, ISO8859_1)) {
  96             for(int i=0; i<4000; i++) {
  97                 String number = new Integer(i).toString();
  98                 for (int h=0; h<4-number.length(); h++)
  99                     awriter.write("0");
 100                 awriter.write(""+i);
 101                 awriter.newLine();
 102             }
 103         }
 104     }
 105 }