Transfers.java revision 3261
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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
* @summary Comprehensive test for FileChannel.transfer{From,To}
* @bug 4708120
* @author Mark Reinhold
*/
public class Transfers {
private static class Failure
extends RuntimeException
{
super(x);
}
super(s);
}
}
// -- Writing and reading random bytes --
throws IOException
{
throw new IOException("Incomplete write");
}
private static void writeRandomBytes(long seed,
throws IOException
{
}
throws IOException
{
}
byte[] bytes)
throws IOException
{
throw new IOException("Incomplete read");
throw new Failure("Wrong data written");
}
long seed)
throws IOException
{
}
throws IOException
{
}
// For debugging
//
throws IOException
{
throw new IOException("Incomplete read");
byte prev = -1;
int r = 0; // Repeats
int n = 0;
if (b == prev) {
r++;
continue;
}
if (r > 0) {
int c = prev & 0xff;
if (c < 0x10)
if (r > 1) {
}
n++;
}
prev = b;
r = 1;
}
if (r > 0) {
int c = prev & 0xff;
if (c < 0x10)
if (r > 1) {
}
n++;
}
if (bb.hasRemaining())
}
static File sourceFile;
static File targetFile;
// -- Self-verifying sources and targets --
static abstract class Source {
protected final int size;
protected final long seed;
}
return name;
}
abstract ReadableByteChannel channel();
abstract void verify() throws IOException;
}
static class FileSource
extends Source
{
private final RandomAccessFile raf;
private final FileChannel fc;
fn = sourceFile;
}
return fc;
}
void verify() throws IOException {
throw new Failure("Wrong position: "
")");
}
}
static class UserSource
extends Source
{
private ReadableByteChannel ch;
private final ByteBuffer src;
ch = new ReadableByteChannel() {
if (!src.hasRemaining())
return -1;
return nr;
}
public boolean isOpen() {
return true;
}
public void close() { }
};
}
return ch;
}
void verify() {
+ " bytes remaining (expected 1)");
}
}
static abstract class Target {
protected final int size;
protected final long seed;
}
return name;
}
abstract WritableByteChannel channel();
abstract void verify() throws IOException;
}
static class FileTarget
extends Target
{
private final RandomAccessFile raf;
private final FileChannel fc;
fn = targetFile;
}
return fc;
}
void verify() throws IOException {
throw new Failure("Wrong position: "
}
}
static class UserTarget
extends Target
{
private WritableByteChannel ch;
private final ByteBuffer dst;
ch = new WritableByteChannel() {
return nr;
}
public boolean isOpen() {
return true;
}
public void close() { }
};
}
return ch;
}
void verify() {
+ " bytes remaining (expected 1)");
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) {
}
boolean hasNext() {
}
int next() {
throw new IllegalStateException();
if (cur < 6) {
cur++;
return cur;
}
p2 <<= 1;
return cur;
}
cur++;
return cur;
}
}
// -- Tests --
private static boolean debug = false;
private static boolean verbose = false;
if (!verbose)
return;
}
throws IOException
{
// Clear source, then randomize just the source region
// Randomize position
if (n != len)
throw new Failure("Incorrect transfer length: " + n
// Check that source wasn't changed
throw new Failure("Position changed");
if (debug)
// Check that target was updated correctly
}
throws IOException
{
// Clear target
// Randomize position
if (n != len)
throw new Failure("Incorrect transfer length: " + n
// Check that source didn't change, and was read correctly
// Check that target was updated correctly
throw new Failure("Position changed");
if (debug)
}
throws Exception
{
verbose = true;
}
fn.deleteOnExit();
int failures = 0;
if (!verbose)
+ ":");
try {
if (to) {
if (user)
else
}
else {
if (user)
else
}
} catch (Failure x) {
+ ", offset " + off
+ ", length " + len);
x.printStackTrace(out);
failures++;
}
}
}
if (!verbose)
if (user)
break;
}
if (to)
break;
}
sourceFile.delete();
targetFile.delete();
if (failures > 0) {
throw new RuntimeException("Some tests failed");
}
}
}