Path: blob/master/test/jdk/java/nio/channels/FileChannel/Transfer.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 4434723 4482726 4559072 4795550 5081340 5103988 698454525* @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed)26* @library ..27* @library /test/lib28* @build jdk.test.lib.RandomFactory29* @run testng/timeout=300 Transfer30* @key randomness31*/3233import java.io.BufferedReader;34import java.io.File;35import java.io.FileInputStream;36import java.io.FileOutputStream;37import java.io.InputStreamReader;38import java.io.IOException;39import java.io.RandomAccessFile;40import java.io.Reader;41import java.net.InetAddress;42import java.net.InetSocketAddress;43import java.nio.ByteBuffer;44import java.nio.channels.FileChannel;45import java.nio.channels.NonReadableChannelException;46import java.nio.channels.Pipe;47import java.nio.channels.ServerSocketChannel;48import java.nio.channels.SocketChannel;49import java.nio.channels.spi.SelectorProvider;50import java.util.Random;51import java.util.concurrent.TimeUnit;5253import jdk.test.lib.RandomFactory;5455import org.testng.annotations.Test;5657public class Transfer {5859private static Random generator = RandomFactory.getRandom();6061@Test62public void testFileChannel() throws Exception {63File source = File.createTempFile("source", null);64source.deleteOnExit();65File sink = File.createTempFile("sink", null);66sink.deleteOnExit();6768FileOutputStream fos = new FileOutputStream(source);69FileChannel sourceChannel = fos.getChannel();70sourceChannel.write(ByteBuffer.wrap(71"Use the source, Luke!".getBytes()));72sourceChannel.close();7374FileInputStream fis = new FileInputStream(source);75sourceChannel = fis.getChannel();7677RandomAccessFile raf = new RandomAccessFile(sink, "rw");78FileChannel sinkChannel = raf.getChannel();79long oldSinkPosition = sinkChannel.position();80long oldSourcePosition = sourceChannel.position();8182long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);83if (bytesWritten != 10)84throw new RuntimeException("Transfer failed");8586if (sourceChannel.position() == oldSourcePosition)87throw new RuntimeException("Source position didn't change");8889if (sinkChannel.position() != oldSinkPosition)90throw new RuntimeException("Sink position changed");9192if (sinkChannel.size() != 10)93throw new RuntimeException("Unexpected sink size");9495bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);9697if (bytesWritten > 0)98throw new RuntimeException("Wrote past file size");99100sourceChannel.close();101sinkChannel.close();102103source.delete();104sink.delete();105}106107@Test108public void testReadableByteChannel() throws Exception {109int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };110111for (int size : testSizes) {112SelectorProvider sp = SelectorProvider.provider();113Pipe p = sp.openPipe();114Pipe.SinkChannel sink = p.sink();115Pipe.SourceChannel source = p.source();116sink.configureBlocking(false);117118ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);119byte[] someBytes = new byte[size + 10];120generator.nextBytes(someBytes);121outgoingdata.put(someBytes);122outgoingdata.flip();123124int totalWritten = 0;125while (totalWritten < size + 10) {126int written = sink.write(outgoingdata);127if (written < 0)128throw new Exception("Write failed");129totalWritten += written;130}131132File f = File.createTempFile("blah"+size, null);133f.deleteOnExit();134RandomAccessFile raf = new RandomAccessFile(f, "rw");135FileChannel fc = raf.getChannel();136long oldPosition = fc.position();137138long bytesWritten = fc.transferFrom(source, 0, size);139fc.force(true);140if (bytesWritten != size)141throw new RuntimeException("Transfer failed");142143if (fc.position() != oldPosition)144throw new RuntimeException("Position changed");145146if (fc.size() != size)147throw new RuntimeException("Unexpected sink size "+ fc.size());148149fc.close();150sink.close();151source.close();152153f.delete();154}155}156157@Test158public void xferTest02() throws Exception { // for bug 4482726159byte[] srcData = new byte[5000];160for (int i=0; i<5000; i++)161srcData[i] = (byte)generator.nextInt();162163// get filechannel for the source file.164File source = File.createTempFile("source", null);165source.deleteOnExit();166RandomAccessFile raf1 = new RandomAccessFile(source, "rw");167FileChannel fc1 = raf1.getChannel();168169// write out data to the file channel170long bytesWritten = 0;171while (bytesWritten < 5000) {172bytesWritten = fc1.write(ByteBuffer.wrap(srcData));173}174175// get filechannel for the dst file.176File dest = File.createTempFile("dest", null);177dest.deleteOnExit();178RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");179FileChannel fc2 = raf2.getChannel();180181int bytesToWrite = 3000;182int startPosition = 1000;183184bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);185186fc1.close();187fc2.close();188raf1.close();189raf2.close();190191source.delete();192dest.delete();193}194195@Test196public void xferTest03() throws Exception { // for bug 4559072197byte[] srcData = new byte[] {1,2,3,4} ;198199// get filechannel for the source file.200File source = File.createTempFile("source", null);201source.deleteOnExit();202RandomAccessFile raf1 = new RandomAccessFile(source, "rw");203FileChannel fc1 = raf1.getChannel();204fc1.truncate(0);205206// write out data to the file channel207int bytesWritten = 0;208while (bytesWritten < 4) {209bytesWritten = fc1.write(ByteBuffer.wrap(srcData));210}211212// get filechannel for the dst file.213File dest = File.createTempFile("dest", null);214dest.deleteOnExit();215RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");216FileChannel fc2 = raf2.getChannel();217fc2.truncate(0);218219fc1.transferTo(0, srcData.length + 1, fc2);220221if (fc2.size() > 4)222throw new Exception("xferTest03 failed");223224fc1.close();225fc2.close();226raf1.close();227raf2.close();228229source.delete();230dest.delete();231}232233// xferTest04() and xferTest05() moved to Transfer4GBFile.java234235static void checkFileData(File file, String expected) throws Exception {236FileInputStream fis = new FileInputStream(file);237Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));238StringBuilder sb = new StringBuilder();239int c;240while ((c = r.read()) != -1)241sb.append((char)c);242String contents = sb.toString();243if (! contents.equals(expected))244throw new Exception("expected: " + expected245+ ", got: " + contents);246r.close();247}248249// Test transferFrom asking for more bytes than remain in source250@Test251public void xferTest06() throws Exception { // for bug 5081340252String data = "Use the source, Luke!";253254File source = File.createTempFile("source", null);255source.deleteOnExit();256File sink = File.createTempFile("sink", null);257sink.deleteOnExit();258259FileOutputStream fos = new FileOutputStream(source);260fos.write(data.getBytes("ASCII"));261fos.close();262263FileChannel sourceChannel =264new RandomAccessFile(source, "rw").getChannel();265sourceChannel.position(7);266long remaining = sourceChannel.size() - sourceChannel.position();267FileChannel sinkChannel =268new RandomAccessFile(sink, "rw").getChannel();269long n = sinkChannel.transferFrom(sourceChannel, 0L,270sourceChannel.size()); // overflow271if (n != remaining)272throw new Exception("n == " + n + ", remaining == " + remaining);273274sinkChannel.close();275sourceChannel.close();276277checkFileData(source, data);278checkFileData(sink, data.substring(7,data.length()));279280source.delete();281}282283// Test transferTo to non-blocking socket channel284@Test285public void xferTest07() throws Exception { // for bug 5103988286File source = File.createTempFile("source", null);287source.deleteOnExit();288289FileChannel sourceChannel = new RandomAccessFile(source, "rw")290.getChannel();291sourceChannel.position(32000L)292.write(ByteBuffer.wrap("The End".getBytes()));293294// The sink is a non-blocking socket channel295ServerSocketChannel ssc = ServerSocketChannel.open();296ssc.socket().bind(new InetSocketAddress(0));297InetSocketAddress sa = new InetSocketAddress(298InetAddress.getLocalHost(), ssc.socket().getLocalPort());299SocketChannel sink = SocketChannel.open(sa);300sink.configureBlocking(false);301SocketChannel other = ssc.accept();302303long size = sourceChannel.size();304305// keep sending until congested306long n;307do {308n = sourceChannel.transferTo(0, size, sink);309} while (n > 0);310311sourceChannel.close();312sink.close();313other.close();314ssc.close();315source.delete();316}317318// xferTest08() moved to TransferTo6GBFile.java319320// Test that transferFrom with FileChannel source that is not readable321// throws NonReadableChannelException322@Test323public void xferTest09() throws Exception { // for bug 6984545324File source = File.createTempFile("source", null);325source.deleteOnExit();326327File target = File.createTempFile("target", null);328target.deleteOnExit();329330FileChannel fc1 = new FileOutputStream(source).getChannel();331FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();332try {333fc2.transferFrom(fc1, 0L, 0);334throw new RuntimeException("NonReadableChannelException expected");335} catch (NonReadableChannelException expected) {336} finally {337fc1.close();338fc2.close();339}340}341}342343344