Transfer.java revision 5116:d45bc4307996
1/*
2 * Copyright (c) 2001, 2011, 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
31import 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;
39
40
41public 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") || osName.startsWith("Mac OS")))
232            return;
233        File source = File.createTempFile("blah", null);
234        source.deleteOnExit();
235        long testSize = ((long)Integer.MAX_VALUE) * 2;
236        initTestFile(source, 10);
237        RandomAccessFile raf = new RandomAccessFile(source, "rw");
238        FileChannel fc = raf.getChannel();
239        fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
240        fc.close();
241        raf.close();
242
243        File sink = File.createTempFile("sink", null);
244        sink.deleteOnExit();
245
246        FileInputStream fis = new FileInputStream(source);
247        FileChannel sourceChannel = fis.getChannel();
248
249        raf = new RandomAccessFile(sink, "rw");
250        FileChannel sinkChannel = raf.getChannel();
251
252        long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
253                                                     sinkChannel);
254        if (bytesWritten != 10) {
255            throw new RuntimeException("Transfer test 4 failed " +
256                                       bytesWritten);
257        }
258        sourceChannel.close();
259        sinkChannel.close();
260
261        source.delete();
262        sink.delete();
263    }
264
265    // Test transferFrom with large file
266    public static void xferTest05() throws Exception {
267        // Create a source file & large sink file for the test
268        File source = File.createTempFile("blech", null);
269        source.deleteOnExit();
270        initTestFile(source, 100);
271
272        // Create the sink file as a sparse file if possible
273        File sink = null;
274        FileChannel fc = null;
275        while (fc == null) {
276            sink = File.createTempFile("sink", null);
277            // re-create as a sparse file
278            sink.delete();
279            try {
280                fc = FileChannel.open(sink.toPath(),
281                                      StandardOpenOption.CREATE_NEW,
282                                      StandardOpenOption.WRITE,
283                                      StandardOpenOption.SPARSE);
284            } catch (FileAlreadyExistsException ignore) {
285                // someone else got it
286            }
287        }
288        sink.deleteOnExit();
289
290        long testSize = ((long)Integer.MAX_VALUE) * 2;
291        try {
292            fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
293                     testSize - 40);
294        } catch (IOException e) {
295            // Can't set up the test, abort it
296            System.err.println("xferTest05 was aborted.");
297            return;
298        } finally {
299            fc.close();
300        }
301
302        // Get new channels for the source and sink and attempt transfer
303        FileChannel sourceChannel = new FileInputStream(source).getChannel();
304        try {
305            FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
306            try {
307                long bytesWritten = sinkChannel.transferFrom(sourceChannel,
308                                                             testSize - 40, 10);
309                if (bytesWritten != 10) {
310                    throw new RuntimeException("Transfer test 5 failed " +
311                                               bytesWritten);
312                }
313            } finally {
314                sinkChannel.close();
315            }
316        } finally {
317            sourceChannel.close();
318        }
319
320        source.delete();
321        sink.delete();
322    }
323
324    static void checkFileData(File file, String expected) throws Exception {
325        FileInputStream fis = new FileInputStream(file);
326        Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
327        StringBuilder sb = new StringBuilder();
328        int c;
329        while ((c = r.read()) != -1)
330            sb.append((char)c);
331        String contents = sb.toString();
332        if (! contents.equals(expected))
333            throw new Exception("expected: " + expected
334                                + ", got: " + contents);
335        r.close();
336    }
337
338    // Test transferFrom asking for more bytes than remain in source
339    public static void xferTest06() throws Exception {
340        String data = "Use the source, Luke!";
341
342        File source = File.createTempFile("source", null);
343        source.deleteOnExit();
344        File sink = File.createTempFile("sink", null);
345        sink.deleteOnExit();
346
347        FileOutputStream fos = new FileOutputStream(source);
348        fos.write(data.getBytes("ASCII"));
349        fos.close();
350
351        FileChannel sourceChannel =
352            new RandomAccessFile(source, "rw").getChannel();
353        sourceChannel.position(7);
354        long remaining = sourceChannel.size() - sourceChannel.position();
355        FileChannel sinkChannel =
356            new RandomAccessFile(sink, "rw").getChannel();
357        long n = sinkChannel.transferFrom(sourceChannel, 0L,
358                                          sourceChannel.size()); // overflow
359        if (n != remaining)
360            throw new Exception("n == " + n + ", remaining == " + remaining);
361
362        sinkChannel.close();
363        sourceChannel.close();
364
365        checkFileData(source, data);
366        checkFileData(sink, data.substring(7,data.length()));
367
368        source.delete();
369    }
370
371    // Test transferTo to non-blocking socket channel
372    public static void xferTest07() throws Exception {
373        File source = File.createTempFile("source", null);
374        source.deleteOnExit();
375
376        FileChannel sourceChannel = new RandomAccessFile(source, "rw")
377            .getChannel();
378        sourceChannel.position(32000L)
379            .write(ByteBuffer.wrap("The End".getBytes()));
380
381        // The sink is a non-blocking socket channel
382        ServerSocketChannel ssc = ServerSocketChannel.open();
383        ssc.socket().bind(new InetSocketAddress(0));
384        InetSocketAddress sa = new InetSocketAddress(
385            InetAddress.getLocalHost(), ssc.socket().getLocalPort());
386        SocketChannel sink = SocketChannel.open(sa);
387        sink.configureBlocking(false);
388        SocketChannel other = ssc.accept();
389
390        long size = sourceChannel.size();
391
392        // keep sending until congested
393        long n;
394        do {
395            n = sourceChannel.transferTo(0, size, sink);
396        } while (n > 0);
397
398        sourceChannel.close();
399        sink.close();
400        other.close();
401        ssc.close();
402        source.delete();
403    }
404
405
406    // Test transferTo with file positions larger than 2 and 4GB
407    public static void xferTest08() throws Exception {
408        // Creating a sparse 6GB file on Windows takes too long
409        String osName = System.getProperty("os.name");
410        if (osName.startsWith("Windows"))
411            return;
412
413        final long G = 1024L * 1024L * 1024L;
414
415        // Create 6GB file
416
417        File file = File.createTempFile("source", null);
418        file.deleteOnExit();
419
420        RandomAccessFile raf = new RandomAccessFile(file, "rw");
421        FileChannel fc = raf.getChannel();
422
423        try {
424            fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
425        } catch (IOException x) {
426            System.err.println("Unable to create test file:" + x);
427            fc.close();
428            return;
429        }
430
431        // Setup looback connection and echo server
432
433        ServerSocketChannel ssc = ServerSocketChannel.open();
434        ssc.socket().bind(new InetSocketAddress(0));
435
436        InetAddress lh = InetAddress.getLocalHost();
437        InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
438        SocketChannel source = SocketChannel.open(isa);
439        SocketChannel sink = ssc.accept();
440
441        Thread thr = new Thread(new EchoServer(sink));
442        thr.start();
443
444        // Test data is array of positions and counts
445
446        long testdata[][] = {
447            { 2*G-1,    1 },
448            { 2*G-1,    10 },       // across 2GB boundary
449            { 2*G,      1 },
450            { 2*G,      10 },
451            { 2*G+1,    1 },
452            { 4*G-1,    1 },
453            { 4*G-1,    10 },       // across 4GB boundary
454            { 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        };
464
465        ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
466        ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
467
468        try {
469            byte value = 0;
470            for (int i=0; i<testdata.length; i++) {
471                long position = testdata[(int)i][0];
472                long count = testdata[(int)i][1];
473
474                // generate bytes
475                for (long j=0; j<count; j++) {
476                    sendbuf.put(++value);
477                }
478                sendbuf.flip();
479
480                // write to file and transfer to echo server
481                fc.write(sendbuf, position);
482                fc.transferTo(position, count, source);
483
484                // read from echo server
485                long nread = 0;
486                while (nread < count) {
487                    int n = source.read(readbuf);
488                    if (n < 0)
489                        throw new RuntimeException("Premature EOF!");
490                    nread += n;
491                }
492
493                // check reply from echo server
494                readbuf.flip();
495                sendbuf.flip();
496                if (!readbuf.equals(sendbuf))
497                    throw new RuntimeException("Echo'ed bytes do not match!");
498                readbuf.clear();
499                sendbuf.clear();
500            }
501        } finally {
502            source.close();
503            ssc.close();
504            fc.close();
505            file.delete();
506        }
507    }
508
509    // Test that transferFrom with FileChannel source that is not readable
510    // throws NonReadableChannelException
511    static void xferTest09() throws Exception {
512        File source = File.createTempFile("source", null);
513        source.deleteOnExit();
514
515        File target = File.createTempFile("target", null);
516        target.deleteOnExit();
517
518        FileChannel fc1 = new FileOutputStream(source).getChannel();
519        FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();
520        try {
521            fc2.transferFrom(fc1, 0L, 0);
522            throw new RuntimeException("NonReadableChannelException expected");
523        } catch (NonReadableChannelException expected) {
524        } finally {
525            fc1.close();
526            fc2.close();
527        }
528    }
529
530    /**
531     * Creates file blah of specified size in bytes.
532     */
533    private static void initTestFile(File blah, long size) throws Exception {
534        if (blah.exists())
535            blah.delete();
536        FileOutputStream fos = new FileOutputStream(blah);
537        BufferedWriter awriter
538            = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
539
540        for(int i=0; i<size; i++) {
541            awriter.write("e");
542        }
543        awriter.flush();
544        awriter.close();
545    }
546
547    /**
548     * Simple in-process server to echo bytes read by a given socket channel
549     */
550    static class EchoServer implements Runnable {
551        private SocketChannel sc;
552
553        public EchoServer(SocketChannel sc) {
554            this.sc = sc;
555        }
556
557        public void run() {
558            ByteBuffer bb = ByteBuffer.allocateDirect(1024);
559            try {
560                for (;;) {
561                    int n = sc.read(bb);
562                    if (n < 0)
563                        break;
564
565                    bb.flip();
566                    while (bb.remaining() > 0) {
567                        sc.write(bb);
568                    }
569                    bb.clear();
570                }
571            } catch (IOException x) {
572                x.printStackTrace();
573            } finally {
574                try {
575                    sc.close();
576                } catch (IOException ignore) { }
577            }
578        }
579    }
580
581}
582