Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferToChannel.java
41154 views
/*1* Copyright (c) 2002, 2010, 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 465249625* @summary Test transferTo with different target channels26* @run main TransferToChannel27* @run main/othervm -Djdk.nio.enableFastFileTransfer TransferToChannel28*/2930import java.nio.channels.FileChannel;31import java.nio.channels.WritableByteChannel;32import java.nio.ByteBuffer;33import java.io.*;34import java.util.Random;3536public class TransferToChannel {3738static File file;39static File outFile;40static FileChannel in;41// Chunk size should be larger than FileChannelImpl.TRANSFER_SIZE for good test42static int CHUNK_SIZE = 1024 * 9;4344public static void main(String[] args) throws Exception {45file = File.createTempFile("readingin", null);46outFile = File.createTempFile("writingout", null);47file.deleteOnExit();48outFile.deleteOnExit();49generateBigFile(file);50FileInputStream fis = new FileInputStream(file);51in = fis.getChannel();52test1();53test2();54in.close();55file.delete();56outFile.delete();57}5859static void test1() throws Exception {60for (int i=0; i<10; i++) {61transferFileToUserChannel();62System.gc();63System.err.println("Transferred file...");64}65}6667static void test2() throws Exception {68for (int i=0; i<10; i++) {69transferFileToTrustedChannel();70System.gc();71System.err.println("Transferred file...");72}73}7475static void transferFileToUserChannel() throws Exception {76long remainingBytes = in.size();77long size = remainingBytes;78WritableByteChannel wbc = new WritableByteChannel() {79Random rand = new Random(0);80public int write(ByteBuffer src) throws IOException {81int read = src.remaining();82byte[] incoming = new byte[read];83src.get(incoming);84checkData(incoming, read);85return read == 0 ? -1 : read;86}87public boolean isOpen() {88return true;89}90public void close() throws IOException {91}92void checkData(byte[] incoming, int size) {93byte[] expected = new byte[size];94rand.nextBytes(expected);95for (int i=0; i<size; i++)96if (incoming[i] != expected[i])97throw new RuntimeException("Data corrupted");98}99};100while (remainingBytes > 0) {101long bytesTransferred = in.transferTo(size - remainingBytes,102Math.min(CHUNK_SIZE, remainingBytes), wbc);103if (bytesTransferred >= 0)104remainingBytes -= bytesTransferred;105else106throw new Exception("transfer failed");107}108}109110static void transferFileToTrustedChannel() throws Exception {111long remainingBytes = in.size();112long size = remainingBytes;113FileOutputStream fos = new FileOutputStream(outFile);114FileChannel out = fos.getChannel();115while (remainingBytes > 0) {116long bytesTransferred = in.transferTo(size - remainingBytes,117CHUNK_SIZE, out);118if (bytesTransferred >= 0)119remainingBytes -= bytesTransferred;120else121throw new Exception("transfer failed");122}123out.close();124}125126static void generateBigFile(File file) throws Exception {127OutputStream out = new BufferedOutputStream(128new FileOutputStream(file));129byte[] randomBytes = new byte[1024];130Random rand = new Random(0);131for (int i = 0; i < 1000; i++) {132rand.nextBytes(randomBytes);133out.write(randomBytes);134}135out.flush();136out.close();137}138}139140141