1 /*
   2  * Copyright (c) 2001, 2017, 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 4434723 4482726 4559072 4638365 4795550 5081340 5103988 6253145
  26  *   6984545
  27  * @key intermittent
  28  * @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed)
  29  * @library ..
  30  * @library /lib/testlibrary/
  31  * @build jdk.testlibrary.*
  32  * @run testng Transfer
  33  * @key randomness
  34  */
  35 
  36 import java.io.BufferedReader;
  37 import java.io.BufferedWriter;
  38 import java.io.File;
  39 import java.io.FileInputStream;
  40 import java.io.FileOutputStream;
  41 import java.io.InputStreamReader;
  42 import java.io.IOException;
  43 import java.io.OutputStreamWriter;
  44 import java.io.PrintStream;
  45 import java.io.RandomAccessFile;
  46 import java.io.Reader;
  47 import java.lang.reflect.InvocationTargetException;
  48 import java.lang.reflect.Method;
  49 import java.net.InetAddress;
  50 import java.net.InetSocketAddress;
  51 import java.nio.ByteBuffer;
  52 import java.nio.channels.FileChannel;
  53 import java.nio.channels.NonReadableChannelException;
  54 import java.nio.channels.Pipe;
  55 import java.nio.channels.ServerSocketChannel;
  56 import java.nio.channels.SocketChannel;
  57 import java.nio.channels.spi.SelectorProvider;
  58 import java.nio.file.StandardOpenOption;
  59 import java.nio.file.FileAlreadyExistsException;
  60 import java.util.Random;
  61 import java.util.concurrent.TimeUnit;
  62 
  63 import jdk.testlibrary.RandomFactory;
  64 
  65 import org.testng.annotations.Test;
  66 
  67 public class Transfer {
  68 
  69     private static Random generator = RandomFactory.getRandom();
  70     private static PrintStream err = System.err;
  71     private static PrintStream out = System.out;
  72 
  73     @Test
  74     public void testFileChannel() throws Exception {
  75         File source = File.createTempFile("source", null);
  76         source.deleteOnExit();
  77         File sink = File.createTempFile("sink", null);
  78         sink.deleteOnExit();
  79 
  80         FileOutputStream fos = new FileOutputStream(source);
  81         FileChannel sourceChannel = fos.getChannel();
  82         sourceChannel.write(ByteBuffer.wrap(
  83             "Use the source, Luke!".getBytes()));
  84         sourceChannel.close();
  85 
  86         FileInputStream fis = new FileInputStream(source);
  87         sourceChannel = fis.getChannel();
  88 
  89         RandomAccessFile raf = new RandomAccessFile(sink, "rw");
  90         FileChannel sinkChannel = raf.getChannel();
  91         long oldSinkPosition = sinkChannel.position();
  92         long oldSourcePosition = sourceChannel.position();
  93 
  94         long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
  95         if (bytesWritten != 10)
  96             throw new RuntimeException("Transfer failed");
  97 
  98         if (sourceChannel.position() == oldSourcePosition)
  99             throw new RuntimeException("Source position didn't change");
 100 
 101         if (sinkChannel.position() != oldSinkPosition)
 102             throw new RuntimeException("Sink position changed");
 103 
 104         if (sinkChannel.size() != 10)
 105             throw new RuntimeException("Unexpected sink size");
 106 
 107         bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);
 108 
 109         if (bytesWritten > 0)
 110             throw new RuntimeException("Wrote past file size");
 111 
 112         sourceChannel.close();
 113         sinkChannel.close();
 114 
 115         source.delete();
 116         sink.delete();
 117     }
 118 
 119     @Test
 120     public void testReadableByteChannel() throws Exception {
 121         int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
 122 
 123         for (int size : testSizes) {
 124             SelectorProvider sp = SelectorProvider.provider();
 125             Pipe p = sp.openPipe();
 126             Pipe.SinkChannel sink = p.sink();
 127             Pipe.SourceChannel source = p.source();
 128             sink.configureBlocking(false);
 129 
 130             ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
 131             byte[] someBytes = new byte[size + 10];
 132             generator.nextBytes(someBytes);
 133             outgoingdata.put(someBytes);
 134             outgoingdata.flip();
 135 
 136             int totalWritten = 0;
 137             while (totalWritten < size + 10) {
 138                 int written = sink.write(outgoingdata);
 139                 if (written < 0)
 140                     throw new Exception("Write failed");
 141                 totalWritten += written;
 142             }
 143 
 144             File f = File.createTempFile("blah"+size, null);
 145             f.deleteOnExit();
 146             RandomAccessFile raf = new RandomAccessFile(f, "rw");
 147             FileChannel fc = raf.getChannel();
 148             long oldPosition = fc.position();
 149 
 150             long bytesWritten = fc.transferFrom(source, 0, size);
 151             fc.force(true);
 152             if (bytesWritten != size)
 153                 throw new RuntimeException("Transfer failed");
 154 
 155             if (fc.position() != oldPosition)
 156                 throw new RuntimeException("Position changed");
 157 
 158             if (fc.size() != size)
 159                 throw new RuntimeException("Unexpected sink size "+ fc.size());
 160 
 161             fc.close();
 162             sink.close();
 163             source.close();
 164 
 165             f.delete();
 166         }
 167     }
 168 
 169     @Test
 170     public void xferTest02() throws Exception { // for bug 4482726
 171         byte[] srcData = new byte[5000];
 172         for (int i=0; i<5000; i++)
 173             srcData[i] = (byte)generator.nextInt();
 174 
 175         // get filechannel for the source file.
 176         File source = File.createTempFile("source", null);
 177         source.deleteOnExit();
 178         RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
 179         FileChannel fc1 = raf1.getChannel();
 180 
 181         // write out data to the file channel
 182         long bytesWritten = 0;
 183         while (bytesWritten < 5000) {
 184             bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
 185         }
 186 
 187         // get filechannel for the dst file.
 188         File dest = File.createTempFile("dest", null);
 189         dest.deleteOnExit();
 190         RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
 191         FileChannel fc2 = raf2.getChannel();
 192 
 193         int bytesToWrite = 3000;
 194         int startPosition = 1000;
 195 
 196         bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);
 197 
 198         fc1.close();
 199         fc2.close();
 200         raf1.close();
 201         raf2.close();
 202 
 203         source.delete();
 204         dest.delete();
 205     }
 206 
 207     @Test
 208     public void xferTest03() throws Exception { // for bug 4559072
 209         byte[] srcData = new byte[] {1,2,3,4} ;
 210 
 211         // get filechannel for the source file.
 212         File source = File.createTempFile("source", null);
 213         source.deleteOnExit();
 214         RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
 215         FileChannel fc1 = raf1.getChannel();
 216         fc1.truncate(0);
 217 
 218         // write out data to the file channel
 219         int bytesWritten = 0;
 220         while (bytesWritten < 4) {
 221             bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
 222         }
 223 
 224         // get filechannel for the dst file.
 225         File dest = File.createTempFile("dest", null);
 226         dest.deleteOnExit();
 227         RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
 228         FileChannel fc2 = raf2.getChannel();
 229         fc2.truncate(0);
 230 
 231         fc1.transferTo(0, srcData.length + 1, fc2);
 232 
 233         if (fc2.size() > 4)
 234             throw new Exception("xferTest03 failed");
 235 
 236         fc1.close();
 237         fc2.close();
 238         raf1.close();
 239         raf2.close();
 240 
 241         source.delete();
 242         dest.delete();
 243     }
 244 
 245     // Test transferTo with large file
 246     @Test
 247     public void xferTest04() throws Exception { // for bug 4638365
 248         // Windows and Linux can't handle the really large file sizes for a
 249         // truncate or a positional write required by the test for 4563125
 250         String osName = System.getProperty("os.name");
 251         if (!(osName.startsWith("SunOS") || osName.contains("OS X")))
 252             return;
 253         File source = File.createTempFile("blah", null);
 254         source.deleteOnExit();
 255         long testSize = ((long)Integer.MAX_VALUE) * 2;
 256         initTestFile(source, 10);
 257         RandomAccessFile raf = new RandomAccessFile(source, "rw");
 258         FileChannel fc = raf.getChannel();
 259         fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
 260         fc.close();
 261         raf.close();
 262 
 263         File sink = File.createTempFile("sink", null);
 264         sink.deleteOnExit();
 265 
 266         FileInputStream fis = new FileInputStream(source);
 267         FileChannel sourceChannel = fis.getChannel();
 268 
 269         raf = new RandomAccessFile(sink, "rw");
 270         FileChannel sinkChannel = raf.getChannel();
 271 
 272         long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
 273                                                      sinkChannel);
 274         if (bytesWritten != 10) {
 275             throw new RuntimeException("Transfer test 4 failed " +
 276                                        bytesWritten);
 277         }
 278         sourceChannel.close();
 279         sinkChannel.close();
 280 
 281         source.delete();
 282         sink.delete();
 283     }
 284 
 285     // Test transferFrom with large file
 286     @Test
 287     public void xferTest05() throws Exception { // for bug 4638365
 288         // Create a source file & large sink file for the test
 289         File source = File.createTempFile("blech", null);
 290         source.deleteOnExit();
 291         initTestFile(source, 100);
 292 
 293         // Create the sink file as a sparse file if possible
 294         File sink = null;
 295         FileChannel fc = null;
 296         while (fc == null) {
 297             sink = File.createTempFile("sink", null);
 298             // re-create as a sparse file
 299             sink.delete();
 300             try {
 301                 fc = FileChannel.open(sink.toPath(),
 302                                       StandardOpenOption.CREATE_NEW,
 303                                       StandardOpenOption.WRITE,
 304                                       StandardOpenOption.SPARSE);
 305             } catch (FileAlreadyExistsException ignore) {
 306                 // someone else got it
 307             }
 308         }
 309         sink.deleteOnExit();
 310 
 311         long testSize = ((long)Integer.MAX_VALUE) * 2;
 312         try {
 313             fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
 314                      testSize - 40);
 315         } catch (IOException e) {
 316             // Can't set up the test, abort it
 317             err.println("xferTest05 was aborted.");
 318             return;
 319         } finally {
 320             fc.close();
 321         }
 322 
 323         // Get new channels for the source and sink and attempt transfer
 324         FileChannel sourceChannel = new FileInputStream(source).getChannel();
 325         try {
 326             FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
 327             try {
 328                 long bytesWritten = sinkChannel.transferFrom(sourceChannel,
 329                                                              testSize - 40, 10);
 330                 if (bytesWritten != 10) {
 331                     throw new RuntimeException("Transfer test 5 failed " +
 332                                                bytesWritten);
 333                 }
 334             } finally {
 335                 sinkChannel.close();
 336             }
 337         } finally {
 338             sourceChannel.close();
 339         }
 340 
 341         source.delete();
 342         sink.delete();
 343     }
 344 
 345     static void checkFileData(File file, String expected) throws Exception {
 346         FileInputStream fis = new FileInputStream(file);
 347         Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
 348         StringBuilder sb = new StringBuilder();
 349         int c;
 350         while ((c = r.read()) != -1)
 351             sb.append((char)c);
 352         String contents = sb.toString();
 353         if (! contents.equals(expected))
 354             throw new Exception("expected: " + expected
 355                                 + ", got: " + contents);
 356         r.close();
 357     }
 358 
 359     // Test transferFrom asking for more bytes than remain in source
 360     @Test
 361     public void xferTest06() throws Exception { // for bug 5081340
 362         String data = "Use the source, Luke!";
 363 
 364         File source = File.createTempFile("source", null);
 365         source.deleteOnExit();
 366         File sink = File.createTempFile("sink", null);
 367         sink.deleteOnExit();
 368 
 369         FileOutputStream fos = new FileOutputStream(source);
 370         fos.write(data.getBytes("ASCII"));
 371         fos.close();
 372 
 373         FileChannel sourceChannel =
 374             new RandomAccessFile(source, "rw").getChannel();
 375         sourceChannel.position(7);
 376         long remaining = sourceChannel.size() - sourceChannel.position();
 377         FileChannel sinkChannel =
 378             new RandomAccessFile(sink, "rw").getChannel();
 379         long n = sinkChannel.transferFrom(sourceChannel, 0L,
 380                                           sourceChannel.size()); // overflow
 381         if (n != remaining)
 382             throw new Exception("n == " + n + ", remaining == " + remaining);
 383 
 384         sinkChannel.close();
 385         sourceChannel.close();
 386 
 387         checkFileData(source, data);
 388         checkFileData(sink, data.substring(7,data.length()));
 389 
 390         source.delete();
 391     }
 392 
 393     // Test transferTo to non-blocking socket channel
 394     @Test
 395     public void xferTest07() throws Exception { // for bug 5103988
 396         File source = File.createTempFile("source", null);
 397         source.deleteOnExit();
 398 
 399         FileChannel sourceChannel = new RandomAccessFile(source, "rw")
 400             .getChannel();
 401         sourceChannel.position(32000L)
 402             .write(ByteBuffer.wrap("The End".getBytes()));
 403 
 404         // The sink is a non-blocking socket channel
 405         ServerSocketChannel ssc = ServerSocketChannel.open();
 406         ssc.socket().bind(new InetSocketAddress(0));
 407         InetSocketAddress sa = new InetSocketAddress(
 408             InetAddress.getLocalHost(), ssc.socket().getLocalPort());
 409         SocketChannel sink = SocketChannel.open(sa);
 410         sink.configureBlocking(false);
 411         SocketChannel other = ssc.accept();
 412 
 413         long size = sourceChannel.size();
 414 
 415         // keep sending until congested
 416         long n;
 417         do {
 418             n = sourceChannel.transferTo(0, size, sink);
 419         } while (n > 0);
 420 
 421         sourceChannel.close();
 422         sink.close();
 423         other.close();
 424         ssc.close();
 425         source.delete();
 426     }
 427 
 428 
 429     // Test transferTo with file positions larger than 2 and 4GB
 430     @Test
 431     public void xferTest08() throws Exception { // for bug 6253145
 432         // Creating a sparse 6GB file on Windows takes too long
 433         String osName = System.getProperty("os.name");
 434         if (osName.startsWith("Windows"))
 435             return;
 436 
 437         final long G = 1024L * 1024L * 1024L;
 438 
 439         // Create 6GB file
 440 
 441         File file = File.createTempFile("source", null);
 442         file.deleteOnExit();
 443 
 444         RandomAccessFile raf = new RandomAccessFile(file, "rw");
 445         FileChannel fc = raf.getChannel();
 446 
 447         out.println("  Creating large file...");
 448         long t0 = System.nanoTime();
 449         try {
 450             fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
 451             long t1 = System.nanoTime();
 452             out.printf("  Created large file in %d ns (%d ms) %n",
 453             t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
 454         } catch (IOException x) {
 455             err.println("  Unable to create test file:" + x);
 456             fc.close();
 457             return;
 458         }
 459 
 460         // Setup looback connection and echo server
 461 
 462         ServerSocketChannel ssc = ServerSocketChannel.open();
 463         ssc.socket().bind(new InetSocketAddress(0));
 464 
 465         InetAddress lh = InetAddress.getLocalHost();
 466         InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
 467         SocketChannel source = SocketChannel.open(isa);
 468         SocketChannel sink = ssc.accept();
 469 
 470         Thread thr = new Thread(new EchoServer(sink));
 471         thr.start();
 472 
 473         // Test data is array of positions and counts
 474 
 475         long testdata[][] = {
 476             { 2*G-1,    1 },
 477             { 2*G-1,    10 },       // across 2GB boundary
 478             { 2*G,      1 },
 479             { 2*G,      10 },
 480             { 2*G+1,    1 },
 481             { 4*G-1,    1 },
 482             { 4*G-1,    10 },       // across 4GB boundary
 483             { 4*G,      1 },
 484             { 4*G,      10 },
 485             { 4*G+1,    1 },
 486             { 5*G-1,    1 },
 487             { 5*G-1,    10 },
 488             { 5*G,      1 },
 489             { 5*G,      10 },
 490             { 5*G+1,    1 },
 491             { 6*G,      1 },
 492         };
 493 
 494         ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
 495         ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
 496 
 497         try {
 498             byte value = 0;
 499             for (int i=0; i<testdata.length; i++) {
 500                 long position = testdata[(int)i][0];
 501                 long count = testdata[(int)i][1];
 502 
 503                 // generate bytes
 504                 for (long j=0; j<count; j++) {
 505                     sendbuf.put(++value);
 506                 }
 507                 sendbuf.flip();
 508 
 509                 // write to file and transfer to echo server
 510                 fc.write(sendbuf, position);
 511                 t0 = System.nanoTime();
 512                 fc.transferTo(position, count, source);
 513                 out.printf("  transferTo(%d, %2d, source): %d ns%n",
 514                     position, count, System.nanoTime() - t0);
 515 
 516                 // read from echo server
 517                 long nread = 0;
 518                 while (nread < count) {
 519                     int n = source.read(readbuf);
 520                     if (n < 0)
 521                         throw new RuntimeException("Premature EOF!");
 522                     nread += n;
 523                 }
 524 
 525                 // check reply from echo server
 526                 readbuf.flip();
 527                 sendbuf.flip();
 528                 if (!readbuf.equals(sendbuf))
 529                     throw new RuntimeException("Echoed bytes do not match!");
 530                 readbuf.clear();
 531                 sendbuf.clear();
 532             }
 533         } finally {
 534             source.close();
 535             ssc.close();
 536             fc.close();
 537             file.delete();
 538         }
 539     }
 540 
 541     // Test that transferFrom with FileChannel source that is not readable
 542     // throws NonReadableChannelException
 543     @Test
 544     public void xferTest09() throws Exception { // for bug 6984545
 545         File source = File.createTempFile("source", null);
 546         source.deleteOnExit();
 547 
 548         File target = File.createTempFile("target", null);
 549         target.deleteOnExit();
 550 
 551         FileChannel fc1 = new FileOutputStream(source).getChannel();
 552         FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();
 553         try {
 554             fc2.transferFrom(fc1, 0L, 0);
 555             throw new RuntimeException("NonReadableChannelException expected");
 556         } catch (NonReadableChannelException expected) {
 557         } finally {
 558             fc1.close();
 559             fc2.close();
 560         }
 561     }
 562 
 563     /**
 564      * Creates file blah of specified size in bytes.
 565      */
 566     private static void initTestFile(File blah, long size) throws Exception {
 567         if (blah.exists())
 568             blah.delete();
 569         FileOutputStream fos = new FileOutputStream(blah);
 570         BufferedWriter awriter
 571             = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
 572 
 573         for(int i=0; i<size; i++) {
 574             awriter.write("e");
 575         }
 576         awriter.flush();
 577         awriter.close();
 578     }
 579 
 580     /**
 581      * Simple in-process server to echo bytes read by a given socket channel
 582      */
 583     static class EchoServer implements Runnable {
 584         private SocketChannel sc;
 585 
 586         public EchoServer(SocketChannel sc) {
 587             this.sc = sc;
 588         }
 589 
 590         public void run() {
 591             ByteBuffer bb = ByteBuffer.allocateDirect(1024);
 592             try {
 593                 for (;;) {
 594                     int n = sc.read(bb);
 595                     if (n < 0)
 596                         break;
 597 
 598                     bb.flip();
 599                     while (bb.remaining() > 0) {
 600                         sc.write(bb);
 601                     }
 602                     bb.clear();
 603                 }
 604             } catch (IOException x) {
 605                 x.printStackTrace();
 606             } finally {
 607                 try {
 608                     sc.close();
 609                 } catch (IOException ignore) { }
 610             }
 611         }
 612     }
 613 
 614 }