Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferTo6GBFile.java
41154 views
/*1* Copyright (c) 2001, 2017, 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 625314525* @summary Test FileChannel.transferTo with file positions up to 8GB26* @run testng/timeout=300 TransferTo6GBFile27*/2829import java.io.File;30import java.io.IOException;31import java.io.PrintStream;32import java.io.RandomAccessFile;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.nio.ByteBuffer;36import java.nio.channels.FileChannel;37import java.nio.channels.ServerSocketChannel;38import java.nio.channels.SocketChannel;39import java.util.concurrent.TimeUnit;4041import org.testng.annotations.Test;4243public class TransferTo6GBFile {4445private static PrintStream err = System.err;46private static PrintStream out = System.out;4748// Test transferTo with file positions larger than 2 and 4GB49@Test50public void xferTest08() throws Exception { // for bug 625314551final long G = 1024L * 1024L * 1024L;5253// Create 6GB file5455File file = File.createTempFile("source", null);56file.deleteOnExit();5758RandomAccessFile raf = new RandomAccessFile(file, "rw");59FileChannel fc = raf.getChannel();6061out.println(" Writing large file...");62long t0 = System.nanoTime();63try {64fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);65long t1 = System.nanoTime();66out.printf(" Wrote large file in %d ns (%d ms) %n",67t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));68} catch (IOException x) {69err.println(" Unable to create test file:" + x);70fc.close();71return;72}7374// Setup looback connection and echo server7576ServerSocketChannel ssc = ServerSocketChannel.open();77ssc.socket().bind(new InetSocketAddress(0));7879InetAddress lh = InetAddress.getLocalHost();80InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());81SocketChannel source = SocketChannel.open(isa);82SocketChannel sink = ssc.accept();8384Thread thr = new Thread(new EchoServer(sink));85thr.start();8687// Test data is array of positions and counts8889long testdata[][] = {90{ 2*G-1, 1 },91{ 2*G-1, 10 }, // across 2GB boundary92{ 2*G, 1 },93{ 2*G, 10 },94{ 2*G+1, 1 },95{ 4*G-1, 1 },96{ 4*G-1, 10 }, // across 4GB boundary97{ 4*G, 1 },98{ 4*G, 10 },99{ 4*G+1, 1 },100{ 5*G-1, 1 },101{ 5*G-1, 10 },102{ 5*G, 1 },103{ 5*G, 10 },104{ 5*G+1, 1 },105{ 6*G, 1 },106};107108ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);109ByteBuffer readbuf = ByteBuffer.allocateDirect(100);110111try {112byte value = 0;113for (int i=0; i<testdata.length; i++) {114long position = testdata[(int)i][0];115long count = testdata[(int)i][1];116117// generate bytes118for (long j=0; j<count; j++) {119sendbuf.put(++value);120}121sendbuf.flip();122123// write to file and transfer to echo server124fc.write(sendbuf, position);125t0 = System.nanoTime();126fc.transferTo(position, count, source);127out.printf(" transferTo(%d, %2d, source): %d ns%n",128position, count, System.nanoTime() - t0);129130// read from echo server131long nread = 0;132while (nread < count) {133int n = source.read(readbuf);134if (n < 0)135throw new RuntimeException("Premature EOF!");136nread += n;137}138139// check reply from echo server140readbuf.flip();141sendbuf.flip();142if (!readbuf.equals(sendbuf))143throw new RuntimeException("Echoed bytes do not match!");144readbuf.clear();145sendbuf.clear();146}147} finally {148source.close();149ssc.close();150fc.close();151file.delete();152}153}154155/**156* Simple in-process server to echo bytes read by a given socket channel157*/158static class EchoServer implements Runnable {159private SocketChannel sc;160161public EchoServer(SocketChannel sc) {162this.sc = sc;163}164165public void run() {166ByteBuffer bb = ByteBuffer.allocateDirect(1024);167try {168for (;;) {169int n = sc.read(bb);170if (n < 0)171break;172173bb.flip();174while (bb.remaining() > 0) {175sc.write(bb);176}177bb.clear();178}179} catch (IOException x) {180x.printStackTrace();181} finally {182try {183sc.close();184} catch (IOException ignore) { }185}186}187}188}189190191