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 und die Messung sind noch experimentell.