0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4652496
0N/A * @summary Test transferTo with different target channels
0N/A */
0N/A
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport java.nio.channels.WritableByteChannel;
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.io.*;
0N/Aimport java.util.Random;
0N/A
0N/Apublic class TransferToChannel {
0N/A
0N/A static File file;
0N/A static File outFile;
0N/A static FileChannel in;
0N/A // Chunk size should be larger than FileChannelImpl.TRANSFER_SIZE for good test
0N/A static int CHUNK_SIZE = 1024 * 9;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A file = File.createTempFile("readingin", null);
0N/A outFile = File.createTempFile("writingout", null);
0N/A file.deleteOnExit();
0N/A outFile.deleteOnExit();
0N/A generateBigFile(file);
0N/A FileInputStream fis = new FileInputStream(file);
0N/A in = fis.getChannel();
0N/A test1();
0N/A test2();
0N/A in.close();
2546N/A file.delete();
2546N/A outFile.delete();
0N/A }
0N/A
0N/A static void test1() throws Exception {
0N/A for (int i=0; i<10; i++) {
0N/A transferFileToUserChannel();
0N/A System.gc();
0N/A System.err.println("Transferred file...");
0N/A }
0N/A }
0N/A
0N/A static void test2() throws Exception {
0N/A for (int i=0; i<10; i++) {
0N/A transferFileToTrustedChannel();
0N/A System.gc();
0N/A System.err.println("Transferred file...");
0N/A }
0N/A }
0N/A
0N/A static void transferFileToUserChannel() throws Exception {
0N/A long remainingBytes = in.size();
0N/A long size = remainingBytes;
0N/A WritableByteChannel wbc = new WritableByteChannel() {
0N/A Random rand = new Random(0);
0N/A public int write(ByteBuffer src) throws IOException {
0N/A int read = src.remaining();
0N/A byte[] incoming = new byte[read];
0N/A src.get(incoming);
0N/A checkData(incoming, read);
0N/A return read == 0 ? -1 : read;
0N/A }
0N/A public boolean isOpen() {
0N/A return true;
0N/A }
0N/A public void close() throws IOException {
0N/A }
0N/A void checkData(byte[] incoming, int size) {
0N/A byte[] expected = new byte[size];
0N/A rand.nextBytes(expected);
0N/A for (int i=0; i<size; i++)
0N/A if (incoming[i] != expected[i])
0N/A throw new RuntimeException("Data corrupted");
0N/A }
0N/A };
0N/A while (remainingBytes > 0) {
0N/A long bytesTransferred = in.transferTo(size - remainingBytes,
0N/A Math.min(CHUNK_SIZE, remainingBytes), wbc);
0N/A if (bytesTransferred >= 0)
0N/A remainingBytes -= bytesTransferred;
0N/A else
0N/A throw new Exception("transfer failed");
0N/A }
0N/A }
0N/A
0N/A static void transferFileToTrustedChannel() throws Exception {
0N/A long remainingBytes = in.size();
0N/A long size = remainingBytes;
0N/A FileOutputStream fos = new FileOutputStream(outFile);
0N/A FileChannel out = fos.getChannel();
0N/A while (remainingBytes > 0) {
0N/A long bytesTransferred = in.transferTo(size - remainingBytes,
0N/A CHUNK_SIZE, out);
0N/A if (bytesTransferred >= 0)
0N/A remainingBytes -= bytesTransferred;
0N/A else
0N/A throw new Exception("transfer failed");
0N/A }
0N/A out.close();
0N/A }
0N/A
0N/A static void generateBigFile(File file) throws Exception {
0N/A OutputStream out = new BufferedOutputStream(
0N/A new FileOutputStream(file));
0N/A byte[] randomBytes = new byte[1024];
0N/A Random rand = new Random(0);
0N/A for (int i = 0; i < 1000; i++) {
0N/A rand.nextBytes(randomBytes);
0N/A out.write(randomBytes);
0N/A }
0N/A out.flush();
0N/A out.close();
0N/A }
0N/A}