27import java.nio.ByteBuffer;
28import java.nio.charset.Charset;
29import java.nio.file.Path;
30import java.nio.file.Paths;
31import java.util.ArrayList;
32import java.util.Arrays;
35import org.jau.fs.FMode;
36import org.jau.fs.FileStats;
37import org.jau.fs.FileUtil;
38import org.jau.fs.TraverseOptions;
39import org.jau.io.Buffers;
40import org.jau.io.ByteInStream;
41import org.jau.io.ByteInStreamUtil;
42import org.jau.io.ByteInStream_Feed;
43import org.jau.io.ByteInStream_File;
44import org.jau.io.ByteInStream_URL;
45import org.jau.io.ByteOutStream_File;
46import org.jau.io.PrintUtil;
47import org.jau.io.UriTk;
48import org.junit.AfterClass;
49import org.junit.Assert;
50import org.junit.FixMethodOrder;
52import org.junit.runners.MethodSorters;
54import jau.pkg.PlatformRuntime;
55import jau.test.junit.util.JunitTracer;
57@FixMethodOrder(MethodSorters.NAME_ASCENDING)
59 static final boolean DEBUG =
false;
61 static final String payload_version =
"0";
62 static final String payload_version_parent =
"0";
64 static final int IDX_11kiB = 0;
65 static final int IDX_65MiB = 1;
66 static List<String> fname_payload_lst =
new ArrayList<String>();
67 static List<String> fname_payload_copy_lst =
new ArrayList<String>();
68 static List<Long> fname_payload_size_lst =
new ArrayList<Long>();
70 static boolean file_exists(
final String name) {
71 final FileStats stats =
new FileStats( name );
72 return stats.is_file();
75 static long file_size(
final String name) {
76 final FileStats stats =
new FileStats( name );
80 static boolean remove_file(
final String name) {
81 final FileStats stats =
new FileStats( name );
83 if( stats.is_file() ) {
84 if( !FileUtil.remove(name, TraverseOptions.none) ) {
85 PrintUtil.println(System.err,
"Remove.1: Failed deletion of existing file "+name);
90 return !stats.exists();
91 }
catch (
final Exception ex) {
92 PrintUtil.println(System.err,
"Remove.2: Failed deletion of existing file "+name+
": "+ex.getMessage());
98 static boolean add_test_file(
final String name,
final long size_limit) {
99 Assert.assertTrue( remove_file(name) );
100 Assert.assertTrue( remove_file(name+
".enc") );
101 Assert.assertTrue( remove_file(name+
".enc.dec") );
104 final String one_line =
"Hello World, this is a test and I like it. Exactly 100 characters long. 0123456780 abcdefghjklmnop..";
105 final Charset charset = Charset.forName(
"ASCII");
106 final byte[] one_line_bytes = one_line.getBytes(charset);
108 Assert.assertFalse( file_exists(name) );
109 try (ByteOutStream_File out =
new ByteOutStream_File(name, FMode.def_file)) {
110 Assert.assertTrue( out.good() );
111 Assert.assertTrue( out.is_open() );
113 final int line_len = one_line_bytes.length;
114 for(size=0; size < size_limit; size+=line_len) {
115 if( line_len != out.write( one_line_bytes, 0, line_len ) ) {
116 PrintUtil.fprintf_td(System.err,
"Write %d bytes to test file failed: %s", line_len, out.toString());
120 if( 1 != out.write( one_line_bytes, 0, 1 ) ) {
121 PrintUtil.fprintf_td(System.err,
"Write %d bytes to test file failed: %s", 1, out.toString());
128 fname_payload_lst.add(name);
129 fname_payload_copy_lst.add(name+
".copy");
130 fname_payload_size_lst.add( Long.valueOf(size) );
135 PlatformRuntime.checkInitialized();
136 Assert.assertTrue( add_test_file(
"test_cipher_01_11kiB.bin", 1024*11) );
137 Assert.assertTrue( add_test_file(
"test_cipher_02_65MiB.bin", 1024*1024*65) );
140 static boolean system(
final String[] command) {
143 proc = Runtime.getRuntime().exec(command);
147 catch(
final Exception ex)
152 ex.printStackTrace();
157 static final String mini_httpd_exe() {
158 final String os_name = System.getProperty(
"os.name");
159 if(
"FreeBSD".equals(os_name) ) {
160 return "/usr/local/sbin/mini_httpd";
162 return "/usr/sbin/mini_httpd";
168 if( UriTk.protocol_supported(
"http:") ) {
169 Assert.assertTrue( system(
new String[]{
"killall",
"mini_httpd"}) );
173 static void httpd_start() {
174 if( UriTk.protocol_supported(
"http:") ) {
175 Assert.assertTrue( system(
new String[]{
"killall",
"mini_httpd"}) );
176 final Path path = Paths.get(
"");
177 final String directoryName = path.toAbsolutePath().toString();
178 final String[] cmd =
new String[]{mini_httpd_exe(),
"-p",
"8080",
"-l", directoryName+
"/mini_httpd.log"};
179 PrintUtil.fprintf_td(System.err,
"%s%n", Arrays.toString(cmd));
180 Assert.assertTrue( system(cmd) );
184 final static String url_input_root =
"http://localhost:8080/";
187 static boolean transfer_std(
final ByteInStream input,
final String output_fname,
final int buffer_size) {
188 final long _t0 = org.jau.sys.Clock.currentTimeMillis();
189 PrintUtil.fprintf_td(System.err,
"Transfer Start: %s%n", input);
190 remove_file(output_fname);
192 if( file_exists( output_fname ) ) {
195 final long[] out_bytes_payload = { 0 };
196 try ( ByteOutStream_File out =
new ByteOutStream_File(output_fname, FMode.def_file) ) {
198 Assert.assertTrue( out.good() );
200 final ByteInStreamUtil.StreamConsumer1 consumer = (
final byte[] data,
final int data_len,
final boolean is_final) -> {
201 if( !is_final && ( !input.has_content_size() || out_bytes_payload[0] + data_len < input.content_size() ) ) {
202 final int written = out.write( data, 0, data_len );
203 out_bytes_payload[0] += written;
204 return data_len == written;
206 final int written = out.write( data, 0, data_len );
207 out_bytes_payload[0] += written;
211 final byte[] io_buffer =
new byte[buffer_size];
212 final long in_bytes_total = ByteInStreamUtil.read_stream(input, io_buffer, consumer);
216 if ( 0==in_bytes_total || input.fail() ) {
217 PrintUtil.fprintf_td(System.err,
"ByteStream copy failed: Input file read failed in %s, out %s%n", input, out);
221 PrintUtil.fprintf_td(System.err,
"ByteStream copy failed: Output file write failed in %s, out %s%n", input, out);
226 final long _td = org.jau.sys.Clock.currentTimeMillis() - _t0;
227 ByteInStreamUtil.print_stats(
"Transfer "+output_fname, out_bytes_payload[0], _td);
228 PrintUtil.fprintf_td(System.err,
"Transfer End: %s%n", input.toString());
233 static boolean transfer_nio(
final ByteInStream input,
final String output_fname,
final int buffer_size) {
234 final long _t0 = org.jau.sys.Clock.currentTimeMillis();
235 PrintUtil.fprintf_td(System.err,
"Transfer Start: %s%n", input);
236 remove_file(output_fname);
238 if( file_exists( output_fname ) ) {
241 final long[] out_bytes_payload = { 0 };
242 try ( ByteOutStream_File out =
new ByteOutStream_File(output_fname, FMode.def_file) ) {
243 Assert.assertTrue( out.good() );
245 final ByteInStreamUtil.StreamConsumer2 consumer = (
final ByteBuffer data,
final boolean is_final) -> {
246 final int data_len = data.remaining();
247 if( !is_final && ( !input.has_content_size() || out_bytes_payload[0] + data_len < input.content_size() ) ) {
248 final int written = out.write(data);
250 out_bytes_payload[0] += written;
251 return written == data_len;
253 final int written = out.write(data);
255 out_bytes_payload[0] += written;
259 final ByteBuffer io_buffer = Buffers.newDirectByteBuffer(buffer_size);
260 final long in_bytes_total = ByteInStreamUtil.read_stream(input, io_buffer, consumer);
264 if ( 0==in_bytes_total || input.fail() ) {
265 PrintUtil.fprintf_td(System.err,
"ByteStream copy failed: Input file read failed in %s, out %s%n", input, out);
269 PrintUtil.fprintf_td(System.err,
"ByteStream copy failed: Output file write failed in %s, out %s%n", input, out);
274 final long _td = org.jau.sys.Clock.currentTimeMillis() - _t0;
275 ByteInStreamUtil.print_stats(
"Transfer "+output_fname, out_bytes_payload[0], _td);
276 PrintUtil.fprintf_td(System.err,
"Transfer End: %s%n", input.toString());
281 @Test(timeout = 10000)
282 public final
void test00a_protocols_error() {
283 PlatformRuntime.checkInitialized();
284 final boolean http_support_expected = UriTk.protocol_supported(
"http:");
285 final boolean file_support_expected = UriTk.protocol_supported(
"file:");
288 final List<String> protos = UriTk.supported_protocols();
289 PrintUtil.fprintf_td(System.err,
"test00_protocols: Supported protocols: %d: %s%n", protos.size(), protos);
290 if( http_support_expected ) {
291 Assert.assertTrue( 0 < protos.size() );
293 Assert.assertTrue( 0 == protos.size() );
296 final int file_idx = IDX_11kiB;
298 final String url =
"not_exiting_file.txt";
299 Assert.assertFalse( UriTk.is_local_file_protocol(url) );
300 Assert.assertFalse( UriTk.protocol_supported(url) );
302 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
304 PrintUtil.fprintf_td(System.err,
"test00_protocols: not_exiting_file: %s%n", in);
306 Assert.assertNull(in);
310 final String url =
"file://not_exiting_file_uri.txt";
311 Assert.assertTrue( UriTk.is_local_file_protocol(url) );
312 Assert.assertEquals( file_support_expected, UriTk.protocol_supported(url) );
314 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
316 PrintUtil.fprintf_td(System.err,
"test00_protocols: not_exiting_file_uri: %s%n", in);
318 Assert.assertNull(in);
322 final String url =
"lala://localhost:8080/" + fname_payload_lst.get(file_idx);
323 Assert.assertFalse( UriTk.is_local_file_protocol(url) );
324 Assert.assertFalse( UriTk.protocol_supported(url) );
326 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
328 PrintUtil.fprintf_td(System.err,
"test00_protocols: not_exiting_protocol_uri: %s%n", in);
330 Assert.assertNull(in);
334 final String url = url_input_root +
"not_exiting_http_uri.txt";
335 Assert.assertFalse( UriTk.is_local_file_protocol(url) );
336 Assert.assertEquals( http_support_expected, UriTk.protocol_supported(url) );
338 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
339 if( http_support_expected ) {
340 Assert.assertNotNull(in);
341 try { Thread.sleep(100); }
catch (
final Throwable t) {}
342 PrintUtil.fprintf_td(System.err,
"test00_protocols: not_exiting_http_uri: %s%n", in);
343 Assert.assertFalse( in.good() );
344 Assert.assertTrue( in.fail() );
345 Assert.assertEquals( 0, in.content_size() );
347 Assert.assertNull(in);
353 @Test(timeout = 10000)
354 public final
void test00b_protocols_ok() {
355 PlatformRuntime.checkInitialized();
356 final boolean http_support_expected = UriTk.protocol_supported(
"http:");
357 final boolean file_support_expected = UriTk.protocol_supported(
"file:");
359 final int file_idx = IDX_11kiB;
361 final String url = fname_payload_lst.get(file_idx);
362 Assert.assertFalse( UriTk.is_local_file_protocol(url) );
363 Assert.assertFalse( UriTk.protocol_supported(url) );
365 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
367 PrintUtil.fprintf_td(System.err,
"test00_protocols: local-file-0: %s%n", in);
369 Assert.assertNotEquals(
null, in );
370 Assert.assertFalse( in.fail() );
372 final boolean res = transfer_nio(in, fname_payload_copy_lst.get(file_idx), 4096);
373 Assert.assertTrue( res );
375 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
376 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
377 Assert.assertEquals( in.content_size(), copy_size );
378 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
379 Assert.assertTrue( FileUtil.compare(in.id(), fname_payload_copy_lst.get(file_idx),
true ) );
383 final String url =
"file://" + fname_payload_lst.get(file_idx);
384 Assert.assertTrue( UriTk.is_local_file_protocol(url) );
385 Assert.assertEquals( file_support_expected, UriTk.protocol_supported(url) );
387 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
389 PrintUtil.fprintf_td(System.err,
"test00_protocols: local-file-1: %s%n", in);
391 Assert.assertNotNull( in );
392 Assert.assertFalse( in.fail() );
394 final boolean res = transfer_nio(in, fname_payload_copy_lst.get(file_idx), 4096);
395 Assert.assertTrue( res );
397 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
398 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
399 Assert.assertEquals( in.content_size(), copy_size );
400 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
401 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
405 final String url = url_input_root + fname_payload_lst.get(file_idx);
406 Assert.assertFalse( UriTk.is_local_file_protocol(url) );
407 Assert.assertEquals( http_support_expected, UriTk.protocol_supported(url) );
409 try(
final ByteInStream in = ByteInStreamUtil.to_ByteInStream(url) ) {
411 PrintUtil.fprintf_td(System.err,
"test00_protocols: http: %s%n", in);
413 if( http_support_expected ) {
414 Assert.assertNotNull( in );
415 Assert.assertFalse( in.fail() );
417 final boolean res = transfer_nio(in, fname_payload_copy_lst.get(file_idx), 4096);
418 Assert.assertTrue( res );
420 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
421 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
422 Assert.assertEquals( in.content_size(), copy_size );
423 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
424 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
426 Assert.assertNull( in );
432 @Test(timeout = 10000)
433 public
void test01_copy_file_ok_11kiB() {
434 PlatformRuntime.checkInitialized();
435 final int file_idx = IDX_11kiB;
436 try(
final ByteInStream_File data_stream =
new ByteInStream_File(fname_payload_lst.get(file_idx)) ) {
437 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 4096);
438 Assert.assertTrue( res );
440 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
441 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
442 Assert.assertEquals( data_stream.content_size(), copy_size );
443 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
444 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
448 @Test(timeout = 10000)
449 public
void test02_copy_file_ok_65MiB_nio_buff4k() {
450 PlatformRuntime.checkInitialized();
451 final int file_idx = IDX_65MiB;
452 try(
final ByteInStream_File data_stream =
new ByteInStream_File(fname_payload_lst.get(file_idx)) ) {
453 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 4096);
454 Assert.assertTrue( res );
456 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
457 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
458 Assert.assertEquals( data_stream.content_size(), copy_size );
459 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
460 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
464 @Test(timeout = 10000)
465 public
void test03_copy_file_ok_65MiB_std_buff4k() {
466 PlatformRuntime.checkInitialized();
467 final int file_idx = IDX_65MiB;
468 try(
final ByteInStream_File data_stream =
new ByteInStream_File(fname_payload_lst.get(file_idx)) ) {
469 final boolean res = transfer_std(data_stream, fname_payload_copy_lst.get(file_idx), 4096);
470 Assert.assertTrue( res );
472 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
473 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
474 Assert.assertEquals( data_stream.content_size(), copy_size );
475 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
476 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
480 @Test(timeout = 10000)
481 public
void test04_copy_file_ok_65MiB_nio_buff32k() {
482 PlatformRuntime.checkInitialized();
483 final int file_idx = IDX_65MiB;
484 try(
final ByteInStream_File data_stream =
new ByteInStream_File(fname_payload_lst.get(file_idx)) ) {
485 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 32768);
486 Assert.assertTrue( res );
488 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
489 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
490 Assert.assertEquals( data_stream.content_size(), copy_size );
491 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
492 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
496 @Test(timeout = 10000)
497 public
void test05_copy_file_ok_65MiB_std_buff32k() {
498 PlatformRuntime.checkInitialized();
499 final int file_idx = IDX_65MiB;
500 try(
final ByteInStream_File data_stream =
new ByteInStream_File(fname_payload_lst.get(file_idx)) ) {
501 final boolean res = transfer_std(data_stream, fname_payload_copy_lst.get(file_idx), 32768);
502 Assert.assertTrue( res );
504 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
505 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
506 Assert.assertEquals( data_stream.content_size(), copy_size );
507 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
508 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
512 @Test(timeout = 10000)
513 public
void test11_copy_http_ok_nio_buff32k() {
514 PlatformRuntime.checkInitialized();
515 if( !UriTk.protocol_supported(
"http:") ) {
516 PrintUtil.fprintf_td(System.err,
"http not supported, abort%n");
521 final int file_idx = IDX_11kiB;
523 final String uri_original = url_input_root + fname_payload_lst.get(file_idx);
525 try(
final ByteInStream_URL data_stream =
new ByteInStream_URL(uri_original, 500) ) {
526 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 32768);
527 Assert.assertTrue( res );
529 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
530 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
531 Assert.assertEquals( data_stream.content_size(), copy_size );
532 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
533 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
537 final int file_idx = IDX_65MiB;
539 final String uri_original = url_input_root + fname_payload_lst.get(file_idx);
541 try(
final ByteInStream_URL data_stream =
new ByteInStream_URL(uri_original, 500) ) {
542 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 32768);
543 Assert.assertTrue( res );
545 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
546 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
547 Assert.assertEquals( data_stream.content_size(), copy_size );
548 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
549 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
554 @Test(timeout = 10000)
555 public
void test12_copy_http_404() {
556 PlatformRuntime.checkInitialized();
557 if( !UriTk.protocol_supported(
"http:") ) {
558 PrintUtil.fprintf_td(System.err,
"http not supported, abort%n");
563 final int file_idx = IDX_11kiB;
565 final String uri_original = url_input_root +
"doesnt_exists.txt";
567 try(
final ByteInStream_URL data_stream =
new ByteInStream_URL(uri_original, 500) ) {
568 final boolean res = transfer_nio(data_stream, fname_payload_copy_lst.get(file_idx), 4096);
569 Assert.assertFalse( res );
571 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
572 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
573 Assert.assertTrue( data_stream.fail() );
574 Assert.assertFalse( data_stream.has_content_size() );
575 Assert.assertEquals( data_stream.content_size(), 0 );
576 Assert.assertEquals( 0, copy_size );
581 static Thread executeOffThread(
final Runnable runobj,
final String threadName,
final boolean detach) {
582 final Thread t =
new Thread( runobj, threadName );
583 t.setDaemon( detach );
589 static void feed_source_00(
final ByteInStream_Feed data_feed,
final int feed_size) {
591 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
592 final byte buffer[] =
new byte[feed_size];
593 while( data_stream.good() ) {
594 final int count = data_stream.read(buffer, 0, buffer.length);
597 if( data_feed.write(buffer, 0, count) ) {
598 try { Thread.sleep(16); }
catch (
final Throwable t) {}
605 data_feed.set_eof( data_feed.fail() ? -1 : 1 );
610 static void feed_source_01(
final ByteInStream_Feed data_feed,
final int feed_size) {
612 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
613 final long file_size = data_stream.content_size();
614 data_feed.set_content_size( file_size );
615 final byte buffer[] =
new byte[feed_size];
616 while( data_stream.good() && xfer_total < file_size ) {
617 final int count = data_stream.read(buffer, 0, buffer.length);
620 if( data_feed.write(buffer, 0, count) ) {
621 try { Thread.sleep(16); }
catch (
final Throwable t) {}
628 data_feed.set_eof( !data_feed.fail() && xfer_total == file_size ? 1 : -1 );
633 static void feed_source_10_nio(
final ByteInStream_Feed data_feed,
final int feed_size) {
635 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
636 final long file_size = data_stream.content_size();
637 data_feed.set_content_size( file_size );
638 final ByteBuffer buffer = Buffers.newDirectByteBuffer(feed_size);
639 while( data_stream.good() && xfer_total < file_size ) {
640 final int count = data_stream.read(buffer);
643 if( !data_feed.write(buffer) ) {
648 data_feed.set_eof( !data_feed.fail() && xfer_total == file_size ? 1 : -1 );
653 static void feed_source_10_std(
final ByteInStream_Feed data_feed,
final int feed_size) {
655 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
656 final long file_size = data_stream.content_size();
657 data_feed.set_content_size( file_size );
658 final byte buffer[] =
new byte[feed_size];
659 while( data_stream.good() && xfer_total < file_size ) {
660 final int count = data_stream.read(buffer, 0, buffer.length);
663 if( !data_feed.write(buffer, 0, count) ) {
668 data_feed.set_eof( !data_feed.fail() && xfer_total == file_size ? 1 : -1 );
673 static void feed_source_20(
final ByteInStream_Feed data_feed,
final int feed_size) {
675 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
676 final byte buffer[] =
new byte[feed_size];
677 while( data_stream.good() ) {
678 final int count = data_stream.read(buffer, 0, buffer.length);
681 if( data_feed.write(buffer, 0, count) ) {
682 if( xfer_total >= 1024 ) {
683 data_feed.set_eof( -1 );
697 static void feed_source_21(
final ByteInStream_Feed data_feed,
final int feed_size) {
699 try(
final ByteInStream_File data_stream =
new ByteInStream_File(data_feed.id() ) ) {
700 final long file_size = data_stream.content_size();
701 data_feed.set_content_size( file_size );
702 final byte buffer[] =
new byte[feed_size];
703 while( data_stream.good() ) {
704 final int count = data_stream.read(buffer, 0, buffer.length);
707 if( data_feed.write(buffer, 0, count) ) {
708 if( xfer_total >= file_size/4 ) {
709 data_feed.set_eof( -1 );
722 @Test(timeout = 10000)
723 public
void test20_copy_fed_ok_buff4k_feed1k() {
724 PlatformRuntime.checkInitialized();
725 final int buffer_size = 4096;
726 final int feed_size = 1024;
728 final int file_idx = IDX_11kiB;
731 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
732 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_nio(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_10",
false );
734 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
736 feeder_thread.join(1000);
737 }
catch (
final InterruptedException e) { }
738 Assert.assertTrue( res );
740 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
741 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
742 Assert.assertEquals( data_feed.content_size(), copy_size );
743 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
744 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
748 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
749 final Thread feeder_thread = executeOffThread( () -> { feed_source_01(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_01",
false );
751 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
753 feeder_thread.join(1000);
754 }
catch (
final InterruptedException e) { }
755 Assert.assertTrue( res );
757 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
758 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
759 Assert.assertEquals( data_feed.content_size(), copy_size );
760 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
761 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
765 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
766 final Thread feeder_thread = executeOffThread( () -> { feed_source_00(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_00",
false );
768 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
770 feeder_thread.join(1000);
771 }
catch (
final InterruptedException e) { }
772 Assert.assertTrue( res );
774 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
775 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
776 Assert.assertEquals( data_feed.content_size(), 0 );
777 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
778 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
782 final int file_idx = IDX_65MiB;
785 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
786 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_nio(data_feed, feed_size); },
"test21_copy_fed_ok2::feed_source_10",
false );
788 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
790 feeder_thread.join(1000);
791 }
catch (
final InterruptedException e) { }
792 Assert.assertTrue( res );
794 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
795 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
796 Assert.assertEquals( data_feed.content_size(), copy_size );
797 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
798 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
803 @Test(timeout = 10000)
804 public
void test21_copy_fed_ok_buff32k() {
805 PlatformRuntime.checkInitialized();
806 final int buffer_size = 32768;
807 final int feed_size = 32768;
809 final int file_idx = IDX_11kiB;
812 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
813 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_nio(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_10",
false );
815 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
817 feeder_thread.join(1000);
818 }
catch (
final InterruptedException e) { }
819 Assert.assertTrue( res );
821 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
822 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
823 Assert.assertEquals( data_feed.content_size(), copy_size );
824 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
825 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
829 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
830 final Thread feeder_thread = executeOffThread( () -> { feed_source_01(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_01",
false );
832 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
834 feeder_thread.join(1000);
835 }
catch (
final InterruptedException e) { }
836 Assert.assertTrue( res );
838 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
839 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
840 Assert.assertEquals( data_feed.content_size(), copy_size );
841 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
842 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
846 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
847 final Thread feeder_thread = executeOffThread( () -> { feed_source_00(data_feed, feed_size); },
"test21_copy_fed_ok::feed_source_00",
false );
849 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
851 feeder_thread.join(1000);
852 }
catch (
final InterruptedException e) { }
853 Assert.assertTrue( res );
855 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
856 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
857 Assert.assertEquals( data_feed.content_size(), 0 );
858 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
859 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
864 @Test(timeout = 10000)
865 public
void test22_copy_fed_ok_buff32k_nio() {
866 PlatformRuntime.checkInitialized();
867 final int buffer_size = 32768;
868 final int feed_size = 32768;
870 final int file_idx = IDX_65MiB;
873 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
874 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_nio(data_feed, feed_size); },
"test21_copy_fed_ok2::feed_source_10",
false );
876 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
878 feeder_thread.join(1000);
879 }
catch (
final InterruptedException e) { }
880 Assert.assertTrue( res );
882 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
883 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
884 Assert.assertEquals( data_feed.content_size(), copy_size );
885 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
886 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
891 @Test(timeout = 10000)
892 public
void test23_copy_fed_ok_buff32k_std() {
893 PlatformRuntime.checkInitialized();
894 final int buffer_size = 32768;
895 final int feed_size = 32768;
897 final int file_idx = IDX_65MiB;
900 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
901 final Thread feeder_thread = executeOffThread( () -> { feed_source_10_std(data_feed, feed_size); },
"test23_copy_fed_ok2::feed_source_10",
false );
903 final boolean res = transfer_std(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
905 feeder_thread.join(1000);
906 }
catch (
final InterruptedException e) { }
907 Assert.assertTrue( res );
909 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
910 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
911 Assert.assertEquals( data_feed.content_size(), copy_size );
912 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), copy_size );
913 Assert.assertTrue( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
918 @Test(timeout = 10000)
919 public
void test24_copy_fed_irq() {
920 PlatformRuntime.checkInitialized();
921 final int buffer_size = 4096;
922 final int feed_size = 1024;
924 final int file_idx = IDX_65MiB;
927 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
928 final Thread feeder_thread = executeOffThread( () -> { feed_source_20(data_feed, feed_size); },
"test22_copy_fed_irq::feed_source_20",
false );
930 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
932 feeder_thread.join(1000);
933 }
catch (
final InterruptedException e) { }
934 Assert.assertFalse( res );
936 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
937 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
938 Assert.assertFalse( data_feed.has_content_size() );
939 Assert.assertEquals( data_feed.content_size(), 0 );
940 Assert.assertTrue( fname_payload_size_lst.get(file_idx).longValue() > copy_size );
941 Assert.assertFalse( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
945 try(
final ByteInStream_Feed data_feed =
new ByteInStream_Feed(fname_payload_lst.get(file_idx), 500) ) {
946 final Thread feeder_thread = executeOffThread( () -> { feed_source_21(data_feed, feed_size); },
"test22_copy_fed_irq::feed_source_21",
false );
948 final boolean res = transfer_nio(data_feed, fname_payload_copy_lst.get(file_idx), buffer_size);
950 feeder_thread.join(1000);
951 }
catch (
final InterruptedException e) { }
952 Assert.assertFalse( res );
954 Assert.assertTrue( file_exists( fname_payload_copy_lst.get(file_idx) ) );
955 final long copy_size = file_size(fname_payload_copy_lst.get(file_idx));
956 Assert.assertTrue( data_feed.has_content_size() );
957 Assert.assertEquals( fname_payload_size_lst.get(file_idx).longValue(), data_feed.content_size() );
958 Assert.assertTrue( data_feed.content_size() > copy_size );
959 Assert.assertFalse( FileUtil.compare(fname_payload_lst.get(file_idx), fname_payload_copy_lst.get(file_idx),
true ) );
964 public static void main(
final String args[]) {
static void main(final String args[])