blob: f2bd4f459319f649b86c38a2dc0624c373215476 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 6191269
26 * @summary Test truncate method of FileChannel
27 */
28
29import java.io.*;
30import java.nio.MappedByteBuffer;
31import java.nio.channels.*;
32import java.nio.channels.FileChannel;
33import java.util.Random;
34
35
36/**
37 * Testing FileChannel's truncate method.
38 */
39
40public class Truncate {
41
42 private static Random generator = new Random();
43
44 private static File blah;
45
46 public static void main(String[] args) throws Exception {
47 blah = File.createTempFile("blah", null);
48 blah.deleteOnExit();
49 for(int i=0; i<100; i++) {
50 long testSize = generator.nextInt(1000) + 10;
51 initTestFile(blah, testSize);
52 RandomAccessFile fis = new RandomAccessFile(blah, "rw");
53 FileChannel c = fis.getChannel();
54 if (c.size() != testSize)
55 throw new RuntimeException("Size failed");
56
57 long position = generator.nextInt((int)testSize);
58 c.position(position);
59
60 long newSize = generator.nextInt((int)testSize);
61 c.truncate(newSize);
62
63 if (c.size() != newSize)
64 throw new RuntimeException("Truncate failed");
65
66 if (position > newSize) {
67 if (c.position() != newSize)
68 throw new RuntimeException("Position greater than size");
69 } else {
70 if (c.position() != position)
71 throw new RuntimeException("Truncate changed position");
72 }
73
74 c.close();
75 fis.close();
76 }
77 blah.delete();
78 }
79
80 /**
81 * Creates file blah of specified size in bytes.
82 *
83 */
84 private static void initTestFile(File blah, long size) throws Exception {
85 if (blah.exists())
86 blah.delete();
87 FileOutputStream fos = new FileOutputStream(blah);
88 BufferedWriter awriter
89 = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
90
91 for(int i=0; i<size; i++) {
92 awriter.write("e");
93 }
94 awriter.flush();
95 awriter.close();
96 }
97}