Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/Transfer.java
38828 views
/*1* Copyright (c) 2001, 2012, 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 4638365 4795550 5081340 5103988 625314525* 698454526* @summary Test FileChannel.transferFrom and transferTo27* @library ..28* @key randomness29*/3031import java.io.*;32import java.net.*;33import java.nio.*;34import java.nio.channels.*;35import java.nio.channels.spi.SelectorProvider;36import java.nio.file.StandardOpenOption;37import java.nio.file.FileAlreadyExistsException;38import java.util.Random;394041public class Transfer {4243private static Random generator = new Random();4445private static int[] testSizes = {460, 10, 1023, 1024, 1025, 2047, 2048, 2049 };4748public static void main(String[] args) throws Exception {49testFileChannel();50for (int i=0; i<testSizes.length; i++)51testReadableByteChannel(testSizes[i]);52xferTest02(); // for bug 448272653xferTest03(); // for bug 455907254xferTest04(); // for bug 463836555xferTest05(); // for bug 463836556xferTest06(); // for bug 508134057xferTest07(); // for bug 510398858xferTest08(); // for bug 625314559xferTest09(); // for bug 698454560}6162private static 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}106107private static void testReadableByteChannel(int size) throws Exception {108SelectorProvider sp = SelectorProvider.provider();109Pipe p = sp.openPipe();110Pipe.SinkChannel sink = p.sink();111Pipe.SourceChannel source = p.source();112sink.configureBlocking(false);113114ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);115byte[] someBytes = new byte[size + 10];116generator.nextBytes(someBytes);117outgoingdata.put(someBytes);118outgoingdata.flip();119120int totalWritten = 0;121while (totalWritten < size + 10) {122int written = sink.write(outgoingdata);123if (written < 0)124throw new Exception("Write failed");125totalWritten += written;126}127128File f = File.createTempFile("blah"+size, null);129f.deleteOnExit();130RandomAccessFile raf = new RandomAccessFile(f, "rw");131FileChannel fc = raf.getChannel();132long oldPosition = fc.position();133134long bytesWritten = fc.transferFrom(source, 0, size);135fc.force(true);136if (bytesWritten != size)137throw new RuntimeException("Transfer failed");138139if (fc.position() != oldPosition)140throw new RuntimeException("Position changed");141142if (fc.size() != size)143throw new RuntimeException("Unexpected sink size "+ fc.size());144145fc.close();146sink.close();147source.close();148149f.delete();150}151152public static void xferTest02() throws Exception {153byte[] srcData = new byte[5000];154for (int i=0; i<5000; i++)155srcData[i] = (byte)generator.nextInt();156157// get filechannel for the source file.158File source = File.createTempFile("source", null);159source.deleteOnExit();160RandomAccessFile raf1 = new RandomAccessFile(source, "rw");161FileChannel fc1 = raf1.getChannel();162163// write out data to the file channel164long bytesWritten = 0;165while (bytesWritten < 5000) {166bytesWritten = fc1.write(ByteBuffer.wrap(srcData));167}168169// get filechannel for the dst file.170File dest = File.createTempFile("dest", null);171dest.deleteOnExit();172RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");173FileChannel fc2 = raf2.getChannel();174175int bytesToWrite = 3000;176int startPosition = 1000;177178bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);179180fc1.close();181fc2.close();182raf1.close();183raf2.close();184185source.delete();186dest.delete();187}188189public static void xferTest03() throws Exception {190byte[] srcData = new byte[] {1,2,3,4} ;191192// get filechannel for the source file.193File source = File.createTempFile("source", null);194source.deleteOnExit();195RandomAccessFile raf1 = new RandomAccessFile(source, "rw");196FileChannel fc1 = raf1.getChannel();197fc1.truncate(0);198199// write out data to the file channel200int bytesWritten = 0;201while (bytesWritten < 4) {202bytesWritten = fc1.write(ByteBuffer.wrap(srcData));203}204205// get filechannel for the dst file.206File dest = File.createTempFile("dest", null);207dest.deleteOnExit();208RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");209FileChannel fc2 = raf2.getChannel();210fc2.truncate(0);211212fc1.transferTo(0, srcData.length + 1, fc2);213214if (fc2.size() > 4)215throw new Exception("xferTest03 failed");216217fc1.close();218fc2.close();219raf1.close();220raf2.close();221222source.delete();223dest.delete();224}225226// Test transferTo with large file227public static void xferTest04() throws Exception {228// Windows and Linux can't handle the really large file sizes for a229// truncate or a positional write required by the test for 4563125230String osName = System.getProperty("os.name");231if (!(osName.startsWith("SunOS") || osName.contains("OS X")))232return;233File source = File.createTempFile("blah", null);234source.deleteOnExit();235long testSize = ((long)Integer.MAX_VALUE) * 2;236initTestFile(source, 10);237RandomAccessFile raf = new RandomAccessFile(source, "rw");238FileChannel fc = raf.getChannel();239fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);240fc.close();241raf.close();242243File sink = File.createTempFile("sink", null);244sink.deleteOnExit();245246FileInputStream fis = new FileInputStream(source);247FileChannel sourceChannel = fis.getChannel();248249raf = new RandomAccessFile(sink, "rw");250FileChannel sinkChannel = raf.getChannel();251252long bytesWritten = sourceChannel.transferTo(testSize -40, 10,253sinkChannel);254if (bytesWritten != 10) {255throw new RuntimeException("Transfer test 4 failed " +256bytesWritten);257}258sourceChannel.close();259sinkChannel.close();260261source.delete();262sink.delete();263}264265// Test transferFrom with large file266public static void xferTest05() throws Exception {267// Create a source file & large sink file for the test268File source = File.createTempFile("blech", null);269source.deleteOnExit();270initTestFile(source, 100);271272// Create the sink file as a sparse file if possible273File sink = null;274FileChannel fc = null;275while (fc == null) {276sink = File.createTempFile("sink", null);277// re-create as a sparse file278sink.delete();279try {280fc = FileChannel.open(sink.toPath(),281StandardOpenOption.CREATE_NEW,282StandardOpenOption.WRITE,283StandardOpenOption.SPARSE);284} catch (FileAlreadyExistsException ignore) {285// someone else got it286}287}288sink.deleteOnExit();289290long testSize = ((long)Integer.MAX_VALUE) * 2;291try {292fc.write(ByteBuffer.wrap("Use the source!".getBytes()),293testSize - 40);294} catch (IOException e) {295// Can't set up the test, abort it296System.err.println("xferTest05 was aborted.");297return;298} finally {299fc.close();300}301302// Get new channels for the source and sink and attempt transfer303FileChannel sourceChannel = new FileInputStream(source).getChannel();304try {305FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();306try {307long bytesWritten = sinkChannel.transferFrom(sourceChannel,308testSize - 40, 10);309if (bytesWritten != 10) {310throw new RuntimeException("Transfer test 5 failed " +311bytesWritten);312}313} finally {314sinkChannel.close();315}316} finally {317sourceChannel.close();318}319320source.delete();321sink.delete();322}323324static void checkFileData(File file, String expected) throws Exception {325FileInputStream fis = new FileInputStream(file);326Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));327StringBuilder sb = new StringBuilder();328int c;329while ((c = r.read()) != -1)330sb.append((char)c);331String contents = sb.toString();332if (! contents.equals(expected))333throw new Exception("expected: " + expected334+ ", got: " + contents);335r.close();336}337338// Test transferFrom asking for more bytes than remain in source339public static void xferTest06() throws Exception {340String data = "Use the source, Luke!";341342File source = File.createTempFile("source", null);343source.deleteOnExit();344File sink = File.createTempFile("sink", null);345sink.deleteOnExit();346347FileOutputStream fos = new FileOutputStream(source);348fos.write(data.getBytes("ASCII"));349fos.close();350351FileChannel sourceChannel =352new RandomAccessFile(source, "rw").getChannel();353sourceChannel.position(7);354long remaining = sourceChannel.size() - sourceChannel.position();355FileChannel sinkChannel =356new RandomAccessFile(sink, "rw").getChannel();357long n = sinkChannel.transferFrom(sourceChannel, 0L,358sourceChannel.size()); // overflow359if (n != remaining)360throw new Exception("n == " + n + ", remaining == " + remaining);361362sinkChannel.close();363sourceChannel.close();364365checkFileData(source, data);366checkFileData(sink, data.substring(7,data.length()));367368source.delete();369}370371// Test transferTo to non-blocking socket channel372public static void xferTest07() throws Exception {373File source = File.createTempFile("source", null);374source.deleteOnExit();375376FileChannel sourceChannel = new RandomAccessFile(source, "rw")377.getChannel();378sourceChannel.position(32000L)379.write(ByteBuffer.wrap("The End".getBytes()));380381// The sink is a non-blocking socket channel382ServerSocketChannel ssc = ServerSocketChannel.open();383ssc.socket().bind(new InetSocketAddress(0));384InetSocketAddress sa = new InetSocketAddress(385InetAddress.getLocalHost(), ssc.socket().getLocalPort());386SocketChannel sink = SocketChannel.open(sa);387sink.configureBlocking(false);388SocketChannel other = ssc.accept();389390long size = sourceChannel.size();391392// keep sending until congested393long n;394do {395n = sourceChannel.transferTo(0, size, sink);396} while (n > 0);397398sourceChannel.close();399sink.close();400other.close();401ssc.close();402source.delete();403}404405406// Test transferTo with file positions larger than 2 and 4GB407public static void xferTest08() throws Exception {408// Creating a sparse 6GB file on Windows takes too long409String osName = System.getProperty("os.name");410if (osName.startsWith("Windows"))411return;412413final long G = 1024L * 1024L * 1024L;414415// Create 6GB file416417File file = File.createTempFile("source", null);418file.deleteOnExit();419420RandomAccessFile raf = new RandomAccessFile(file, "rw");421FileChannel fc = raf.getChannel();422423try {424fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);425} catch (IOException x) {426System.err.println("Unable to create test file:" + x);427fc.close();428return;429}430431// Setup looback connection and echo server432433ServerSocketChannel ssc = ServerSocketChannel.open();434ssc.socket().bind(new InetSocketAddress(0));435436InetAddress lh = InetAddress.getLocalHost();437InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());438SocketChannel source = SocketChannel.open(isa);439SocketChannel sink = ssc.accept();440441Thread thr = new Thread(new EchoServer(sink));442thr.start();443444// Test data is array of positions and counts445446long testdata[][] = {447{ 2*G-1, 1 },448{ 2*G-1, 10 }, // across 2GB boundary449{ 2*G, 1 },450{ 2*G, 10 },451{ 2*G+1, 1 },452{ 4*G-1, 1 },453{ 4*G-1, 10 }, // across 4GB boundary454{ 4*G, 1 },455{ 4*G, 10 },456{ 4*G+1, 1 },457{ 5*G-1, 1 },458{ 5*G-1, 10 },459{ 5*G, 1 },460{ 5*G, 10 },461{ 5*G+1, 1 },462{ 6*G, 1 },463};464465ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);466ByteBuffer readbuf = ByteBuffer.allocateDirect(100);467468try {469byte value = 0;470for (int i=0; i<testdata.length; i++) {471long position = testdata[(int)i][0];472long count = testdata[(int)i][1];473474// generate bytes475for (long j=0; j<count; j++) {476sendbuf.put(++value);477}478sendbuf.flip();479480// write to file and transfer to echo server481fc.write(sendbuf, position);482fc.transferTo(position, count, source);483484// read from echo server485long nread = 0;486while (nread < count) {487int n = source.read(readbuf);488if (n < 0)489throw new RuntimeException("Premature EOF!");490nread += n;491}492493// check reply from echo server494readbuf.flip();495sendbuf.flip();496if (!readbuf.equals(sendbuf))497throw new RuntimeException("Echo'ed bytes do not match!");498readbuf.clear();499sendbuf.clear();500}501} finally {502source.close();503ssc.close();504fc.close();505file.delete();506}507}508509// Test that transferFrom with FileChannel source that is not readable510// throws NonReadableChannelException511static void xferTest09() throws Exception {512File source = File.createTempFile("source", null);513source.deleteOnExit();514515File target = File.createTempFile("target", null);516target.deleteOnExit();517518FileChannel fc1 = new FileOutputStream(source).getChannel();519FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();520try {521fc2.transferFrom(fc1, 0L, 0);522throw new RuntimeException("NonReadableChannelException expected");523} catch (NonReadableChannelException expected) {524} finally {525fc1.close();526fc2.close();527}528}529530/**531* Creates file blah of specified size in bytes.532*/533private static void initTestFile(File blah, long size) throws Exception {534if (blah.exists())535blah.delete();536FileOutputStream fos = new FileOutputStream(blah);537BufferedWriter awriter538= new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));539540for(int i=0; i<size; i++) {541awriter.write("e");542}543awriter.flush();544awriter.close();545}546547/**548* Simple in-process server to echo bytes read by a given socket channel549*/550static class EchoServer implements Runnable {551private SocketChannel sc;552553public EchoServer(SocketChannel sc) {554this.sc = sc;555}556557public void run() {558ByteBuffer bb = ByteBuffer.allocateDirect(1024);559try {560for (;;) {561int n = sc.read(bb);562if (n < 0)563break;564565bb.flip();566while (bb.remaining() > 0) {567sc.write(bb);568}569bb.clear();570}571} catch (IOException x) {572x.printStackTrace();573} finally {574try {575sc.close();576} catch (IOException ignore) { }577}578}579}580581}582583584