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