public static long transfer( InputStream in,
				 OutputStream out,
				 long maxReadLength,
				 int bufferSize ) 
	throws IllegalArgumentException,
	       IOException {

	byte[] buffer = new byte[ bufferSize ];
	int len;
	long totalLength = 0;
	long bytesLeft = maxReadLength; // Math.max( maxReadLength, bufferSize );

	while( (maxReadLength == -1 || totalLength < maxReadLength)
	       //&& (len = in.read(buffer,0,(int)bytesLeft)) > 0 ) {
	       && (len = in.read(buffer,0,(int)Math.max( maxReadLength, bufferSize ))) != -1 ) {

	    //for( int i = 0; i < len; i++ )
	    //	System.out.print( "X" + (char)buffer[i] );

	    out.write( buffer, 0, len );
	    out.flush();
	    
	    totalLength += len;
	    bytesLeft -= len;
	}

	return totalLength;
    }