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