Path: blob/master/test/jdk/java/nio/channels/FileChannel/Transfer4GBFile.java
41154 views
/*1* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 463836525* @summary Test FileChannel.transferFrom and transferTo for 4GB files26* @run testng/timeout=300 Transfer4GBFile27*/2829import java.io.BufferedWriter;30import java.io.File;31import java.io.FileInputStream;32import java.io.FileOutputStream;33import java.io.IOException;34import java.io.OutputStreamWriter;35import java.io.PrintStream;36import java.io.RandomAccessFile;37import java.nio.ByteBuffer;38import java.nio.channels.FileChannel;39import java.nio.file.StandardOpenOption;40import java.nio.file.FileAlreadyExistsException;41import java.util.concurrent.TimeUnit;4243import org.testng.annotations.Test;4445public class Transfer4GBFile {4647private static PrintStream err = System.err;48private static PrintStream out = System.out;4950// Test transferTo with large file51@Test52public void xferTest04() throws Exception { // for bug 463836553File source = File.createTempFile("blah", null);54source.deleteOnExit();55long testSize = ((long)Integer.MAX_VALUE) * 2;56initTestFile(source, 10);57RandomAccessFile raf = new RandomAccessFile(source, "rw");58FileChannel fc = raf.getChannel();59out.println(" Writing large file...");60long t0 = System.nanoTime();61fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);62long t1 = System.nanoTime();63out.printf(" Wrote large file in %d ns (%d ms) %n",64t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));6566fc.close();67raf.close();6869File sink = File.createTempFile("sink", null);70sink.deleteOnExit();7172FileInputStream fis = new FileInputStream(source);73FileChannel sourceChannel = fis.getChannel();7475raf = new RandomAccessFile(sink, "rw");76FileChannel sinkChannel = raf.getChannel();7778long bytesWritten = sourceChannel.transferTo(testSize -40, 10,79sinkChannel);80if (bytesWritten != 10) {81throw new RuntimeException("Transfer test 4 failed " +82bytesWritten);83}84sourceChannel.close();85sinkChannel.close();8687source.delete();88sink.delete();89}9091// Test transferFrom with large file92@Test93public void xferTest05() throws Exception { // for bug 463836594// Create a source file & large sink file for the test95File source = File.createTempFile("blech", null);96source.deleteOnExit();97initTestFile(source, 100);9899// Create the sink file as a sparse file if possible100File sink = null;101FileChannel fc = null;102while (fc == null) {103sink = File.createTempFile("sink", null);104// re-create as a sparse file105sink.delete();106try {107fc = FileChannel.open(sink.toPath(),108StandardOpenOption.CREATE_NEW,109StandardOpenOption.WRITE,110StandardOpenOption.SPARSE);111} catch (FileAlreadyExistsException ignore) {112// someone else got it113}114}115sink.deleteOnExit();116117long testSize = ((long)Integer.MAX_VALUE) * 2;118try {119out.println(" Writing large file...");120long t0 = System.nanoTime();121fc.write(ByteBuffer.wrap("Use the source!".getBytes()),122testSize - 40);123long t1 = System.nanoTime();124out.printf(" Wrote large file in %d ns (%d ms) %n",125t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));126} catch (IOException e) {127// Can't set up the test, abort it128err.println("xferTest05 was aborted.");129return;130} finally {131fc.close();132}133134// Get new channels for the source and sink and attempt transfer135FileChannel sourceChannel = new FileInputStream(source).getChannel();136try {137FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();138try {139long bytesWritten = sinkChannel.transferFrom(sourceChannel,140testSize - 40, 10);141if (bytesWritten != 10) {142throw new RuntimeException("Transfer test 5 failed " +143bytesWritten);144}145} finally {146sinkChannel.close();147}148} finally {149sourceChannel.close();150}151152source.delete();153sink.delete();154}155156/**157* Creates file blah of specified size in bytes.158*/159private static void initTestFile(File blah, long size) throws Exception {160if (blah.exists())161blah.delete();162FileOutputStream fos = new FileOutputStream(blah);163BufferedWriter awriter164= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));165166for(int i=0; i<size; i++) {167awriter.write("e");168}169awriter.flush();170awriter.close();171}172}173174175