Path: blob/master/test/jdk/java/nio/channels/FileChannel/TransferOverlappedFileChannel.java
66645 views
/*1* Copyright (c) 2021, 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 814024125* @summary Test transferring to and from same file channel26*/27import java.io.BufferedOutputStream;28import java.io.File;29import java.io.FileOutputStream;30import java.io.OutputStream;31import java.io.RandomAccessFile;32import java.io.IOException;33import java.nio.channels.FileChannel;34import java.util.Random;3536public class TransferOverlappedFileChannel {3738public static void main(String[] args) throws Exception {39File file = File.createTempFile("readingin", null);40file.deleteOnExit();41generateBigFile(file);42RandomAccessFile raf = new RandomAccessFile(file, "rw");43try (FileChannel channel = raf.getChannel()) {44transferToNoOverlap(file, channel);45transferToOverlap(file, channel);46transferFromNoOverlap(file, channel);47transferFromOverlap(file, channel);48} finally {49file.delete();50}51}5253private static void transferToNoOverlap(File file, FileChannel channel)54throws IOException {55final long length = file.length();5657// position at three quarters58channel.position(length*3/4);59// copy last quarter to third quarter60// (copied and overwritten regions do NOT overlap)61// So: 1 2 3 4 -> 1 2 4 462channel.transferTo(length / 2, length / 4, channel);63System.out.println("transferToNoOverlap: OK");64}6566private static void transferToOverlap(File file, FileChannel channel)67throws IOException {68final long length = file.length();6970// position at half71channel.position(length/2);72// copy last half to second quarter73// (copied and overwritten regions DO overlap)74// So: 1 2 3 4 -> 1 3 4 475channel.transferTo(length / 4, length / 2, channel);76System.out.println("transferToOverlap: OK");77}7879private static void transferFromNoOverlap(File file, FileChannel channel)80throws IOException {81final long length = file.length();8283// position at three quarters84channel.position(length*3/4);85// copy last quarter to third quarter86// (copied and overwritten regions do NOT overlap)87// So: 1 2 3 4 -> 1 2 4 488channel.transferFrom(channel, length / 2, length / 4);89System.out.println("transferFromNoOverlap: OK");90}9192private static void transferFromOverlap(File file, FileChannel channel)93throws IOException {94final long length = file.length();9596// position at half97channel.position(length/2);98// copy last half to second quarter99// (copied and overwritten regions DO overlap)100// So: 1 2 3 4 -> 1 3 4 4101channel.transferFrom(channel, length / 4, length / 2);102System.out.println("transferFromOverlap: OK");103}104105private static void generateBigFile(File file) throws Exception {106try (OutputStream out = new BufferedOutputStream(107new FileOutputStream(file))) {108byte[] randomBytes = new byte[1024];109Random rand = new Random(0);110rand.nextBytes(randomBytes);111for (int i = 0; i < 1024; i++) {112out.write(randomBytes);113}114out.flush();115}116}117}118119120