Transfers.java revision 2546
1N/A/*
1N/A * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
1N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1N/A *
1N/A * This code is free software; you can redistribute it and/or modify it
1N/A * under the terms of the GNU General Public License version 2 only, as
1N/A * published by the Free Software Foundation.
1N/A *
1N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1N/A * version 2 for more details (a copy is included in the LICENSE file that
1N/A * accompanied this code).
1N/A *
1N/A * You should have received a copy of the GNU General Public License version
1N/A * 2 along with this work; if not, write to the Free Software Foundation,
1N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1N/A *
1N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1N/A * or visit www.oracle.com if you need additional information or have any
1N/A * questions.
1N/A */
1N/A
1N/A/* @test
1N/A * @summary Comprehensive test for FileChannel.transfer{From,To}
1N/A * @bug 4708120
1N/A * @author Mark Reinhold
1N/A */
1N/A
1N/Aimport java.io.*;
1N/Aimport java.nio.*;
1N/Aimport java.nio.channels.*;
1N/Aimport java.util.*;
1N/A
1N/A
1N/Apublic class Transfers {
1N/A
1N/A static PrintStream out = System.out;
1N/A
1N/A private static class Failure
1N/A extends RuntimeException
1N/A {
1N/A
1N/A Failure(Exception x) {
1N/A super(x);
1N/A }
1N/A
1N/A Failure(String s) {
1N/A super(s);
1N/A }
1N/A
1N/A }
1N/A
1N/A
1N/A // -- Writing and reading random bytes --
1N/A
1N/A private static void writeBytes(byte[] ba, FileChannel fc,
1N/A int off, int len)
1N/A throws IOException
1N/A {
1N/A fc.position(off);
1N/A if (fc.write(ByteBuffer.wrap(ba, 0, len)) != len)
1N/A throw new IOException("Incomplete write");
1N/A fc.position(0);
1N/A }
1N/A
1N/A private static void writeRandomBytes(long seed,
1N/A FileChannel fc, int off, int len)
1N/A throws IOException
1N/A {
1N/A Random r = new Random(seed);
1N/A byte[] ba = new byte[len];
1N/A r.nextBytes(ba);
1N/A writeBytes(ba, fc, off, len);
1N/A }
1N/A
1N/A private static void writeZeroBytes(FileChannel fc, int off, int len)
1N/A throws IOException
1N/A {
1N/A byte[] ba = new byte[len];
1N/A writeBytes(ba, fc, off, len);
1N/A }
1N/A
1N/A private static void checkBytes(FileChannel fc, int off, int len,
1N/A byte[] bytes)
1N/A throws IOException
1N/A {
1N/A ByteBuffer bb = ByteBuffer.allocate(len);
1N/A fc.position(off);
1N/A if (fc.read(bb) != len)
1N/A throw new IOException("Incomplete read");
1N/A bb.flip();
1N/A ByteBuffer bab = ByteBuffer.wrap(bytes, 0, len);
1N/A if (!bb.equals(bab))
1N/A throw new Failure("Wrong data written");
1N/A }
1N/A
1N/A private static void checkRandomBytes(FileChannel fc, int off, int len,
1N/A long seed)
1N/A throws IOException
1N/A {
1N/A byte[] ba = new byte[len];
1N/A Random r = new Random(seed);
1N/A r.nextBytes(ba);
1N/A checkBytes(fc, off, len, ba);
1N/A }
1N/A
1N/A private static void checkZeroBytes(FileChannel fc, int off, int len)
1N/A throws IOException
1N/A {
1N/A byte[] ba = new byte[len];
1N/A checkBytes(fc, off, len, ba);
1N/A }
1N/A
1N/A // For debugging
1N/A //
1N/A private static void dump(FileChannel fc)
1N/A throws IOException
1N/A {
1N/A int sz = (int)fc.size();
1N/A ByteBuffer bb = ByteBuffer.allocate(sz);
1N/A fc.position(0);
1N/A if (fc.read(bb) != sz)
1N/A throw new IOException("Incomplete read");
1N/A bb.flip();
1N/A byte prev = -1;
1N/A int r = 0; // Repeats
1N/A int n = 0;
1N/A while (bb.hasRemaining() && (n < 32)) {
1N/A byte b = bb.get();
1N/A if (b == prev) {
1N/A r++;
1N/A continue;
1N/A }
1N/A if (r > 0) {
1N/A int c = prev & 0xff;
1N/A if (c < 0x10)
1N/A out.print('0');
1N/A out.print(Integer.toHexString(c));
1N/A if (r > 1) {
1N/A out.print("[");
1N/A out.print(r);
1N/A out.print("]");
1N/A }
1N/A n++;
1N/A }
1N/A prev = b;
1N/A r = 1;
1N/A }
1N/A if (r > 0) {
1N/A int c = prev & 0xff;
1N/A if (c < 0x10)
1N/A out.print('0');
1N/A out.print(Integer.toHexString(c));
1N/A if (r > 1) {
1N/A out.print("[");
1N/A out.print(r);
1N/A out.print("]");
1N/A }
1N/A n++;
1N/A }
1N/A if (bb.hasRemaining())
1N/A out.print("...");
1N/A out.println();
1N/A }
1N/A
1N/A
1N/A
1N/A static File sourceFile;
1N/A static File targetFile;
1N/A
1N/A // -- Self-verifying sources and targets --
1N/A
1N/A static abstract class Source {
1N/A
1N/A protected final int size;
1N/A protected final long seed;
1N/A private final String name;
1N/A
1N/A Source(int size, long seed, String name) {
1N/A this.size = size;
1N/A this.seed = seed;
1N/A this.name = name;
1N/A }
1N/A
1N/A String name() {
1N/A return name;
1N/A }
1N/A
1N/A abstract ReadableByteChannel channel();
1N/A
1N/A abstract void verify() throws IOException;
1N/A
1N/A }
1N/A
1N/A static class FileSource
1N/A extends Source
1N/A {
1N/A private final File fn;
1N/A private final RandomAccessFile raf;
1N/A private final FileChannel fc;
1N/A
1N/A FileSource(int size, long seed) throws IOException {
1N/A super(size, seed, "FileChannel");
1N/A fn = sourceFile;
1N/A raf = new RandomAccessFile(fn, "rw");
1N/A fc = raf.getChannel();
1N/A fc.position(0);
1N/A writeRandomBytes(seed, fc, 0, size);
1N/A }
1N/A
1N/A ReadableByteChannel channel() {
1N/A return fc;
1N/A }
1N/A
1N/A void verify() throws IOException {
1N/A if (fc.position() != size)
1N/A throw new Failure("Wrong position: "
1N/A + fc.position() + " (expected " + size +
1N/A ")");
checkRandomBytes(fc, 0, size, seed);
fc.close();
raf.close(); // Bug in 1.4.0
}
}
static class UserSource
extends Source
{
private ReadableByteChannel ch;
private final ByteBuffer src;
UserSource(int size, long seed) {
super(size, seed, "UserChannel");
final byte[] bytes = new byte[size + 1];
Random r = new Random(seed);
r.nextBytes(bytes);
src = ByteBuffer.wrap(bytes);
ch = new ReadableByteChannel() {
public int read(ByteBuffer dst) {
if (!src.hasRemaining())
return -1;
int nr = Math.min(src.remaining(), dst.remaining());
ByteBuffer s = src.duplicate();
s.limit(s.position() + nr);
dst.put(s);
src.position(src.position() + nr);
return nr;
}
public boolean isOpen() {
return true;
}
public void close() { }
};
}
ReadableByteChannel channel() {
return ch;
}
void verify() {
if (src.remaining() != 1)
throw new Failure("Source has " + src.remaining()
+ " bytes remaining (expected 1)");
}
}
static abstract class Target {
protected final int size;
protected final long seed;
private final String name;
Target(int size, long seed, String name) {
this.size = size;
this.seed = seed;
this.name = name;
}
String name() {
return name;
}
abstract WritableByteChannel channel();
abstract void verify() throws IOException;
}
static class FileTarget
extends Target
{
private final File fn;
private final RandomAccessFile raf;
private final FileChannel fc;
FileTarget(int size, long seed) throws IOException {
super(size, seed, "FileChannel");
fn = targetFile;
raf = new RandomAccessFile(fn, "rw");
fc = raf.getChannel();
fc.position(0);
}
WritableByteChannel channel() {
return fc;
}
void verify() throws IOException {
if (fc.position() != size)
throw new Failure("Wrong position: "
+ fc.position() + " (expected " + size + ")");
checkRandomBytes(fc, 0, size, seed);
fc.close();
raf.close(); // Bug in 1.4.0
}
}
static class UserTarget
extends Target
{
private WritableByteChannel ch;
private final ByteBuffer dst;
UserTarget(int size, long seed) {
super(size, seed, "UserChannel");
dst = ByteBuffer.wrap(new byte[size + 1]);
ch = new WritableByteChannel() {
public int write(ByteBuffer src) {
int nr = Math.min(src.remaining(), dst.remaining());
ByteBuffer s = src.duplicate();
s.limit(s.position() + nr);
dst.put(s);
src.position(src.position() + nr);
return nr;
}
public boolean isOpen() {
return true;
}
public void close() { }
};
}
WritableByteChannel channel() {
return ch;
}
void verify() {
if (dst.remaining() != 1)
throw new Failure("Destination has " + dst.remaining()
+ " bytes remaining (expected 1)");
byte[] ba = new byte[size];
Random r = new Random(seed);
r.nextBytes(ba);
dst.flip();
ByteBuffer rbb = ByteBuffer.wrap(ba, 0, size);
if (!dst.equals(rbb))
throw new Failure("Wrong data written");
}
}
// Generates a sequence of ints of the form 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
// 15, 16, 17, 31, 32, 33, ..., 2^i-1, 2^i, 2^i+1, ..., max.
static class IntGenerator {
private int max;
private int cur = -1;
private int p2 = 8;
IntGenerator(int max) {
this.max = max;
}
boolean hasNext() {
return cur < max;
}
int next() {
if (cur >= max)
throw new IllegalStateException();
if (cur < 6) {
cur++;
return cur;
}
if (cur == p2 + 1) {
p2 <<= 1;
cur = p2 - 1;
return cur;
}
cur++;
return cur;
}
}
// -- Tests --
private static final int MAX_XFER_SIZE = 1 << 14;
private static final int MAX_FILE_SIZE = MAX_XFER_SIZE << 1;
private static boolean debug = false;
private static boolean verbose = false;
static void show(String dir, String channelName, int off, int len) {
if (!verbose)
return;
out.println(dir + " " + channelName +
": offset " + off + ", length " + len);
}
static void testTo(long seed, FileChannel fc, int off, int len, Target tgt)
throws IOException
{
show("To", tgt.name(), off, len);
// Clear source, then randomize just the source region
writeZeroBytes(fc, 0, MAX_FILE_SIZE);
writeRandomBytes(seed, fc, off, len);
// Randomize position
int pos = (int)seed & 0xfff;
fc.position(pos);
int n = (int)fc.transferTo(off, len, tgt.channel());
if (n != len)
throw new Failure("Incorrect transfer length: " + n
+ " (expected " + len + ")");
// Check that source wasn't changed
if (fc.position() != pos)
throw new Failure("Position changed");
if (debug)
dump(fc);
checkRandomBytes(fc, off, len, seed);
writeZeroBytes(fc, off, len);
checkZeroBytes(fc, 0, MAX_FILE_SIZE);
// Check that target was updated correctly
tgt.verify();
}
static void testFrom(long seed, Source src, FileChannel fc, int off, int len)
throws IOException
{
show("From", src.name(), off, len);
// Clear target
writeZeroBytes(fc, 0, MAX_FILE_SIZE);
// Randomize position
int pos = (int)seed & 0xfff;
fc.position(pos);
int n = (int)fc.transferFrom(src.channel(), off, len);
if (n != len)
throw new Failure("Incorrect transfer length: " + n
+ " (expected " + len + ")");
// Check that source didn't change, and was read correctly
src.verify();
// Check that target was updated correctly
if (fc.position() != pos)
throw new Failure("Position changed");
if (debug)
dump(fc);
checkRandomBytes(fc, off, len, seed);
writeZeroBytes(fc, off, len);
checkZeroBytes(fc, 0, MAX_FILE_SIZE);
}
public static void main(String[] args)
throws Exception
{
if (args.length > 0) {
if (args[0].indexOf('v') >= 0)
verbose = true;
if (args[0].indexOf('d') >= 0)
debug = verbose = true;
}
sourceFile = File.createTempFile("xfer.src.", "");
sourceFile.deleteOnExit();
targetFile = File.createTempFile("xfer.tgt.", "");
targetFile.deleteOnExit();
File fn = File.createTempFile("xfer.fch.", "");
fn.deleteOnExit();
FileChannel fc = new RandomAccessFile(fn, "rw").getChannel();
Random rnd = new Random();
int failures = 0;
for (boolean to = false;; to = true) {
for (boolean user = false;; user = true) {
if (!verbose)
out.print((to ? "To " : "From ") +
(user ? "user channel" : "file channel")
+ ":");
IntGenerator offGen = new IntGenerator(MAX_XFER_SIZE + 2);
while (offGen.hasNext()) {
int off = offGen.next();
if (!verbose) out.print(" " + off);
IntGenerator lenGen = new IntGenerator(MAX_XFER_SIZE + 2);
while (lenGen.hasNext()) {
int len = lenGen.next();
long s = rnd.nextLong();
String chName = null;
try {
if (to) {
Target tgt;
if (user)
tgt = new UserTarget(len, s);
else
tgt = new FileTarget(len, s);
chName = tgt.name();
testTo(s, fc, off, len, tgt);
}
else {
Source src;
if (user)
src = new UserSource(len, s);
else
src = new FileSource(len, s);
chName = src.name();
testFrom(s, src, fc, off, len);
}
} catch (Failure x) {
out.println();
out.println("FAILURE: " + chName
+ ", offset " + off
+ ", length " + len);
x.printStackTrace(out);
failures++;
}
}
}
if (!verbose)
out.println();
if (user)
break;
}
if (to)
break;
}
sourceFile.delete();
targetFile.delete();
fn.delete();
if (failures > 0) {
out.println();
throw new RuntimeException("Some tests failed");
}
}
}