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 6191269 6709457
  26  * @summary Test truncate method of FileChannel
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.FileChannel;
  32 import static java.nio.file.StandardOpenOption.*;
  33 import java.util.Random;
  34 
  35 
  36 /**
  37  * Testing FileChannel's truncate method.
  38  */
  39 
  40 public class Truncate {
  41     private static final Random generator = new Random();
  42 
  43     public static void main(String[] args) throws Exception {
  44         File blah = File.createTempFile("blah", null);
  45         blah.deleteOnExit();
  46         try {
  47             basicTest(blah);
  48             appendTest(blah);
  49         } finally {
  50             blah.delete();
  51         }
  52     }
  53 
  54     /**
  55      * Basic test of asserts in truncate's specification.
  56      */
  57     static void basicTest(File blah) throws Exception {
  58         for(int i=0; i<100; i++) {
  59             long testSize = generator.nextInt(1000) + 10;
  60             initTestFile(blah, testSize);
  61 
  62             try (FileChannel fc = (i < 50) ?
  63                  new RandomAccessFile(blah, "rw").getChannel() :
  64                  FileChannel.open(blah.toPath(), READ, WRITE))
  65                 {
  66                     if (fc.size() != testSize)
  67                         throw new RuntimeException("Size failed");
  68 
  69                     long position = generator.nextInt((int)testSize);
  70                     fc.position(position);
  71 
  72                     long newSize = generator.nextInt((int)testSize);
  73                     fc.truncate(newSize);
  74 
  75                     if (fc.size() != newSize)
  76                         throw new RuntimeException("Truncate failed");
  77 
  78                     if (position > newSize) {
  79                         if (fc.position() != newSize)
  80                             throw new RuntimeException("Position greater than size");
  81                     } else {
  82                         if (fc.position() != position)
  83                             throw new RuntimeException("Truncate changed position");
  84                     };
  85                 }
  86         }
  87     }
  88 
  89     /**
  90      * Test behavior of truncate method when file is opened for append
  91      */
  92     static void appendTest(File blah) throws Exception {
  93         for (int i=0; i<10; i++) {
  94             long testSize = generator.nextInt(1000) + 10;
  95             initTestFile(blah, testSize);
  96             try (FileChannel fc = (i < 5) ?
  97                  new FileOutputStream(blah, true).getChannel() :
  98                  FileChannel.open(blah.toPath(), APPEND))
  99                 {
 100                     // truncate file
 101                     long newSize = generator.nextInt((int)testSize);
 102                     fc.truncate(newSize);
 103                     if (fc.size() != newSize)
 104                         throw new RuntimeException("Truncate failed");
 105 
 106                     // write one byte
 107                     ByteBuffer buf = ByteBuffer.allocate(1);
 108                     buf.put((byte)'x');
 109                     buf.flip();
 110                     fc.write(buf);
 111                     if (fc.size() != (newSize+1))
 112                         throw new RuntimeException("Unexpected size");
 113                 }
 114         }
 115     }
 116 
 117     /**
 118      * Creates file blah of specified size in bytes.
 119      *
 120      */
 121     private static void initTestFile(File blah, long size) throws Exception {
 122         if (blah.exists())
 123             blah.delete();
 124         FileOutputStream fos = new FileOutputStream(blah);
 125         BufferedWriter awriter
 126             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 127 
 128         for(int i=0; i<size; i++) {
 129             awriter.write("e");
 130         }
 131         awriter.flush();
 132         awriter.close();
 133     }
 134 }