/* * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 4434723 4482726 4559072 4795550 5081340 5103988 6984545 * @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed) * @library .. * @library /test/lib * @build jdk.test.lib.RandomFactory * @run testng/timeout=300 Transfer * @key randomness
*/
int totalWritten = 0; while (totalWritten < size + 10) { int written = sink.write(outgoingdata); if (written < 0) thrownew Exception("Write failed");
totalWritten += written;
}
File f = File.createTempFile("blah"+size, null);
f.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
FileChannel fc = raf.getChannel(); long oldPosition = fc.position();
long bytesWritten = fc.transferFrom(source, 0, size);
fc.force(true); if (bytesWritten != size) thrownew RuntimeException("Transfer failed");
if (fc.position() != oldPosition) thrownew RuntimeException("Position changed");
if (fc.size() != size) thrownew RuntimeException("Unexpected sink size "+ fc.size());
// get filechannel for the source file.
File source = File.createTempFile("source", null);
source.deleteOnExit();
RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
FileChannel fc1 = raf1.getChannel();
// write out data to the file channel long bytesWritten = 0; while (bytesWritten < 5000) {
bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
}
// get filechannel for the dst file.
File dest = File.createTempFile("dest", null);
dest.deleteOnExit();
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
FileChannel fc2 = raf2.getChannel();
int bytesToWrite = 3000; int startPosition = 1000;
// get filechannel for the source file.
File source = File.createTempFile("source", null);
source.deleteOnExit();
RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
FileChannel fc1 = raf1.getChannel();
fc1.truncate(0);
// write out data to the file channel int bytesWritten = 0; while (bytesWritten < 4) {
bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
}
// get filechannel for the dst file.
File dest = File.createTempFile("dest", null);
dest.deleteOnExit();
RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
FileChannel fc2 = raf2.getChannel();
fc2.truncate(0);
fc1.transferTo(0, srcData.length + 1, fc2);
if (fc2.size() > 4) thrownew Exception("xferTest03 failed");
// xferTest04() and xferTest05() moved to Transfer4GBFile.java
staticvoid checkFileData(File file, String expected) throws Exception {
FileInputStream fis = new FileInputStream(file);
Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
StringBuilder sb = new StringBuilder(); int c; while ((c = r.read()) != -1)
sb.append((char)c);
String contents = sb.toString(); if (! contents.equals(expected)) thrownew Exception("expected: " + expected
+ ", got: " + contents);
r.close();
}
// Test transferFrom asking for more bytes than remain in source
@Test publicvoid xferTest06() throws Exception { // for bug 5081340
String data = "Use the source, Luke!";
// Test transferTo to non-blocking socket channel
@Test publicvoid xferTest07() throws Exception { // for bug 5103988
File source = File.createTempFile("source", null);
source.deleteOnExit();
FileChannel sourceChannel = new RandomAccessFile(source, "rw")
.getChannel();
sourceChannel.position(32000L)
.write(ByteBuffer.wrap("The End".getBytes()));
// The sink is a non-blocking socket channel
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(0));
InetSocketAddress sa = new InetSocketAddress(
InetAddress.getLocalHost(), ssc.socket().getLocalPort());
SocketChannel sink = SocketChannel.open(sa);
sink.configureBlocking(false);
SocketChannel other = ssc.accept();
long size = sourceChannel.size();
// keep sending until congested long n; do {
n = sourceChannel.transferTo(0, size, sink);
} while (n > 0);
// Test that transferFrom with FileChannel source that is not readable // throws NonReadableChannelException
@Test publicvoid xferTest09() throws Exception { // for bug 6984545
File source = File.createTempFile("source", null);
source.deleteOnExit();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.