1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.tests.java.nio.channels; 18 19import java.io.File; 20import java.io.FileInputStream; 21import java.io.FileNotFoundException; 22import java.io.FileOutputStream; 23import java.io.IOException; 24import java.io.RandomAccessFile; 25import java.io.UnsupportedEncodingException; 26import java.net.InetAddress; 27import java.net.InetSocketAddress; 28import java.nio.BufferOverflowException; 29import java.nio.ByteBuffer; 30import java.nio.MappedByteBuffer; 31import java.nio.ReadOnlyBufferException; 32import java.nio.channels.ClosedChannelException; 33import java.nio.channels.DatagramChannel; 34import java.nio.channels.FileChannel; 35import java.nio.channels.FileLock; 36import java.nio.channels.NonReadableChannelException; 37import java.nio.channels.NonWritableChannelException; 38import java.nio.channels.OverlappingFileLockException; 39import java.nio.channels.Pipe; 40import java.nio.channels.ReadableByteChannel; 41import java.nio.channels.ServerSocketChannel; 42import java.nio.channels.SocketChannel; 43import java.nio.channels.WritableByteChannel; 44import java.nio.channels.FileChannel.MapMode; 45import java.util.Arrays; 46 47import junit.framework.TestCase; 48 49import libcore.io.IoUtils; 50 51public class FileChannelTest extends TestCase { 52 53 private static final int CAPACITY = 100; 54 55 private static final int LIMITED_CAPACITY = 2; 56 57 private static final int TIME_OUT = 10000; 58 59 private static final String CONTENT = "MYTESTSTRING needs to be a little long"; 60 61 private static final byte[] TEST_BYTES; 62 63 private static final byte[] CONTENT_AS_BYTES; 64 65 private static final int CONTENT_AS_BYTES_LENGTH; 66 67 static { 68 try { 69 TEST_BYTES = "test".getBytes("iso8859-1"); 70 CONTENT_AS_BYTES = CONTENT.getBytes("iso8859-1"); 71 CONTENT_AS_BYTES_LENGTH = CONTENT_AS_BYTES.length; 72 } catch (UnsupportedEncodingException e) { 73 throw new Error(e); 74 } 75 } 76 77 private static final int CONTENT_LENGTH = CONTENT.length(); 78 79 private FileChannel readOnlyFileChannel; 80 81 private FileChannel readOnlyFileChannel2; 82 83 private FileChannel writeOnlyFileChannel; 84 85 private FileChannel writeOnlyFileChannel2; 86 87 private FileChannel readWriteFileChannel; 88 89 private File fileOfReadOnlyFileChannel; 90 91 private File fileOfWriteOnlyFileChannel; 92 93 private File fileOfReadWriteFileChannel; 94 95 private ReadableByteChannel readByteChannel; 96 97 private WritableByteChannel writableByteChannel; 98 99 private DatagramChannel datagramChannelSender; 100 101 private DatagramChannel datagramChannelReceiver; 102 103 private ServerSocketChannel serverSocketChannel; 104 105 private SocketChannel socketChannelSender; 106 107 private SocketChannel socketChannelReceiver; 108 109 private Pipe pipe; 110 111 // to read content from FileChannel 112 private FileInputStream fis; 113 114 private FileLock fileLock; 115 116 protected void setUp() throws Exception { 117 fileOfReadOnlyFileChannel = File.createTempFile( 118 "File_of_readOnlyFileChannel", "tmp"); 119 fileOfReadOnlyFileChannel.deleteOnExit(); 120 fileOfWriteOnlyFileChannel = File.createTempFile( 121 "File_of_writeOnlyFileChannel", "tmp"); 122 fileOfWriteOnlyFileChannel.deleteOnExit(); 123 fileOfReadWriteFileChannel = File.createTempFile( 124 "File_of_readWriteFileChannel", "tmp"); 125 fileOfReadWriteFileChannel.deleteOnExit(); 126 fis = null; 127 fileLock = null; 128 readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel) 129 .getChannel(); 130 readOnlyFileChannel2 = new FileInputStream(fileOfReadOnlyFileChannel) 131 .getChannel(); 132 writeOnlyFileChannel = new FileOutputStream(fileOfWriteOnlyFileChannel) 133 .getChannel(); 134 writeOnlyFileChannel2 = new FileOutputStream(fileOfWriteOnlyFileChannel) 135 .getChannel(); 136 readWriteFileChannel = new RandomAccessFile(fileOfReadWriteFileChannel, 137 "rw").getChannel(); 138 } 139 140 protected void tearDown() { 141 IoUtils.closeQuietly(readOnlyFileChannel); 142 IoUtils.closeQuietly(readOnlyFileChannel2); 143 IoUtils.closeQuietly(writeOnlyFileChannel); 144 IoUtils.closeQuietly(writeOnlyFileChannel2); 145 IoUtils.closeQuietly(readWriteFileChannel); 146 IoUtils.closeQuietly(fis); 147 148 if (null != fileLock) { 149 try { 150 fileLock.release(); 151 } catch (IOException e) { 152 // do nothing 153 } 154 } 155 156 if (null != fileOfReadOnlyFileChannel) { 157 fileOfReadOnlyFileChannel.delete(); 158 } 159 if (null != fileOfWriteOnlyFileChannel) { 160 fileOfWriteOnlyFileChannel.delete(); 161 } 162 if (null != fileOfReadWriteFileChannel) { 163 fileOfReadWriteFileChannel.delete(); 164 } 165 166 IoUtils.closeQuietly(datagramChannelSender); 167 IoUtils.closeQuietly(datagramChannelReceiver); 168 IoUtils.closeQuietly(serverSocketChannel); 169 IoUtils.closeQuietly(socketChannelSender); 170 IoUtils.closeQuietly(socketChannelReceiver); 171 if (null != pipe) { 172 IoUtils.closeQuietly(pipe.source()); 173 IoUtils.closeQuietly(pipe.sink()); 174 } 175 } 176 177 /** 178 * @tests java.nio.channels.FileChannel#force(boolean) 179 */ 180 public void test_forceJ() throws Exception { 181 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 182 writeOnlyFileChannel.write(writeBuffer); 183 writeOnlyFileChannel.force(true); 184 185 byte[] readBuffer = new byte[CONTENT_AS_BYTES_LENGTH]; 186 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 187 fis.read(readBuffer); 188 assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer)); 189 } 190 191 /** 192 * @tests java.nio.channels.FileChannel#force(boolean) 193 */ 194 public void test_forceJ_closed() throws Exception { 195 writeOnlyFileChannel.close(); 196 try { 197 writeOnlyFileChannel.force(true); 198 fail(); 199 } catch (ClosedChannelException expected) { 200 } 201 202 try { 203 writeOnlyFileChannel.force(false); 204 fail(); 205 } catch (ClosedChannelException expected) { 206 } 207 } 208 209 /** 210 * @tests java.nio.channels.FileChannel#force(boolean) 211 */ 212 public void test_forceJ_ReadOnlyChannel() throws Exception { 213 // force on a read only file channel has no effect. 214 readOnlyFileChannel.force(true); 215 readOnlyFileChannel.force(false); 216 } 217 218 /** 219 * @tests java.nio.channels.FileChannel#position() 220 */ 221 public void test_position_Init() throws Exception { 222 assertEquals(0, readOnlyFileChannel.position()); 223 assertEquals(0, writeOnlyFileChannel.position()); 224 assertEquals(0, readWriteFileChannel.position()); 225 } 226 227 /** 228 * @tests java.nio.channels.FileChannel#position() 229 */ 230 public void test_position_ReadOnly() throws Exception { 231 writeDataToFile(fileOfReadOnlyFileChannel); 232 233 assertEquals(0, readOnlyFileChannel.position()); 234 ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 235 readOnlyFileChannel.read(readBuffer); 236 assertEquals(CONTENT_LENGTH, readOnlyFileChannel.position()); 237 } 238 239 /** 240 * Initializes test file. 241 * 242 * @param file 243 * @throws FileNotFoundException 244 * @throws IOException 245 */ 246 private void writeDataToFile(File file) throws FileNotFoundException, 247 IOException { 248 FileOutputStream fos = new FileOutputStream(file); 249 try { 250 fos.write(CONTENT_AS_BYTES); 251 } finally { 252 fos.close(); 253 } 254 } 255 256 /** 257 * Initializes large test file. 258 * 259 * @param file the file to be written 260 * @param size the content size to be written 261 * @throws FileNotFoundException 262 * @throws IOException 263 */ 264 private void writeLargeDataToFile(File file, int size) throws FileNotFoundException, 265 IOException { 266 FileOutputStream fos = new FileOutputStream(file); 267 byte[] buf = new byte[size]; 268 269 try { 270 // we don't care about content - just need a particular file size 271 fos.write(buf); 272 } finally { 273 fos.close(); 274 } 275 } 276 277 /** 278 * @tests java.nio.channels.FileChannel#position() 279 */ 280 public void test_position_WriteOnly() throws Exception { 281 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 282 writeOnlyFileChannel.write(writeBuffer); 283 assertEquals(CONTENT_LENGTH, writeOnlyFileChannel.position()); 284 } 285 286 /** 287 * @tests java.nio.channels.FileChannel#position() 288 */ 289 public void test_position_ReadWrite() throws Exception { 290 writeDataToFile(fileOfReadWriteFileChannel); 291 292 assertEquals(0, readWriteFileChannel.position()); 293 ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 294 readWriteFileChannel.read(readBuffer); 295 assertEquals(CONTENT_LENGTH, readWriteFileChannel.position()); 296 297 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 298 readWriteFileChannel.write(writeBuffer); 299 assertEquals(CONTENT_LENGTH * 2, readWriteFileChannel.position()); 300 } 301 302 /** 303 * @tests java.nio.channels.FileChannel#position() 304 */ 305 public void test_position_Closed() throws Exception { 306 readOnlyFileChannel.close(); 307 try { 308 readOnlyFileChannel.position(); 309 fail("should throw ClosedChannelException"); 310 } catch (ClosedChannelException expected) { 311 } 312 313 writeOnlyFileChannel.close(); 314 try { 315 writeOnlyFileChannel.position(); 316 fail("should throw ClosedChannelException"); 317 } catch (ClosedChannelException expected) { 318 } 319 320 readWriteFileChannel.close(); 321 try { 322 readWriteFileChannel.position(); 323 fail("should throw ClosedChannelException"); 324 } catch (ClosedChannelException expected) { 325 } 326 } 327 328 /** 329 * @tests java.nio.channels.FileChannel#position(long) 330 */ 331 public void test_positionJ_Closed() throws Exception { 332 final long POSITION = 100; 333 334 readOnlyFileChannel.close(); 335 try { 336 readOnlyFileChannel.position(POSITION); 337 fail(); 338 } catch (ClosedChannelException expected) { 339 } 340 341 writeOnlyFileChannel.close(); 342 try { 343 writeOnlyFileChannel.position(POSITION); 344 fail(); 345 } catch (ClosedChannelException expected) { 346 } 347 348 readWriteFileChannel.close(); 349 try { 350 readWriteFileChannel.position(POSITION); 351 fail(); 352 } catch (ClosedChannelException expected) { 353 } 354 } 355 356 /** 357 * @tests java.nio.channels.FileChannel#position(long) 358 */ 359 public void test_positionJ_Negative() throws Exception { 360 final long NEGATIVE_POSITION = -1; 361 try { 362 readOnlyFileChannel.position(NEGATIVE_POSITION); 363 fail("should throw IllegalArgumentException"); 364 } catch (IllegalArgumentException e) { 365 // expected 366 } 367 368 try { 369 writeOnlyFileChannel.position(NEGATIVE_POSITION); 370 fail("should throw IllegalArgumentException"); 371 } catch (IllegalArgumentException e) { 372 // expected 373 } 374 375 try { 376 readWriteFileChannel.position(NEGATIVE_POSITION); 377 fail("should throw IllegalArgumentException"); 378 } catch (IllegalArgumentException e) { 379 // expected 380 } 381 } 382 383 /** 384 * @tests java.nio.channels.FileChannel#position(long) 385 */ 386 public void test_positionJ_ReadOnly() throws Exception { 387 writeDataToFile(fileOfReadOnlyFileChannel); 388 389 // set the position of the read only file channel to POSITION 390 final int POSITION = 4; 391 readOnlyFileChannel.position(POSITION); 392 393 // reads the content left to readBuffer through read only file channel 394 ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 395 int count = readOnlyFileChannel.read(readBuffer); 396 assertEquals(CONTENT_LENGTH - POSITION, count); 397 398 // asserts the content read is the part which stays beyond the POSITION 399 readBuffer.flip(); 400 int i = POSITION; 401 while (readBuffer.hasRemaining()) { 402 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 403 i++; 404 } 405 } 406 407 /** 408 * @tests java.nio.channels.FileChannel#position(long) 409 */ 410 public void test_positionJ_WriteOnly() throws Exception { 411 writeDataToFile(fileOfWriteOnlyFileChannel); 412 413 // init data to write 414 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 415 416 // set the position of the write only file channel to POSITION 417 final int POSITION = 4; 418 writeOnlyFileChannel.position(POSITION); 419 420 // writes to the write only file channel 421 writeOnlyFileChannel.write(writeBuffer); 422 // force to write out. 423 writeOnlyFileChannel.close(); 424 425 // gets the result of the write only file channel 426 byte[] result = new byte[POSITION + CONTENT_LENGTH]; 427 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 428 fis.read(result); 429 430 // constructs the expected result which has content[0... POSITION] plus 431 // content[0...length()] 432 byte[] expectedResult = new byte[POSITION + CONTENT_LENGTH]; 433 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION); 434 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION, 435 CONTENT_LENGTH); 436 437 // asserts result of the write only file channel same as expected 438 assertTrue(Arrays.equals(expectedResult, result)); 439 } 440 441 /** 442 * @tests java.nio.channels.FileChannel#size() 443 */ 444 public void test_size_Init() throws Exception { 445 assertEquals(0, readOnlyFileChannel.size()); 446 assertEquals(0, writeOnlyFileChannel.size()); 447 assertEquals(0, readWriteFileChannel.size()); 448 } 449 450 /** 451 * @tests java.nio.channels.FileChannel#size() 452 */ 453 public void test_size() throws Exception { 454 writeDataToFile(fileOfReadOnlyFileChannel); 455 assertEquals(fileOfReadOnlyFileChannel.length(), readOnlyFileChannel 456 .size()); 457 458 459 // REGRESSION test for read(ByteBuffer[], int, int) on special files 460 try { 461 FileChannel specialFile = 462 new FileInputStream("/dev/zero").getChannel(); 463 assertEquals(0, specialFile.size()); 464 ByteBuffer buf = ByteBuffer.allocate(8); 465 assertEquals(8, specialFile.read(buf)); 466 ByteBuffer[] bufs = { ByteBuffer.allocate(8) }; 467 assertEquals(8, specialFile.read(bufs, 0, 1)); 468 specialFile.close(); 469 } catch (FileNotFoundException e) { 470 // skip test if special file doesn't exist 471 } 472 } 473 474 /** 475 * @tests java.nio.channels.FileChannel#size() 476 */ 477 public void test_size_Closed() throws Exception { 478 readOnlyFileChannel.close(); 479 try { 480 readOnlyFileChannel.size(); 481 fail("should throw ClosedChannelException"); 482 } catch (ClosedChannelException e) { 483 // expected 484 } 485 486 writeOnlyFileChannel.close(); 487 try { 488 writeOnlyFileChannel.size(); 489 fail("should throw ClosedChannelException"); 490 } catch (ClosedChannelException e) { 491 // expected 492 } 493 494 readWriteFileChannel.close(); 495 try { 496 readWriteFileChannel.size(); 497 fail("should throw ClosedChannelException"); 498 } catch (ClosedChannelException e) { 499 // expected 500 } 501 } 502 503 /** 504 * @tests java.nio.channels.FileChannel#truncate(long) 505 */ 506 public void test_truncateJ_Closed() throws Exception { 507 readOnlyFileChannel.close(); 508 try { 509 readOnlyFileChannel.truncate(0); 510 fail("should throw ClosedChannelException"); 511 } catch (ClosedChannelException e) { 512 // expected 513 } 514 515 writeOnlyFileChannel.close(); 516 try { 517 writeOnlyFileChannel.truncate(0); 518 fail("should throw ClosedChannelException"); 519 } catch (ClosedChannelException e) { 520 // expected 521 } 522 523 readWriteFileChannel.close(); 524 try { 525 readWriteFileChannel.truncate(-1); 526 fail("should throw ClosedChannelException"); 527 } catch (ClosedChannelException e) { 528 // expected 529 } 530 } 531 532 /** 533 * @tests java.nio.channels.FileChannel#truncate(long) 534 */ 535 public void test_truncateJ_IllegalArgument() throws Exception { 536 // regression test for Harmony-941 537 try { 538 readOnlyFileChannel.truncate(-1); 539 fail("should throw IllegalArgumentException"); 540 } catch (IllegalArgumentException e) { 541 // expected 542 } 543 544 try { 545 writeOnlyFileChannel.truncate(-1); 546 fail("should throw IllegalArgumentException"); 547 } catch (IllegalArgumentException e) { 548 // expected 549 } 550 551 try { 552 readWriteFileChannel.truncate(-1); 553 fail("should throw IllegalArgumentException"); 554 } catch (IllegalArgumentException e) { 555 // expected 556 } 557 } 558 559 /** 560 * @tests java.nio.channels.FileChannel#truncate(long) 561 */ 562 public void test_truncateJ_ReadOnly() throws Exception { 563 writeDataToFile(fileOfReadOnlyFileChannel); 564 try { 565 readOnlyFileChannel.truncate(readOnlyFileChannel.size()); 566 fail("should throw NonWritableChannelException."); 567 } catch (NonWritableChannelException e) { 568 // expected 569 } 570 571 try { 572 readOnlyFileChannel.truncate(0); 573 fail("should throw NonWritableChannelException."); 574 } catch (NonWritableChannelException e) { 575 // expected 576 } 577 } 578 579 /** 580 * @tests java.nio.channels.FileChannel#truncate(long) 581 */ 582 public void test_truncateJ() throws Exception { 583 writeDataToFile(fileOfReadWriteFileChannel); 584 585 int truncateLength = CONTENT_LENGTH + 2; 586 assertEquals(readWriteFileChannel, readWriteFileChannel.truncate(truncateLength)); 587 assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length()); 588 589 truncateLength = CONTENT_LENGTH; 590 assertEquals(readWriteFileChannel, readWriteFileChannel 591 .truncate(truncateLength)); 592 assertEquals(CONTENT_LENGTH, fileOfReadWriteFileChannel.length()); 593 594 truncateLength = CONTENT_LENGTH / 2; 595 assertEquals(readWriteFileChannel, readWriteFileChannel 596 .truncate(truncateLength)); 597 assertEquals(truncateLength, fileOfReadWriteFileChannel.length()); 598 } 599 600 /** 601 * @tests java.nio.channels.FileChannel#lock() 602 */ 603 public void test_lock_Closed() throws Exception { 604 readOnlyFileChannel.close(); 605 try { 606 readOnlyFileChannel.lock(); 607 fail("should throw ClosedChannelException"); 608 } catch (ClosedChannelException expected) {} 609 610 writeOnlyFileChannel.close(); 611 try { 612 writeOnlyFileChannel.lock(); 613 fail("should throw ClosedChannelException"); 614 } catch (ClosedChannelException expected) {} 615 616 readWriteFileChannel.close(); 617 try { 618 readWriteFileChannel.lock(); 619 fail("should throw ClosedChannelException"); 620 } catch (ClosedChannelException expected) {} 621 } 622 623 /** 624 * @tests java.nio.channels.FileChannel#lock() 625 */ 626 public void test_lock_NonWritable() throws Exception { 627 try { 628 readOnlyFileChannel.lock(); 629 fail("should throw NonWritableChannelException"); 630 } catch (NonWritableChannelException expected) {} 631 } 632 633 /** 634 * @tests java.nio.channels.FileChannel#lock() 635 */ 636 public void test_lock() throws Exception { 637 fileLock = writeOnlyFileChannel.lock(); 638 assertTrue(fileLock.isValid()); 639 assertFalse(fileLock.isShared()); 640 assertSame(writeOnlyFileChannel, fileLock.channel()); 641 assertEquals(Long.MAX_VALUE, fileLock.size()); 642 assertEquals(0, fileLock.position()); 643 } 644 645 /** 646 * @tests java.nio.channels.FileChannel#lock() 647 */ 648 public void test_lock_OverlappingException() throws Exception { 649 fileLock = writeOnlyFileChannel.lock(); 650 assertTrue(fileLock.isValid()); 651 652 // Test the same channel cannot be locked twice. 653 try { 654 writeOnlyFileChannel.lock(); 655 fail("should throw OverlappingFileLockException"); 656 } catch (OverlappingFileLockException expected) {} 657 658 // Test that a different channel on the same file also cannot be locked. 659 try { 660 writeOnlyFileChannel2.lock(); 661 fail("should throw OverlappingFileLockException"); 662 } catch (OverlappingFileLockException expected) {} 663 } 664 665 /** 666 * @tests java.nio.channels.FileChannel#lock() 667 */ 668 public void test_lock_After_Release() throws Exception { 669 fileLock = writeOnlyFileChannel.lock(); 670 fileLock.release(); 671 // After release file lock can be obtained again. 672 fileLock = writeOnlyFileChannel.lock(); 673 assertTrue(fileLock.isValid()); 674 675 // A different channel should be able to obtain a lock after it has been released 676 fileLock.release(); 677 assertTrue(writeOnlyFileChannel2.lock().isValid()); 678 } 679 680 /** 681 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 682 */ 683 public void test_lockJJZ_Closed() throws Exception { 684 readOnlyFileChannel.close(); 685 try { 686 readOnlyFileChannel.lock(0, 10, false); 687 fail("should throw ClosedChannelException"); 688 } catch (ClosedChannelException e) { 689 // expected 690 } 691 692 writeOnlyFileChannel.close(); 693 try { 694 writeOnlyFileChannel.lock(0, 10, false); 695 fail("should throw ClosedChannelException"); 696 } catch (ClosedChannelException e) { 697 // expected 698 } 699 700 readWriteFileChannel.close(); 701 try { 702 readWriteFileChannel.lock(0, 10, false); 703 fail("should throw ClosedChannelException"); 704 } catch (ClosedChannelException e) { 705 // expected 706 } 707 708 // throws ClosedChannelException before IllegalArgumentException 709 try { 710 readWriteFileChannel.lock(-1, 0, false); 711 fail("should throw ClosedChannelException"); 712 } catch (ClosedChannelException e) { 713 // expected 714 } 715 } 716 717 /** 718 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 719 */ 720 public void test_lockJJZ_IllegalArgument() throws Exception { 721 try { 722 writeOnlyFileChannel.lock(0, -1, false); 723 fail("should throw IllegalArgumentException"); 724 } catch (IllegalArgumentException e) { 725 // expected 726 } 727 728 try { 729 writeOnlyFileChannel.lock(-1, 0, false); 730 fail("should throw IllegalArgumentException"); 731 } catch (IllegalArgumentException e) { 732 // expected 733 } 734 735 try { 736 readWriteFileChannel.lock(-1, -1, false); 737 fail("should throw IllegalArgumentException"); 738 } catch (IllegalArgumentException e) { 739 // expected 740 } 741 742 try { 743 readWriteFileChannel.lock(Long.MAX_VALUE, 1, false); 744 fail("should throw IllegalArgumentException"); 745 } catch (IllegalArgumentException e) { 746 // expected 747 } 748 } 749 750 /** 751 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 752 */ 753 public void test_lockJJZ_NonWritable() throws Exception { 754 try { 755 readOnlyFileChannel.lock(0, 10, false); 756 fail("should throw NonWritableChannelException"); 757 } catch (NonWritableChannelException e) { 758 // expected 759 } 760 761 // throws NonWritableChannelException before IllegalArgumentException 762 try { 763 readOnlyFileChannel.lock(-1, 0, false); 764 fail("should throw NonWritableChannelException"); 765 } catch (NonWritableChannelException e) { 766 // expected 767 } 768 } 769 770 /** 771 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 772 */ 773 public void test_lockJJZ_NonReadable() throws Exception { 774 try { 775 writeOnlyFileChannel.lock(0, 10, true); 776 fail("should throw NonReadableChannelException"); 777 } catch (NonReadableChannelException e) { 778 // expected 779 } 780 781 // throws NonReadableChannelException before IllegalArgumentException 782 try { 783 writeOnlyFileChannel.lock(-1, 0, true); 784 fail("should throw NonReadableChannelException"); 785 } catch (NonReadableChannelException e) { 786 // expected 787 } 788 } 789 790 /** 791 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 792 */ 793 public void test_lockJJZ_Shared() throws Exception { 794 final long POSITION = 100; 795 final long SIZE = 200; 796 fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true); 797 assertTrue(fileLock.isValid()); 798 // fileLock.isShared depends on whether the underlying platform support 799 // shared lock, but it works on Windows & Linux. 800 assertTrue(fileLock.isShared()); 801 assertSame(readOnlyFileChannel, fileLock.channel()); 802 assertEquals(POSITION, fileLock.position()); 803 assertEquals(SIZE, fileLock.size()); 804 } 805 806 /** 807 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 808 */ 809 public void test_lockJJZ_NotShared() throws Exception { 810 final long POSITION = 100; 811 final long SIZE = 200; 812 fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false); 813 assertTrue(fileLock.isValid()); 814 assertFalse(fileLock.isShared()); 815 assertSame(writeOnlyFileChannel, fileLock.channel()); 816 assertEquals(POSITION, fileLock.position()); 817 assertEquals(SIZE, fileLock.size()); 818 } 819 820 /** 821 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 822 */ 823 public void test_lockJJZ_Long_MAX_VALUE() throws Exception { 824 final long POSITION = 0; 825 final long SIZE = Long.MAX_VALUE; 826 fileLock = readOnlyFileChannel.lock(POSITION, SIZE, true); 827 assertTrue(fileLock.isValid()); 828 assertTrue(fileLock.isShared()); 829 assertEquals(POSITION, fileLock.position()); 830 assertEquals(SIZE, fileLock.size()); 831 assertSame(readOnlyFileChannel, fileLock.channel()); 832 } 833 834 /** 835 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 836 */ 837 public void test_lockJJZ_Overlapping() throws Exception { 838 final long POSITION = 100; 839 final long SIZE = 200; 840 fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false); 841 assertTrue(fileLock.isValid()); 842 843 // Test the same channel cannot be locked twice. 844 try { 845 writeOnlyFileChannel.lock(POSITION + 1, SIZE, false); 846 fail("should throw OverlappingFileLockException"); 847 } catch (OverlappingFileLockException expected) {} 848 849 // Test that a different channel on the same file also cannot be locked. 850 try { 851 writeOnlyFileChannel2.lock(POSITION + 1, SIZE, false); 852 fail("should throw OverlappingFileLockException"); 853 } catch (OverlappingFileLockException expected) {} 854 } 855 856 /** 857 * @tests java.nio.channels.FileChannel#lock(long, long, boolean) 858 */ 859 public void test_lockJJZ_NotOverlapping() throws Exception { 860 final long POSITION = 100; 861 final long SIZE = 200; 862 FileLock fileLock1 = writeOnlyFileChannel.lock(POSITION, SIZE, false); 863 assertTrue(fileLock1.isValid()); 864 FileLock fileLock2 = writeOnlyFileChannel.lock(POSITION + SIZE, SIZE, 865 false); 866 assertTrue(fileLock2.isValid()); 867 } 868 869 /** 870 * @tests java.nio.channels.FileChannel#lock(long,long,boolean) 871 */ 872 public void test_lockJJZ_After_Release() throws Exception { 873 fileLock = writeOnlyFileChannel.lock(0, 10, false); 874 fileLock.release(); 875 // after release file lock can be obtained again. 876 fileLock = writeOnlyFileChannel.lock(0, 10, false); 877 assertTrue(fileLock.isValid()); 878 } 879 880 /** 881 * @tests java.nio.channels.FileChannel#tryLock() 882 */ 883 public void test_tryLock_Closed() throws Exception { 884 readOnlyFileChannel.close(); 885 try { 886 readOnlyFileChannel.tryLock(); 887 fail("should throw ClosedChannelException"); 888 } catch (ClosedChannelException expected) {} 889 890 writeOnlyFileChannel.close(); 891 try { 892 writeOnlyFileChannel.tryLock(); 893 fail("should throw ClosedChannelException"); 894 } catch (ClosedChannelException expected) {} 895 896 readWriteFileChannel.close(); 897 try { 898 readWriteFileChannel.tryLock(); 899 fail("should throw ClosedChannelException"); 900 } catch (ClosedChannelException expected) {} 901 } 902 903 /** 904 * @tests java.nio.channels.FileChannel#tryLock() 905 */ 906 public void test_tryLock_NonWritable() throws Exception { 907 try { 908 readOnlyFileChannel.tryLock(); 909 fail("should throw NonWritableChannelException"); 910 } catch (NonWritableChannelException expected) {} 911 } 912 913 /** 914 * @tests java.nio.channels.FileChannel#tryLock() 915 */ 916 public void test_tryLock() throws Exception { 917 fileLock = writeOnlyFileChannel.tryLock(); 918 assertTrue(fileLock.isValid()); 919 assertFalse(fileLock.isShared()); 920 assertSame(writeOnlyFileChannel, fileLock.channel()); 921 assertEquals(0, fileLock.position()); 922 assertEquals(Long.MAX_VALUE, fileLock.size()); 923 } 924 925 /** 926 * @tests java.nio.channels.FileChannel#tryLock() 927 */ 928 public void test_tryLock_Overlapping() throws Exception { 929 fileLock = writeOnlyFileChannel.tryLock(); 930 assertTrue(fileLock.isValid()); 931 932 // Test the same channel cannot be locked twice. 933 try { 934 writeOnlyFileChannel.tryLock(); 935 fail("should throw OverlappingFileLockException"); 936 } catch (OverlappingFileLockException expected) {} 937 938 // Test that a different channel on the same file also cannot be locked. 939 try { 940 writeOnlyFileChannel2.tryLock(); 941 fail("should throw OverlappingFileLockException"); 942 } catch (OverlappingFileLockException expected) {} 943 } 944 945 /** 946 * @tests java.nio.channels.FileChannel#tryLock() 947 */ 948 public void test_tryLock_After_Release() throws Exception { 949 fileLock = writeOnlyFileChannel.tryLock(); 950 fileLock.release(); 951 952 // After release file lock can be obtained again. 953 fileLock = writeOnlyFileChannel.tryLock(); 954 assertTrue(fileLock.isValid()); 955 956 // Test that the same channel can acquire the lock after it has been released 957 fileLock.release(); 958 fileLock = writeOnlyFileChannel.tryLock(); 959 assertTrue(fileLock.isValid()); 960 961 // Test that a different channel can acquire the lock after it has been released 962 fileLock.release(); 963 fileLock = writeOnlyFileChannel2.tryLock(); 964 assertTrue(fileLock.isValid()); 965 } 966 967 /** 968 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 969 */ 970 public void test_tryLockJJZ_Closed() throws Exception { 971 readOnlyFileChannel.close(); 972 try { 973 readOnlyFileChannel.tryLock(0, 10, false); 974 fail("should throw ClosedChannelException"); 975 } catch (ClosedChannelException e) { 976 // expected 977 } 978 979 writeOnlyFileChannel.close(); 980 try { 981 writeOnlyFileChannel.tryLock(0, 10, false); 982 fail("should throw ClosedChannelException"); 983 } catch (ClosedChannelException e) { 984 // expected 985 } 986 987 readWriteFileChannel.close(); 988 try { 989 readWriteFileChannel.tryLock(0, 10, false); 990 fail("should throw ClosedChannelException"); 991 } catch (ClosedChannelException e) { 992 // expected 993 } 994 995 // throws ClosedChannelException before IllegalArgumentException 996 try { 997 readWriteFileChannel.tryLock(-1, 0, false); 998 fail("should throw ClosedChannelException"); 999 } catch (ClosedChannelException e) { 1000 // expected 1001 } 1002 } 1003 1004 /** 1005 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1006 */ 1007 public void test_tryLockJJZ_IllegalArgument() throws Exception { 1008 try { 1009 writeOnlyFileChannel.tryLock(0, -1, false); 1010 fail("should throw IllegalArgumentException"); 1011 } catch (IllegalArgumentException e) { 1012 // expected 1013 } 1014 1015 try { 1016 writeOnlyFileChannel.tryLock(-1, 0, false); 1017 fail("should throw IllegalArgumentException"); 1018 } catch (IllegalArgumentException e) { 1019 // expected 1020 } 1021 1022 try { 1023 readWriteFileChannel.tryLock(-1, -1, false); 1024 fail("should throw IllegalArgumentException"); 1025 } catch (IllegalArgumentException e) { 1026 // expected 1027 } 1028 1029 try { 1030 readWriteFileChannel.tryLock(Long.MAX_VALUE, 1, false); 1031 fail("should throw IllegalArgumentException"); 1032 } catch (IllegalArgumentException e) { 1033 // expected 1034 } 1035 } 1036 1037 /** 1038 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1039 */ 1040 public void test_tryLockJJZ_NonWritable() throws Exception { 1041 try { 1042 readOnlyFileChannel.tryLock(0, 10, false); 1043 fail("should throw NonWritableChannelException"); 1044 } catch (NonWritableChannelException e) { 1045 // expected 1046 } 1047 1048 // throws NonWritableChannelException before IllegalArgumentException 1049 try { 1050 readOnlyFileChannel.tryLock(-1, 0, false); 1051 fail("should throw NonWritableChannelException"); 1052 } catch (NonWritableChannelException e) { 1053 // expected 1054 } 1055 } 1056 1057 /** 1058 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1059 */ 1060 public void test_tryLockJJZ_NonReadable() throws Exception { 1061 try { 1062 writeOnlyFileChannel.tryLock(0, 10, true); 1063 fail("should throw NonReadableChannelException"); 1064 } catch (NonReadableChannelException e) { 1065 // expected 1066 } 1067 1068 // throws NonReadableChannelException before IllegalArgumentException 1069 try { 1070 writeOnlyFileChannel.tryLock(-1, 0, true); 1071 fail("should throw NonReadableChannelException"); 1072 } catch (NonReadableChannelException e) { 1073 // expected 1074 } 1075 } 1076 1077 /** 1078 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1079 */ 1080 public void test_tryLockJJZ_Shared() throws Exception { 1081 final long POSITION = 100; 1082 final long SIZE = 200; 1083 fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true); 1084 assertTrue(fileLock.isValid()); 1085 // fileLock.isShared depends on whether the underlying platform support 1086 // shared lock, but it works on Windows & Linux. 1087 assertTrue(fileLock.isShared()); 1088 assertSame(readOnlyFileChannel, fileLock.channel()); 1089 assertEquals(POSITION, fileLock.position()); 1090 assertEquals(SIZE, fileLock.size()); 1091 } 1092 1093 /** 1094 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1095 */ 1096 public void test_tryLockJJZ_NotShared() throws Exception { 1097 final long POSITION = 100; 1098 final long SIZE = 200; 1099 fileLock = writeOnlyFileChannel.tryLock(POSITION, SIZE, false); 1100 assertTrue(fileLock.isValid()); 1101 assertFalse(fileLock.isShared()); 1102 assertSame(writeOnlyFileChannel, fileLock.channel()); 1103 assertEquals(POSITION, fileLock.position()); 1104 assertEquals(SIZE, fileLock.size()); 1105 } 1106 1107 /** 1108 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1109 */ 1110 public void test_tryLockJJZ_Long_MAX_VALUE() throws Exception { 1111 final long POSITION = 0; 1112 final long SIZE = Long.MAX_VALUE; 1113 fileLock = readOnlyFileChannel.tryLock(POSITION, SIZE, true); 1114 assertTrue(fileLock.isValid()); 1115 assertTrue(fileLock.isShared()); 1116 assertEquals(POSITION, fileLock.position()); 1117 assertEquals(SIZE, fileLock.size()); 1118 assertSame(readOnlyFileChannel, fileLock.channel()); 1119 } 1120 1121 /** 1122 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1123 */ 1124 public void test_tryLockJJZ_Overlapping() throws Exception { 1125 final long POSITION = 100; 1126 final long SIZE = 200; 1127 fileLock = writeOnlyFileChannel.lock(POSITION, SIZE, false); 1128 assertTrue(fileLock.isValid()); 1129 1130 // Test the same channel cannot be locked twice. 1131 try { 1132 writeOnlyFileChannel.tryLock(POSITION + 1, SIZE, false); 1133 fail("should throw OverlappingFileLockException"); 1134 } catch (OverlappingFileLockException expected) {} 1135 1136 // Test that a different channel on the same file also cannot be locked. 1137 try { 1138 writeOnlyFileChannel2.tryLock(POSITION + 1, SIZE, false); 1139 fail("should throw OverlappingFileLockException"); 1140 } catch (OverlappingFileLockException expected) {} 1141 } 1142 1143 /** 1144 * @tests java.nio.channels.FileChannel#tryLock(long, long, boolean) 1145 */ 1146 public void test_tryLockJJZ_NotOverlapping() throws Exception { 1147 final long POSITION = 100; 1148 final long SIZE = 200; 1149 FileLock fileLock1 = writeOnlyFileChannel 1150 .tryLock(POSITION, SIZE, false); 1151 assertTrue(fileLock1.isValid()); 1152 1153 FileLock fileLock2 = writeOnlyFileChannel.tryLock(POSITION + SIZE, 1154 SIZE, false); 1155 assertTrue(fileLock2.isValid()); 1156 } 1157 1158 /** 1159 * @tests java.nio.channels.FileChannel#tryLock(long,long,boolean) 1160 */ 1161 public void test_tryLockJJZ_After_Release() throws Exception { 1162 fileLock = writeOnlyFileChannel.tryLock(0, 10, false); 1163 fileLock.release(); 1164 1165 // after release file lock can be obtained again. 1166 fileLock = writeOnlyFileChannel.tryLock(0, 10, false); 1167 assertTrue(fileLock.isValid()); 1168 } 1169 1170 /** 1171 * @tests java.nio.channels.FileChannel#read(ByteBuffer) 1172 */ 1173 public void test_readLByteBuffer_Null() throws Exception { 1174 ByteBuffer readBuffer = null; 1175 1176 try { 1177 readOnlyFileChannel.read(readBuffer); 1178 fail("should throw NullPointerException"); 1179 } catch (NullPointerException e) { 1180 // expected 1181 } 1182 1183 try { 1184 readWriteFileChannel.read(readBuffer); 1185 fail("should throw NullPointerException"); 1186 } catch (NullPointerException e) { 1187 // expected 1188 } 1189 } 1190 1191 /** 1192 * @tests java.nio.channels.FileChannel#read(ByteBuffer) 1193 */ 1194 public void test_readLByteBuffer_Closed() throws Exception { 1195 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1196 1197 readOnlyFileChannel.close(); 1198 try { 1199 readOnlyFileChannel.read(readBuffer); 1200 fail("should throw ClosedChannelException"); 1201 } catch (ClosedChannelException e) { 1202 // expected 1203 } 1204 1205 writeOnlyFileChannel.close(); 1206 try { 1207 writeOnlyFileChannel.read(readBuffer); 1208 fail("should throw ClosedChannelException"); 1209 } catch (ClosedChannelException e) { 1210 // expected 1211 } 1212 1213 readWriteFileChannel.close(); 1214 try { 1215 readWriteFileChannel.read(readBuffer); 1216 fail("should throw ClosedChannelException"); 1217 } catch (ClosedChannelException e) { 1218 // expected 1219 } 1220 1221 // should throw ClosedChannelException first 1222 readBuffer = null; 1223 try { 1224 readWriteFileChannel.read(readBuffer); 1225 fail(); 1226 } catch (ClosedChannelException expected) { 1227 } catch (NullPointerException expected) { 1228 } 1229 } 1230 1231 public void test_readLByteBuffer_WriteOnly() throws Exception { 1232 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1233 1234 try { 1235 writeOnlyFileChannel.read(readBuffer); 1236 fail(); 1237 } catch (NonReadableChannelException expected) { 1238 } 1239 1240 readBuffer = null; 1241 try { 1242 writeOnlyFileChannel.read(readBuffer); 1243 fail(); 1244 } catch (NonReadableChannelException expected) { 1245 } catch (NullPointerException expected) { 1246 } 1247 } 1248 1249 public void test_readLByteBuffer_EmptyFile() throws Exception { 1250 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1251 int result = readOnlyFileChannel.read(readBuffer); 1252 assertEquals(-1, result); 1253 assertEquals(0, readBuffer.position()); 1254 } 1255 1256 /** 1257 * @tests java.nio.channels.FileChannel#read(ByteBuffer) 1258 */ 1259 public void test_readLByteBuffer_LimitedCapacity() throws Exception { 1260 writeDataToFile(fileOfReadOnlyFileChannel); 1261 1262 ByteBuffer readBuffer = ByteBuffer.allocate(LIMITED_CAPACITY); 1263 int result = readOnlyFileChannel.read(readBuffer); 1264 assertEquals(LIMITED_CAPACITY, result); 1265 assertEquals(LIMITED_CAPACITY, readBuffer.position()); 1266 readBuffer.flip(); 1267 for (int i = 0; i < LIMITED_CAPACITY; i++) { 1268 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 1269 } 1270 } 1271 1272 public void test_readLByteBuffer() throws Exception { 1273 writeDataToFile(fileOfReadOnlyFileChannel); 1274 1275 ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH); 1276 int result = readOnlyFileChannel.read(readBuffer); 1277 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 1278 assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffer.position()); 1279 readBuffer.flip(); 1280 for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) { 1281 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 1282 } 1283 } 1284 1285 public void test_readLByteBufferJ_Null() throws Exception { 1286 try { 1287 readOnlyFileChannel.read(null, 0); 1288 fail(); 1289 } catch (NullPointerException expected) { 1290 } 1291 1292 try { 1293 readWriteFileChannel.read(null, 0); 1294 fail(); 1295 } catch (NullPointerException expected) { 1296 } 1297 } 1298 1299 public void test_readLByteBufferJ_Closed() throws Exception { 1300 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1301 1302 readOnlyFileChannel.close(); 1303 try { 1304 readOnlyFileChannel.read(readBuffer, 0); 1305 fail(); 1306 } catch (ClosedChannelException expected) { 1307 } 1308 1309 readWriteFileChannel.close(); 1310 try { 1311 readWriteFileChannel.read(readBuffer, 0); 1312 fail(); 1313 } catch (ClosedChannelException expected) { 1314 } 1315 } 1316 1317 public void test_readLByteBufferJ_IllegalArgument() throws Exception { 1318 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1319 1320 try { 1321 readOnlyFileChannel.read(readBuffer, -1); 1322 fail(); 1323 } catch (IllegalArgumentException expected) { 1324 } 1325 1326 try { 1327 writeOnlyFileChannel.read(readBuffer, -1); 1328 fail(); 1329 } catch (IllegalArgumentException expected) { 1330 } 1331 1332 try { 1333 readWriteFileChannel.read(readBuffer, -1); 1334 fail(); 1335 } catch (IllegalArgumentException expected) { 1336 } 1337 } 1338 1339 public void test_readLByteBufferJ_WriteOnly() throws Exception { 1340 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1341 1342 try { 1343 writeOnlyFileChannel.read(readBuffer, 0); 1344 fail(); 1345 } catch (NonReadableChannelException expected) { 1346 } 1347 } 1348 1349 public void test_readLByteBufferJ_Emptyfile() throws Exception { 1350 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1351 int result = readOnlyFileChannel.read(readBuffer, 0); 1352 assertEquals(-1, result); 1353 assertEquals(0, readBuffer.position()); 1354 } 1355 1356 public void test_readLByteBufferJ_Position_BeyondFileLimit() throws Exception { 1357 writeDataToFile(fileOfReadOnlyFileChannel); 1358 1359 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1360 int result = readOnlyFileChannel.read(readBuffer, 1361 CONTENT_AS_BYTES.length); 1362 assertEquals(-1, result); 1363 assertEquals(0, readBuffer.position()); 1364 } 1365 1366 public void test_readLByteBufferJ_Position_As_Long() throws Exception { 1367 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1368 try { 1369 readOnlyFileChannel.read(readBuffer, Long.MAX_VALUE); 1370 } catch (IOException expected) { 1371 } 1372 } 1373 1374 public void test_readLByteBufferJ() throws Exception { 1375 writeDataToFile(fileOfReadOnlyFileChannel); 1376 ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY); 1377 1378 final int BUFFER_POSITION = 1; 1379 readBuffer.position(BUFFER_POSITION); 1380 1381 final int POSITION = 2; 1382 int result = readOnlyFileChannel.read(readBuffer, POSITION); 1383 assertEquals(CONTENT_AS_BYTES_LENGTH - POSITION, result); 1384 assertEquals(BUFFER_POSITION + result, readBuffer.position()); 1385 1386 readBuffer.flip(); 1387 readBuffer.position(BUFFER_POSITION); 1388 for (int i = POSITION; i < CONTENT_AS_BYTES_LENGTH; i++) { 1389 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 1390 } 1391 } 1392 1393 /** 1394 * @tests java.nio.channels.FileChannel#read(ByteBuffer[]) 1395 */ 1396 public void test_read$LByteBuffer() throws Exception { 1397 // regression test for Harmony-849 1398 writeDataToFile(fileOfReadOnlyFileChannel); 1399 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1400 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1401 readBuffers[1] = ByteBuffer.allocate(CAPACITY); 1402 1403 long readCount = readOnlyFileChannel.read(readBuffers); 1404 assertEquals(CONTENT_AS_BYTES_LENGTH, readCount); 1405 assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[0].position()); 1406 assertEquals(0, readBuffers[1].position()); 1407 readBuffers[0].flip(); 1408 for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) { 1409 assertEquals(CONTENT_AS_BYTES[i], readBuffers[0].get()); 1410 } 1411 } 1412 1413 /** 1414 * @tests java.nio.channels.FileChannel#read(ByteBuffer[]) 1415 */ 1416 public void test_read$LByteBuffer_mock() throws Exception { 1417 FileChannel mockChannel = new MockFileChannel(); 1418 ByteBuffer[] buffers = new ByteBuffer[2]; 1419 mockChannel.read(buffers); 1420 // Verify that calling read(ByteBuffer[] dsts) leads to the method 1421 // read(dsts, 0, dsts.length) 1422 assertTrue(((MockFileChannel)mockChannel).isReadCalled); 1423 } 1424 1425 /** 1426 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1427 */ 1428 public void test_read$LByteBufferII_Null() throws Exception { 1429 ByteBuffer[] readBuffers = null; 1430 1431 try { 1432 readOnlyFileChannel.read(readBuffers, 0, 1); 1433 fail("should throw NullPointerException"); 1434 } catch (NullPointerException e) { 1435 // expected 1436 } 1437 1438 try { 1439 readOnlyFileChannel.read(readBuffers, 1, 11); 1440 fail("should throw NullPointerException"); 1441 } catch (NullPointerException e) { 1442 // expected 1443 } 1444 1445 try { 1446 writeOnlyFileChannel.read(readBuffers, 0, 1); 1447 fail("should throw NullPointerException"); 1448 } catch (NullPointerException e) { 1449 // expected 1450 } 1451 1452 try { 1453 readWriteFileChannel.read(readBuffers, 0, 1); 1454 fail("should throw NullPointerException"); 1455 } catch (NullPointerException e) { 1456 // expected 1457 } 1458 1459 // first throws NullPointerException 1460 writeOnlyFileChannel.close(); 1461 try { 1462 writeOnlyFileChannel.read(readBuffers, 0, 1); 1463 fail("should throw NullPointerException"); 1464 } catch (NullPointerException e) { 1465 // expected 1466 } 1467 } 1468 1469 /** 1470 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1471 */ 1472 public void test_read$LByteBufferII_Closed() throws Exception { 1473 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1474 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1475 1476 readOnlyFileChannel.close(); 1477 try { 1478 readOnlyFileChannel.read(readBuffers, 0, 1); 1479 fail("should throw ClosedChannelException"); 1480 } catch (ClosedChannelException e) { 1481 // expected 1482 } 1483 1484 writeOnlyFileChannel.close(); 1485 try { 1486 writeOnlyFileChannel.read(readBuffers, 0, 1); 1487 fail("should throw ClosedChannelException"); 1488 } catch (ClosedChannelException e) { 1489 // expected 1490 } 1491 1492 readWriteFileChannel.close(); 1493 try { 1494 readWriteFileChannel.read(readBuffers, 0, 1); 1495 fail("should throw ClosedChannelException"); 1496 } catch (ClosedChannelException e) { 1497 // expected 1498 } 1499 1500 // regression test for Harmony-902 1501 readBuffers[0] = null; 1502 try { 1503 readOnlyFileChannel.read(readBuffers, 0, 1); 1504 fail("should throw ClosedChannelException"); 1505 } catch (ClosedChannelException e) { 1506 // expected 1507 } 1508 try { 1509 writeOnlyFileChannel.read(readBuffers, 0, 1); 1510 fail("should throw ClosedChannelException"); 1511 } catch (ClosedChannelException e) { 1512 // expected 1513 } 1514 try { 1515 readWriteFileChannel.read(readBuffers, 0, 1); 1516 fail("should throw ClosedChannelException"); 1517 } catch (ClosedChannelException e) { 1518 // expected 1519 } 1520 } 1521 1522 /** 1523 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1524 */ 1525 public void test_read$LByteBufferII_WriteOnly() throws Exception { 1526 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1527 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1528 1529 try { 1530 writeOnlyFileChannel.read(readBuffers, 0, 1); 1531 fail("should throw NonReadableChannelException"); 1532 } catch (NonReadableChannelException e) { 1533 // expected 1534 } 1535 1536 // first throws NonReadableChannelException. 1537 readBuffers[0] = null; 1538 try { 1539 writeOnlyFileChannel.read(readBuffers, 0, 1); 1540 fail("should throw NonReadableChannelException"); 1541 } catch (NonReadableChannelException e) { 1542 // expected 1543 } 1544 } 1545 1546 /** 1547 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1548 */ 1549 public void test_read$LByteBufferII_IndexOutOfBound() throws Exception { 1550 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1551 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1552 readBuffers[1] = ByteBuffer.allocate(CAPACITY); 1553 1554 try { 1555 readOnlyFileChannel.read(readBuffers, 2, 1); 1556 fail(); 1557 } catch (IndexOutOfBoundsException expected) { 1558 } 1559 1560 try { 1561 readWriteFileChannel.read(null, -1, 0); 1562 fail(); 1563 } catch (IndexOutOfBoundsException expected) { 1564 } catch (NullPointerException expected) { 1565 } 1566 1567 try { 1568 writeOnlyFileChannel.read(readBuffers, 0, 3); 1569 fail(); 1570 } catch (IndexOutOfBoundsException expected) { 1571 } 1572 1573 try { 1574 readWriteFileChannel.read(readBuffers, -1, 0); 1575 fail(); 1576 } catch (IndexOutOfBoundsException expected) { 1577 } 1578 1579 readWriteFileChannel.close(); 1580 try { 1581 readWriteFileChannel.read(readBuffers, 0, 3); 1582 fail(); 1583 } catch (IndexOutOfBoundsException expected) { 1584 } 1585 } 1586 1587 /** 1588 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1589 */ 1590 public void test_read$LByteBufferII_EmptyFile() throws Exception { 1591 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1592 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1593 readBuffers[1] = ByteBuffer.allocate(CAPACITY); 1594 long result = readOnlyFileChannel.read(readBuffers, 0, 2); 1595 assertEquals(-1, result); 1596 assertEquals(0, readBuffers[0].position()); 1597 assertEquals(0, readBuffers[1].position()); 1598 } 1599 1600 /** 1601 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1602 */ 1603 public void test_read$LByteBufferII_EmptyBuffers() throws Exception { 1604 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1605 try { 1606 readOnlyFileChannel.read(readBuffers, 0, 2); 1607 } catch (NullPointerException e) { 1608 // expected 1609 } 1610 1611 writeDataToFile(fileOfReadOnlyFileChannel); 1612 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1613 try { 1614 readOnlyFileChannel.read(readBuffers, 0, 2); 1615 } catch (NullPointerException e) { 1616 // expected 1617 } 1618 1619 long result = readOnlyFileChannel.read(readBuffers, 0, 1); 1620 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 1621 } 1622 1623 /** 1624 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1625 */ 1626 public void test_read$LByteBufferII_EmptyFile_EmptyBuffers() 1627 throws Exception { 1628 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1629 // will not throw NullPointerException 1630 long result = readOnlyFileChannel.read(readBuffers, 0, 0); 1631 assertEquals(0, result); 1632 } 1633 1634 /** 1635 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1636 */ 1637 public void test_read$LByteBufferII_Length_Zero() throws Exception { 1638 writeDataToFile(fileOfReadOnlyFileChannel); 1639 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1640 readBuffers[0] = ByteBuffer.allocate(LIMITED_CAPACITY); 1641 readBuffers[1] = ByteBuffer.allocate(LIMITED_CAPACITY); 1642 long result = readOnlyFileChannel.read(readBuffers, 1, 0); 1643 assertEquals(0, result); 1644 } 1645 1646 /** 1647 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1648 */ 1649 public void test_read$LByteBufferII_LimitedCapacity() throws Exception { 1650 writeDataToFile(fileOfReadOnlyFileChannel); 1651 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1652 readBuffers[0] = ByteBuffer.allocate(LIMITED_CAPACITY); 1653 readBuffers[1] = ByteBuffer.allocate(LIMITED_CAPACITY); 1654 1655 // reads to the second buffer 1656 long result = readOnlyFileChannel.read(readBuffers, 1, 1); 1657 assertEquals(LIMITED_CAPACITY, result); 1658 assertEquals(0, readBuffers[0].position()); 1659 assertEquals(LIMITED_CAPACITY, readBuffers[1].position()); 1660 1661 readBuffers[1].flip(); 1662 for (int i = 0; i < LIMITED_CAPACITY; i++) { 1663 assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get()); 1664 } 1665 } 1666 1667 /** 1668 * @tests java.nio.channels.FileChannel#read(ByteBuffer[], int, int) 1669 */ 1670 public void test_read$LByteBufferII() throws Exception { 1671 writeDataToFile(fileOfReadOnlyFileChannel); 1672 ByteBuffer[] readBuffers = new ByteBuffer[2]; 1673 readBuffers[0] = ByteBuffer.allocate(CAPACITY); 1674 readBuffers[1] = ByteBuffer.allocate(CAPACITY); 1675 1676 // writes to the second buffer 1677 assertEquals(CONTENT_AS_BYTES_LENGTH, readOnlyFileChannel.read( 1678 readBuffers, 1, 1)); 1679 assertEquals(0, readBuffers[0].position()); 1680 assertEquals(CONTENT_AS_BYTES_LENGTH, readBuffers[1].position()); 1681 1682 readBuffers[1].flip(); 1683 for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) { 1684 assertEquals(CONTENT_AS_BYTES[i], readBuffers[1].get()); 1685 } 1686 } 1687 1688 /** 1689 * @tests java.nio.channels.FileChannel#isOpen() 1690 */ 1691 public void test_isOpen() throws Exception { 1692 // Regression for HARMONY-40 1693 File logFile = File.createTempFile("out", "tmp"); 1694 logFile.deleteOnExit(); 1695 FileOutputStream out = new FileOutputStream(logFile, true); 1696 FileChannel channel = out.getChannel(); 1697 out.write(1); 1698 out.close(); 1699 assertFalse("Assert 0: Channel is still open", channel.isOpen()); 1700 } 1701 1702 /** 1703 * @tests java.nio.channels.FileChannel#position() 1704 */ 1705 public void test_position_append() throws Exception { 1706 // Regression test for Harmony-508 1707 File tmpfile = File.createTempFile("FileOutputStream", "tmp"); 1708 tmpfile.deleteOnExit(); 1709 FileOutputStream fos = new FileOutputStream(tmpfile); 1710 byte[] b = new byte[10]; 1711 for (int i = 0; i < b.length; i++) { 1712 b[i] = (byte) i; 1713 } 1714 fos.write(b); 1715 fos.flush(); 1716 fos.close(); 1717 FileOutputStream f = new FileOutputStream(tmpfile, true); 1718 // We're in append mode, so we should be positioned at the end of the file. 1719 assertEquals(10, f.getChannel().position()); 1720 } 1721 1722 1723 /** 1724 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1725 */ 1726 public void test_map_AbnormalMode() throws IOException { 1727 try { 1728 writeOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH); 1729 fail("should throw NonReadableChannelException."); 1730 } catch (NonReadableChannelException ex) { 1731 // expected; 1732 } 1733 try { 1734 writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH); 1735 fail("should throw NonReadableChannelException."); 1736 } catch (NonReadableChannelException ex) { 1737 // expected; 1738 } 1739 try { 1740 writeOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH); 1741 fail("should throw NonReadableChannelException."); 1742 } catch (NonReadableChannelException ex) { 1743 // expected; 1744 } 1745 writeOnlyFileChannel.close(); 1746 try { 1747 writeOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1); 1748 fail("should throw ClosedChannelException."); 1749 } catch (ClosedChannelException ex) { 1750 // expected; 1751 } 1752 1753 try { 1754 readOnlyFileChannel.map(MapMode.READ_WRITE, 0, CONTENT_LENGTH); 1755 fail("should throw NonWritableChannelException ."); 1756 } catch (NonWritableChannelException ex) { 1757 // expected; 1758 } 1759 try { 1760 readOnlyFileChannel.map(MapMode.PRIVATE, 0, CONTENT_LENGTH); 1761 fail("should throw NonWritableChannelException ."); 1762 } catch (NonWritableChannelException ex) { 1763 // expected; 1764 } 1765 try { 1766 readOnlyFileChannel.map(MapMode.READ_WRITE, -1, CONTENT_LENGTH); 1767 fail("should throw IAE."); 1768 } catch (IllegalArgumentException ex) { 1769 // expected; 1770 } 1771 try { 1772 readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1); 1773 fail("should throw IAE."); 1774 } catch (IllegalArgumentException ex) { 1775 // expected; 1776 } 1777 1778 try { 1779 readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH + 1); 1780 fail(); 1781 } catch (NonWritableChannelException expected) { 1782 } catch (IOException expected) { 1783 } 1784 try { 1785 readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1); 1786 fail(); 1787 } catch (NonWritableChannelException expected) { 1788 } catch (IOException expected) { 1789 } 1790 1791 readOnlyFileChannel.close(); 1792 try { 1793 readOnlyFileChannel.map(MapMode.READ_WRITE, 0, -1); 1794 fail("should throw ClosedChannelException."); 1795 } catch (ClosedChannelException ex) { 1796 // expected; 1797 } 1798 try { 1799 readOnlyFileChannel.map(MapMode.READ_ONLY, 2, CONTENT_LENGTH - 1); 1800 fail("should throw IOException."); 1801 } catch (IOException ex) { 1802 // expected; 1803 } 1804 1805 readWriteFileChannel.close(); 1806 try { 1807 readWriteFileChannel.map(MapMode.READ_WRITE, 0, -1); 1808 fail("should throw ClosedChannelException."); 1809 } catch (ClosedChannelException ex) { 1810 // expected; 1811 } 1812 } 1813 1814 /** 1815 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1816 */ 1817 public void test_map_ReadOnly_CloseChannel() throws IOException { 1818 // close channel has no effect on map if mapped 1819 assertEquals(0, readWriteFileChannel.size()); 1820 MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 1821 0, CONTENT_LENGTH); 1822 assertEquals(CONTENT_LENGTH, readWriteFileChannel.size()); 1823 readOnlyFileChannel.close(); 1824 assertEquals(CONTENT_LENGTH, mapped.limit()); 1825 } 1826 1827 /** 1828 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1829 */ 1830 public void test_map_Private_CloseChannel() throws IOException { 1831 MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0, 1832 CONTENT_LENGTH); 1833 readWriteFileChannel.close(); 1834 mapped.put(TEST_BYTES); 1835 assertEquals(CONTENT_LENGTH, mapped.limit()); 1836 assertEquals("test".length(), mapped.position()); 1837 } 1838 1839 /** 1840 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1841 */ 1842 public void test_map_ReadOnly() throws IOException { 1843 MappedByteBuffer mapped = null; 1844 // try put something to readonly map 1845 writeDataToFile(fileOfReadOnlyFileChannel); 1846 mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH); 1847 try { 1848 mapped.put(TEST_BYTES); 1849 fail("should throw ReadOnlyBufferException."); 1850 } catch (ReadOnlyBufferException ex) { 1851 // expected; 1852 } 1853 assertEquals(CONTENT_LENGTH, mapped.limit()); 1854 assertEquals(CONTENT_LENGTH, mapped.capacity()); 1855 assertEquals(0, mapped.position()); 1856 1857 // try to get a readonly map from read/write channel 1858 writeDataToFile(fileOfReadWriteFileChannel); 1859 mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT 1860 .length()); 1861 assertEquals(CONTENT_LENGTH, mapped.limit()); 1862 assertEquals(CONTENT_LENGTH, mapped.capacity()); 1863 assertEquals(0, mapped.position()); 1864 1865 // map not change channel's position 1866 assertEquals(0, readOnlyFileChannel.position()); 1867 assertEquals(0, readWriteFileChannel.position()); 1868 } 1869 1870 /** 1871 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1872 */ 1873 public void test_map_ReadOnly_NonZeroPosition() throws IOException { 1874 this.writeDataToFile(fileOfReadOnlyFileChannel); 1875 MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 1876 10, CONTENT_LENGTH - 10); 1877 assertEquals(CONTENT_LENGTH - 10, mapped.limit()); 1878 assertEquals(CONTENT_LENGTH - 10, mapped.capacity()); 1879 assertEquals(0, mapped.position()); 1880 } 1881 1882 /** 1883 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1884 */ 1885 public void test_map_Private() throws IOException { 1886 this.writeDataToFile(fileOfReadWriteFileChannel); 1887 MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0, 1888 CONTENT_LENGTH); 1889 assertEquals(CONTENT_LENGTH, mapped.limit()); 1890 // test copy on write if private 1891 ByteBuffer returnByPut = mapped.put(TEST_BYTES); 1892 assertSame(returnByPut, mapped); 1893 ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 1894 mapped.force(); 1895 readWriteFileChannel.read(checkBuffer); 1896 assertEquals(CONTENT, new String(checkBuffer.array(), "iso8859-1")); 1897 1898 // test overflow 1899 try { 1900 mapped.put(("test" + CONTENT).getBytes("iso8859-1")); 1901 fail("should throw BufferOverflowException."); 1902 } catch (BufferOverflowException ex) { 1903 // expected; 1904 } 1905 } 1906 1907 /** 1908 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1909 */ 1910 public void test_map_Private_NonZeroPosition() throws IOException { 1911 MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 10, 1912 CONTENT_LENGTH - 10); 1913 assertEquals(CONTENT_LENGTH - 10, mapped.limit()); 1914 assertEquals(CONTENT_LENGTH - 10, mapped.capacity()); 1915 assertEquals(0, mapped.position()); 1916 } 1917 1918 /** 1919 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1920 */ 1921 public void test_map_ReadWrite() throws IOException { 1922 MappedByteBuffer mapped = null; 1923 writeDataToFile(fileOfReadWriteFileChannel); 1924 mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT 1925 .length()); 1926 1927 // put something will change its channel 1928 ByteBuffer returnByPut = mapped.put(TEST_BYTES); 1929 assertSame(returnByPut, mapped); 1930 String checkString = "test" + CONTENT.substring(4); 1931 ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 1932 mapped.force(); 1933 readWriteFileChannel.position(0); 1934 readWriteFileChannel.read(checkBuffer); 1935 assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1")); 1936 1937 try { 1938 mapped.put(("test" + CONTENT).getBytes("iso8859-1")); 1939 fail("should throw BufferOverflowException."); 1940 } catch (BufferOverflowException ex) { 1941 // expected; 1942 } 1943 } 1944 1945 /** 1946 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1947 */ 1948 public void test_map_ReadWrite_NonZeroPosition() throws IOException { 1949 // test position non-zero 1950 writeDataToFile(fileOfReadWriteFileChannel); 1951 MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 1952 10, CONTENT_LENGTH - 10); 1953 assertEquals(CONTENT_LENGTH - 10, mapped.limit()); 1954 assertEquals(CONTENT.length() - 10, mapped.capacity()); 1955 assertEquals(0, mapped.position()); 1956 mapped.put(TEST_BYTES); 1957 ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH); 1958 readWriteFileChannel.read(checkBuffer); 1959 String expected = CONTENT.substring(0, 10) + "test" 1960 + CONTENT.substring(10 + "test".length()); 1961 assertEquals(expected, new String(checkBuffer.array(), "iso8859-1")); 1962 } 1963 1964 /** 1965 * Tests map() method for the value of positions exceeding memory 1966 * page size and allocation granularity size. 1967 * 1968 * @tests java.nio.channels.FileChannel#map(MapMode,long,long) 1969 */ 1970 public void test_map_LargePosition() throws IOException { 1971 // Regression test for HARMONY-3085 1972 int[] sizes = { 1973 4096, // 4K size (normal page size for Linux & Windows) 1974 65536, // 64K size (alocation granularity size for Windows) 1975 }; 1976 final int CONTENT_LEN = 10; 1977 1978 for (int i = 0; i < sizes.length; ++i) { 1979 // reset the file and the channel for the iterations 1980 // (for the first iteration it was done by setUp() 1981 if (i > 0 ) { 1982 fileOfReadOnlyFileChannel = File.createTempFile( 1983 "File_of_readOnlyFileChannel", "tmp"); 1984 fileOfReadOnlyFileChannel.deleteOnExit(); 1985 readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel) 1986 .getChannel(); 1987 } 1988 1989 writeLargeDataToFile(fileOfReadOnlyFileChannel, sizes[i] + 2 * CONTENT_LEN); 1990 MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 1991 sizes[i], CONTENT_LEN); 1992 assertEquals("Incorrectly mapped file channel for " + sizes[i] 1993 + " position (capacity)", CONTENT_LEN, mapped.capacity()); 1994 assertEquals("Incorrectly mapped file channel for " + sizes[i] 1995 + " position (limit)", CONTENT_LEN, mapped.limit()); 1996 assertEquals("Incorrectly mapped file channel for " + sizes[i] 1997 + " position (position)", 0, mapped.position()); 1998 1999 // map not change channel's position 2000 assertEquals(0, readOnlyFileChannel.position()); 2001 2002 // Close the file and the channel before the next iteration 2003 readOnlyFileChannel.close(); 2004 fileOfReadOnlyFileChannel.delete(); 2005 } 2006 } 2007 2008 /** 2009 * @tests java.nio.channels.FileChannel#write(ByteBuffer) 2010 */ 2011 public void test_writeLByteBuffer_Null() throws Exception { 2012 ByteBuffer writeBuffer = null; 2013 2014 try { 2015 writeOnlyFileChannel.write(writeBuffer); 2016 fail("should throw NullPointerException"); 2017 } catch (NullPointerException e) { 2018 // expected 2019 } 2020 2021 try { 2022 readWriteFileChannel.write(writeBuffer); 2023 fail("should throw NullPointerException"); 2024 } catch (NullPointerException e) { 2025 // expected 2026 } 2027 } 2028 2029 /** 2030 * @tests java.nio.channels.FileChannel#write(ByteBuffer) 2031 */ 2032 public void test_writeLByteBuffer_Closed() throws Exception { 2033 ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY); 2034 2035 readOnlyFileChannel.close(); 2036 try { 2037 readOnlyFileChannel.write(writeBuffer); 2038 fail("should throw ClosedChannelException"); 2039 } catch (ClosedChannelException e) { 2040 // expected 2041 } 2042 2043 writeOnlyFileChannel.close(); 2044 try { 2045 writeOnlyFileChannel.write(writeBuffer); 2046 fail("should throw ClosedChannelException"); 2047 } catch (ClosedChannelException e) { 2048 // expected 2049 } 2050 2051 readWriteFileChannel.close(); 2052 try { 2053 readWriteFileChannel.write(writeBuffer); 2054 fail(); 2055 } catch (ClosedChannelException expected) { 2056 } 2057 2058 writeBuffer = null; 2059 try { 2060 readWriteFileChannel.read(writeBuffer); 2061 fail(); 2062 } catch (NullPointerException expected) { 2063 } catch (ClosedChannelException expected) { 2064 } 2065 } 2066 2067 /** 2068 * @tests java.nio.channels.FileChannel#write(ByteBuffer) 2069 */ 2070 public void test_writeLByteBuffer_ReadOnly() throws Exception { 2071 ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY); 2072 2073 try { 2074 readOnlyFileChannel.write(writeBuffer); 2075 fail("should throw NonWritableChannelException"); 2076 } catch (NonWritableChannelException e) { 2077 // expected 2078 } 2079 2080 // first throws NonWriteableChannelException 2081 writeBuffer = null; 2082 try { 2083 readOnlyFileChannel.write(writeBuffer); 2084 fail("should throw NonWritableChannelException"); 2085 } catch (NonWritableChannelException e) { 2086 // expected 2087 } 2088 } 2089 2090 /** 2091 * @tests java.nio.channels.FileChannel#write(ByteBuffer) 2092 */ 2093 public void test_writeLByteBuffer() throws Exception { 2094 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2095 2096 int result = writeOnlyFileChannel.write(writeBuffer); 2097 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 2098 assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position()); 2099 writeOnlyFileChannel.close(); 2100 2101 assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel 2102 .length()); 2103 2104 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2105 byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH]; 2106 fis.read(inputBuffer); 2107 assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer)); 2108 } 2109 2110 /** 2111 * @tests java.nio.channels.FileChannel#write(ByteBuffer) 2112 */ 2113 public void test_writeLByteBuffer_positioned() throws Exception { 2114 final int pos = 5; 2115 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2116 writeBuffer.position(pos); 2117 int result = writeOnlyFileChannel.write(writeBuffer); 2118 assertEquals(CONTENT_AS_BYTES_LENGTH - pos, result); 2119 assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position()); 2120 writeOnlyFileChannel.close(); 2121 2122 assertEquals(CONTENT_AS_BYTES_LENGTH - pos, fileOfWriteOnlyFileChannel 2123 .length()); 2124 2125 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2126 byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH - pos]; 2127 fis.read(inputBuffer); 2128 String test = CONTENT.substring(pos); 2129 assertTrue(Arrays.equals(test.getBytes("iso8859-1"), inputBuffer)); 2130 } 2131 2132 public void test_writeLByteBufferJ_Null() throws Exception { 2133 try { 2134 readWriteFileChannel.write(null, 0); 2135 fail(); 2136 } catch (NullPointerException expected) { 2137 } catch (NonWritableChannelException expected) { 2138 } 2139 2140 try { 2141 writeOnlyFileChannel.write(null, 0); 2142 fail(); 2143 } catch (NullPointerException expected) { 2144 } 2145 2146 try { 2147 readWriteFileChannel.write(null, 0); 2148 fail(); 2149 } catch (NullPointerException expected) { 2150 } 2151 } 2152 2153 public void test_writeLByteBufferJ_Closed() throws Exception { 2154 ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY); 2155 2156 writeOnlyFileChannel.close(); 2157 try { 2158 writeOnlyFileChannel.write(writeBuffer, 0); 2159 fail(); 2160 } catch (ClosedChannelException expected) { 2161 } 2162 2163 readWriteFileChannel.close(); 2164 try { 2165 readWriteFileChannel.write(writeBuffer, 0); 2166 fail(); 2167 } catch (ClosedChannelException expected) { 2168 } 2169 } 2170 2171 public void test_writeLByteBufferJ_ReadOnly() throws Exception { 2172 ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY); 2173 try { 2174 readOnlyFileChannel.write(writeBuffer, 10); 2175 fail(); 2176 } catch (NonWritableChannelException expected) { 2177 } 2178 } 2179 2180 public void test_writeLByteBufferJ_IllegalArgument() throws Exception { 2181 ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY); 2182 2183 try { 2184 readOnlyFileChannel.write(writeBuffer, -1); 2185 fail(); 2186 } catch (IllegalArgumentException expected) { 2187 } 2188 2189 try { 2190 writeOnlyFileChannel.write(writeBuffer, -1); 2191 fail(); 2192 } catch (IllegalArgumentException expected) { 2193 } 2194 2195 try { 2196 readWriteFileChannel.write(writeBuffer, -1); 2197 fail(); 2198 } catch (IllegalArgumentException expected) { 2199 } 2200 } 2201 2202 /** 2203 * @tests java.nio.channels.FileChannel#write(ByteBuffer,long) 2204 */ 2205 public void test_writeLByteBufferJ() throws Exception { 2206 writeDataToFile(fileOfWriteOnlyFileChannel); 2207 2208 final int POSITION = 4; 2209 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2210 int result = writeOnlyFileChannel.write(writeBuffer, POSITION); 2211 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 2212 assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position()); 2213 writeOnlyFileChannel.close(); 2214 2215 assertEquals(POSITION + CONTENT_AS_BYTES_LENGTH, 2216 fileOfWriteOnlyFileChannel.length()); 2217 2218 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2219 byte[] inputBuffer = new byte[POSITION + CONTENT_AS_BYTES_LENGTH]; 2220 fis.read(inputBuffer); 2221 byte[] expectedResult = new byte[POSITION + CONTENT_AS_BYTES_LENGTH]; 2222 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, POSITION); 2223 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, POSITION, 2224 CONTENT_AS_BYTES_LENGTH); 2225 assertTrue(Arrays.equals(expectedResult, inputBuffer)); 2226 } 2227 2228 /** 2229 * @tests java.nio.channels.FileChannel#write(ByteBuffer[]) 2230 */ 2231 public void test_write$LByteBuffer() throws Exception { 2232 ByteBuffer[] writeBuffers = new ByteBuffer[2]; 2233 MockFileChannel mockFileChannel = new MockFileChannel(); 2234 mockFileChannel.write(writeBuffers); 2235 // verify that calling write(ByteBuffer[] srcs) leads to the method 2236 // write(srcs, 0, srcs.length) 2237 assertTrue(mockFileChannel.isWriteCalled); 2238 } 2239 2240 /** 2241 * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int) 2242 */ 2243 public void test_write$LByteBufferII_Null() throws Exception { 2244 ByteBuffer[] writeBuffers = null; 2245 2246 try { 2247 readOnlyFileChannel.write(writeBuffers, 1, 2); 2248 fail("should throw NullPointerException"); 2249 } catch (NullPointerException e) { 2250 // expected 2251 } 2252 2253 try { 2254 writeOnlyFileChannel.write(writeBuffers, 1, 2); 2255 fail("should throw NullPointerException"); 2256 } catch (NullPointerException e) { 2257 // expected 2258 } 2259 2260 try { 2261 readWriteFileChannel.write(writeBuffers, 1, 2); 2262 fail("should throw NullPointerException"); 2263 } catch (NullPointerException e) { 2264 // expected 2265 } 2266 2267 // first throws NullPointerException 2268 readWriteFileChannel.close(); 2269 try { 2270 readWriteFileChannel.write(writeBuffers, 0, 0); 2271 fail("should throw NullPointerException"); 2272 } catch (NullPointerException e) { 2273 // expected 2274 } 2275 } 2276 2277 /** 2278 * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int) 2279 */ 2280 public void test_write$LByteBufferII_Closed() throws Exception { 2281 ByteBuffer[] writeBuffers = new ByteBuffer[2]; 2282 writeBuffers[0] = ByteBuffer.allocate(CAPACITY); 2283 writeBuffers[1] = ByteBuffer.allocate(CAPACITY); 2284 2285 readOnlyFileChannel.close(); 2286 try { 2287 readOnlyFileChannel.write(writeBuffers, 0, 2); 2288 fail("should throw ClosedChannelException"); 2289 } catch (ClosedChannelException e) { 2290 // expected 2291 } 2292 2293 writeOnlyFileChannel.close(); 2294 try { 2295 writeOnlyFileChannel.write(writeBuffers, 0, 2); 2296 fail("should throw ClosedChannelException"); 2297 } catch (ClosedChannelException e) { 2298 // expected 2299 } 2300 2301 readWriteFileChannel.close(); 2302 try { 2303 readWriteFileChannel.write(writeBuffers, 0, 2); 2304 fail("should throw ClosedChannelException"); 2305 } catch (ClosedChannelException e) { 2306 // expected 2307 } 2308 2309 // throws ClosedChannelException first 2310 writeBuffers[0] = null; 2311 try { 2312 readWriteFileChannel.write(writeBuffers, 0, 2); 2313 fail("should throw ClosedChannelException"); 2314 } catch (ClosedChannelException e) { 2315 // expected 2316 } 2317 } 2318 2319 /** 2320 * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int) 2321 */ 2322 public void test_write$LByteBufferII_ReadOnly() throws Exception { 2323 ByteBuffer[] writeBuffers = new ByteBuffer[2]; 2324 writeBuffers[0] = ByteBuffer.allocate(CAPACITY); 2325 writeBuffers[1] = ByteBuffer.allocate(CAPACITY); 2326 2327 try { 2328 readOnlyFileChannel.write(writeBuffers, 0, 2); 2329 fail(); 2330 } catch (NonWritableChannelException e) { 2331 } 2332 2333 try { 2334 readOnlyFileChannel.write(writeBuffers, 0, -1); 2335 fail(); 2336 } catch (IndexOutOfBoundsException expected) { 2337 } 2338 2339 writeBuffers = null; 2340 try { 2341 readOnlyFileChannel.write(writeBuffers, 0, 1); 2342 fail(); 2343 } catch (NullPointerException expected) { 2344 } 2345 2346 readOnlyFileChannel.close(); 2347 } 2348 2349 /** 2350 * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int) 2351 */ 2352 public void test_write$LByteBufferII_EmptyBuffers() throws Exception { 2353 ByteBuffer[] writeBuffers = new ByteBuffer[2]; 2354 try { 2355 writeOnlyFileChannel.write(writeBuffers, 0, 2); 2356 fail("should throw NullPointerException"); 2357 } catch (NullPointerException e) { 2358 // expected 2359 } 2360 2361 try { 2362 readWriteFileChannel.write(writeBuffers, 0, 2); 2363 fail("should throw NullPointerException"); 2364 } catch (NullPointerException e) { 2365 // expected 2366 } 2367 } 2368 2369 /** 2370 * @tests java.nio.channels.FileChannel#write(ByteBuffer[],int,int) 2371 */ 2372 public void test_write$LByteBufferII() throws Exception { 2373 ByteBuffer[] writeBuffers = new ByteBuffer[2]; 2374 writeBuffers[0] = ByteBuffer.wrap(CONTENT_AS_BYTES); 2375 writeBuffers[1] = ByteBuffer.wrap(CONTENT_AS_BYTES); 2376 2377 long result = writeOnlyFileChannel.write(writeBuffers, 0, 2); 2378 assertEquals(CONTENT_AS_BYTES_LENGTH * 2, result); 2379 assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[0].position()); 2380 assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[1].position()); 2381 writeOnlyFileChannel.close(); 2382 2383 assertEquals(CONTENT_AS_BYTES_LENGTH * 2, fileOfWriteOnlyFileChannel 2384 .length()); 2385 2386 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2387 byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH]; 2388 fis.read(inputBuffer); 2389 byte[] expectedResult = new byte[CONTENT_AS_BYTES_LENGTH * 2]; 2390 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0, 2391 CONTENT_AS_BYTES_LENGTH); 2392 System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 2393 CONTENT_AS_BYTES_LENGTH, CONTENT_AS_BYTES_LENGTH); 2394 assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer)); 2395 } 2396 2397 /** 2398 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2399 */ 2400 public void test_transferFromLReadableByteChannelJJ_Closed() 2401 throws Exception { 2402 readByteChannel = DatagramChannel.open(); 2403 readOnlyFileChannel.close(); 2404 try { 2405 readOnlyFileChannel.transferFrom(readByteChannel, 0, 0); 2406 fail("should throw ClosedChannelException."); 2407 } catch (ClosedChannelException e) { 2408 // expected 2409 } 2410 2411 writeOnlyFileChannel.close(); 2412 try { 2413 writeOnlyFileChannel.transferFrom(readByteChannel, 0, 10); 2414 fail("should throw ClosedChannelException."); 2415 } catch (ClosedChannelException e) { 2416 // expected 2417 } 2418 2419 readWriteFileChannel.close(); 2420 try { 2421 readWriteFileChannel.transferFrom(readByteChannel, 0, 0); 2422 fail("should throw ClosedChannelException."); 2423 } catch (ClosedChannelException e) { 2424 // expected 2425 } 2426 2427 // should throw ClosedChannelException first. 2428 try { 2429 readWriteFileChannel.transferFrom(readByteChannel, 0, -1); 2430 fail("should throw ClosedChannelException."); 2431 } catch (ClosedChannelException e) { 2432 // expected 2433 } 2434 } 2435 2436 /** 2437 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2438 */ 2439 public void test_transferFromLReadableByteChannelJJ_SourceClosed() 2440 throws Exception { 2441 readByteChannel = DatagramChannel.open(); 2442 readByteChannel.close(); 2443 2444 try { 2445 readOnlyFileChannel.transferFrom(readByteChannel, 0, 10); 2446 fail("should throw ClosedChannelException."); 2447 } catch (ClosedChannelException e) { 2448 // expected 2449 } 2450 2451 try { 2452 writeOnlyFileChannel.transferFrom(readByteChannel, 0, 10); 2453 fail("should throw ClosedChannelException."); 2454 } catch (ClosedChannelException e) { 2455 // expected 2456 } 2457 2458 try { 2459 readWriteFileChannel.transferFrom(readByteChannel, 0, 10); 2460 fail("should throw ClosedChannelException."); 2461 } catch (ClosedChannelException e) { 2462 // expected 2463 } 2464 2465 // should throw ClosedChannelException first. 2466 try { 2467 readWriteFileChannel.transferFrom(readByteChannel, 0, -1); 2468 fail("should throw ClosedChannelException."); 2469 } catch (ClosedChannelException e) { 2470 // expected 2471 } 2472 } 2473 2474 /** 2475 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2476 */ 2477 public void test_transferFromLReadableByteChannelJJ_IllegalArgument() 2478 throws Exception { 2479 readByteChannel = DatagramChannel.open(); 2480 try { 2481 writeOnlyFileChannel.transferFrom(readByteChannel, 10, -1); 2482 fail("should throw IllegalArgumentException."); 2483 } catch (IllegalArgumentException e) { 2484 // expected 2485 } 2486 2487 try { 2488 readWriteFileChannel.transferFrom(readByteChannel, -1, -10); 2489 fail("should throw IllegalArgumentException."); 2490 } catch (IllegalArgumentException e) { 2491 // expected 2492 } 2493 } 2494 2495 /** 2496 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2497 */ 2498 public void test_transferFromLReadableByteChannelJJ_NonWritable() 2499 throws Exception { 2500 readByteChannel = DatagramChannel.open(); 2501 try { 2502 readOnlyFileChannel.transferFrom(readByteChannel, 0, 0); 2503 fail("should throw NonWritableChannelException."); 2504 } catch (NonWritableChannelException e) { 2505 // expected 2506 } 2507 } 2508 2509 /** 2510 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2511 */ 2512 public void test_transferFromLReadableByteChannelJJ_SourceNonReadable() 2513 throws Exception { 2514 try { 2515 readWriteFileChannel.transferFrom(writeOnlyFileChannel, 0, 0); 2516 fail("should throw NonReadableChannelException."); 2517 } catch (NonReadableChannelException e) { 2518 // expected 2519 } 2520 2521 // not throws NonReadableChannelException first if position beyond file 2522 // size. 2523 readWriteFileChannel.transferFrom(writeOnlyFileChannel, 10, 10); 2524 } 2525 2526 /** 2527 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2528 */ 2529 public void test_transferFromLReadableByteChannelJJ_PositionBeyondSize() 2530 throws Exception { 2531 // init data to file. 2532 writeDataToFile(fileOfReadOnlyFileChannel); 2533 writeDataToFile(fileOfWriteOnlyFileChannel); 2534 2535 final int READONLYFILECHANNELPOSITION = 2; 2536 readOnlyFileChannel.position(READONLYFILECHANNELPOSITION); 2537 2538 final int POSITION = CONTENT_AS_BYTES_LENGTH * 2; 2539 final int LENGTH = 5; 2540 long result = writeOnlyFileChannel.transferFrom(readOnlyFileChannel, 2541 POSITION, LENGTH); 2542 assertEquals(0, result); 2543 assertEquals(0, writeOnlyFileChannel.position()); 2544 assertEquals(READONLYFILECHANNELPOSITION, readOnlyFileChannel 2545 .position()); 2546 } 2547 2548 /** 2549 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2550 */ 2551 public void test_transferFromLReadableByteChannelJJ_FileChannel() 2552 throws Exception { 2553 // init data to file. 2554 writeDataToFile(fileOfReadOnlyFileChannel); 2555 writeDataToFile(fileOfWriteOnlyFileChannel); 2556 2557 final int READONLYFILECHANNELPOSITION = 2; 2558 final int WRITEONLYFILECHANNELPOSITION = 4; 2559 readOnlyFileChannel.position(READONLYFILECHANNELPOSITION); 2560 writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION); 2561 2562 final int POSITION = 3; 2563 final int LENGTH = 5; 2564 long result = writeOnlyFileChannel.transferFrom(readOnlyFileChannel, 2565 POSITION, LENGTH); 2566 assertEquals(LENGTH, result); 2567 assertEquals(WRITEONLYFILECHANNELPOSITION, writeOnlyFileChannel 2568 .position()); 2569 assertEquals(READONLYFILECHANNELPOSITION + LENGTH, readOnlyFileChannel 2570 .position()); 2571 writeOnlyFileChannel.close(); 2572 2573 final int EXPECTED_LENGTH = POSITION + LENGTH; 2574 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2575 byte[] resultContent = new byte[EXPECTED_LENGTH]; 2576 fis.read(resultContent); 2577 2578 byte[] expectedContent = new byte[EXPECTED_LENGTH]; 2579 System.arraycopy(CONTENT_AS_BYTES, 0, expectedContent, 0, POSITION); 2580 System.arraycopy(CONTENT_AS_BYTES, READONLYFILECHANNELPOSITION, 2581 expectedContent, POSITION, LENGTH); 2582 assertTrue(Arrays.equals(expectedContent, resultContent)); 2583 } 2584 2585 /** 2586 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2587 */ 2588 public void test_transferFromLReadableByteChannelJJ_DatagramChannel() 2589 throws Exception { 2590 // connects two datagramChannels. 2591 datagramChannelReceiver = DatagramChannel.open(); 2592 datagramChannelReceiver.socket().bind( 2593 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2594 datagramChannelSender = DatagramChannel.open(); 2595 datagramChannelSender.socket().bind( 2596 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2597 datagramChannelReceiver.socket().setSoTimeout(TIME_OUT); 2598 datagramChannelReceiver.connect(datagramChannelSender.socket() 2599 .getLocalSocketAddress()); 2600 datagramChannelSender.socket().setSoTimeout(TIME_OUT); 2601 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2602 datagramChannelSender.socket().setSoTimeout(TIME_OUT); 2603 // sends data from datagramChannelSender to datagramChannelReceiver. 2604 datagramChannelSender.send(writeBuffer, datagramChannelReceiver 2605 .socket().getLocalSocketAddress()); 2606 datagramChannelReceiver.socket().setSoTimeout(TIME_OUT); 2607 2608 // transfers data from datagramChannelReceiver to fileChannel. 2609 long result = writeOnlyFileChannel.transferFrom( 2610 datagramChannelReceiver, 0, CONTENT_AS_BYTES_LENGTH); 2611 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 2612 assertEquals(0, writeOnlyFileChannel.position()); 2613 writeOnlyFileChannel.close(); 2614 2615 // gets content from file. 2616 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2617 assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel 2618 .length()); 2619 byte[] resultContent = new byte[CONTENT_AS_BYTES_LENGTH]; 2620 fis.read(resultContent); 2621 2622 // compares contents. 2623 assertTrue(Arrays.equals(CONTENT_AS_BYTES, resultContent)); 2624 } 2625 2626 /** 2627 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2628 */ 2629 public void test_transferFromLReadableByteChannelJJ_SocketChannel() 2630 throws Exception { 2631 // connects two socketChannels. 2632 socketChannelReceiver = SocketChannel.open(); 2633 serverSocketChannel = ServerSocketChannel.open(); 2634 serverSocketChannel.socket().bind( 2635 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2636 socketChannelReceiver.socket().setSoTimeout(TIME_OUT); 2637 socketChannelReceiver.connect(serverSocketChannel.socket() 2638 .getLocalSocketAddress()); 2639 serverSocketChannel.socket().setSoTimeout(TIME_OUT); 2640 socketChannelSender = serverSocketChannel.accept(); 2641 socketChannelSender.socket().setSoTimeout(TIME_OUT); 2642 2643 // sends data from socketChannelSender to socketChannelReceiver. 2644 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2645 socketChannelSender.write(writeBuffer); 2646 2647 // transfers data from socketChannelReceiver to fileChannel. 2648 long result = readWriteFileChannel.transferFrom(socketChannelReceiver, 2649 0, CONTENT_AS_BYTES_LENGTH); 2650 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 2651 assertEquals(0, readWriteFileChannel.position()); 2652 readWriteFileChannel.close(); 2653 2654 // gets content from file. 2655 fis = new FileInputStream(fileOfReadWriteFileChannel); 2656 assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfReadWriteFileChannel 2657 .length()); 2658 byte[] resultContent = new byte[CONTENT_AS_BYTES_LENGTH]; 2659 fis.read(resultContent); 2660 2661 // compares content. 2662 assertTrue(Arrays.equals(CONTENT_AS_BYTES, resultContent)); 2663 } 2664 2665 /** 2666 * @tests java.nio.channels.FileChannel#transferFrom(ReadableByteChannel,long,long) 2667 */ 2668 public void test_transferFromLReadableByteChannelJJ_Pipe() throws Exception { 2669 // inits data in file. 2670 writeDataToFile(fileOfWriteOnlyFileChannel); 2671 2672 // inits pipe. 2673 pipe = Pipe.open(); 2674 2675 // writes content to pipe. 2676 ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES); 2677 pipe.sink().write(writeBuffer); 2678 2679 // transfers data from pipe to fileChannel. 2680 final int OFFSET = 2; 2681 final int LENGTH = 4; 2682 long result = writeOnlyFileChannel.transferFrom(pipe.source(), OFFSET, 2683 LENGTH); 2684 assertEquals(LENGTH, result); 2685 writeOnlyFileChannel.close(); 2686 2687 // gets content from file. 2688 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2689 byte[] resultBytes = new byte[OFFSET + LENGTH]; 2690 fis.read(resultBytes); 2691 2692 // compares content. 2693 byte[] expectedBytes = new byte[OFFSET + LENGTH]; 2694 System.arraycopy(CONTENT_AS_BYTES, 0, expectedBytes, 0, OFFSET); 2695 System.arraycopy(CONTENT_AS_BYTES, 0, expectedBytes, OFFSET, LENGTH); 2696 2697 assertTrue(Arrays.equals(expectedBytes, resultBytes)); 2698 } 2699 2700 /** 2701 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2702 */ 2703 public void test_transferToJJLWritableByteChannel_Null() throws Exception { 2704 writableByteChannel = null; 2705 try { 2706 readOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2707 fail(); 2708 } catch (NullPointerException expected) { 2709 } 2710 2711 try { 2712 writeOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2713 fail(); 2714 } catch (NullPointerException expected) { 2715 } catch (NonReadableChannelException expected) { 2716 } 2717 2718 try { 2719 readWriteFileChannel.transferTo(0, 10, writableByteChannel); 2720 fail(); 2721 } catch (NullPointerException expected) { 2722 } 2723 2724 readOnlyFileChannel.close(); 2725 try { 2726 writeOnlyFileChannel.transferTo(-1, 0, writableByteChannel); 2727 fail(); 2728 } catch (NullPointerException expected) { 2729 } catch (NonReadableChannelException expected) { 2730 } 2731 } 2732 2733 /** 2734 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2735 */ 2736 public void test_transferToJJLWritableByteChannel_Closed() throws Exception { 2737 writableByteChannel = DatagramChannel.open(); 2738 readOnlyFileChannel.close(); 2739 try { 2740 readOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2741 fail("should throw ClosedChannelException."); 2742 } catch (ClosedChannelException e) { 2743 // expected 2744 } 2745 2746 writeOnlyFileChannel.close(); 2747 try { 2748 writeOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2749 fail("should throw ClosedChannelException."); 2750 } catch (ClosedChannelException e) { 2751 // expected 2752 } 2753 2754 readWriteFileChannel.close(); 2755 try { 2756 readWriteFileChannel.transferTo(0, 10, writableByteChannel); 2757 fail("should throw ClosedChannelException."); 2758 } catch (ClosedChannelException e) { 2759 // expected 2760 } 2761 2762 // should throw ClosedChannelException first. 2763 try { 2764 readWriteFileChannel.transferTo(0, -1, writableByteChannel); 2765 fail("should throw ClosedChannelException."); 2766 } catch (ClosedChannelException e) { 2767 // expected 2768 } 2769 } 2770 2771 public void test_transferToJJLWritableByteChannel_SourceClosed() throws Exception { 2772 writableByteChannel = DatagramChannel.open(); 2773 writableByteChannel.close(); 2774 2775 try { 2776 readOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2777 fail(); 2778 } catch (ClosedChannelException expected) { 2779 } 2780 2781 try { 2782 writeOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2783 fail(); 2784 } catch (ClosedChannelException expected) { 2785 } catch (NonReadableChannelException expected) { 2786 } 2787 2788 try { 2789 readWriteFileChannel.transferTo(0, 10, writableByteChannel); 2790 fail(); 2791 } catch (ClosedChannelException expected) { 2792 } 2793 2794 try { 2795 readWriteFileChannel.transferTo(0, -1, writableByteChannel); 2796 fail(); 2797 } catch (ClosedChannelException expected) { 2798 } 2799 } 2800 2801 /** 2802 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2803 */ 2804 public void test_transferToJJLWritableByteChannel_IllegalArgument() 2805 throws Exception { 2806 writableByteChannel = DatagramChannel.open(); 2807 try { 2808 readOnlyFileChannel.transferTo(10, -1, writableByteChannel); 2809 fail("should throw IllegalArgumentException."); 2810 } catch (IllegalArgumentException e) { 2811 // expected 2812 } 2813 2814 try { 2815 readWriteFileChannel.transferTo(-1, -10, writableByteChannel); 2816 fail("should throw IllegalArgumentException."); 2817 } catch (IllegalArgumentException e) { 2818 // expected 2819 } 2820 } 2821 2822 public void test_transferToJJLWritableByteChannel_NonReadable() throws Exception { 2823 writableByteChannel = DatagramChannel.open(); 2824 try { 2825 writeOnlyFileChannel.transferTo(0, 10, writableByteChannel); 2826 fail(); 2827 } catch (NonReadableChannelException expected) { 2828 } 2829 } 2830 2831 /** 2832 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2833 */ 2834 public void test_transferToJJLWritableByteChannel_TargetNonWritable() 2835 throws Exception { 2836 try { 2837 readWriteFileChannel.transferTo(0, 0, readOnlyFileChannel); 2838 fail("should throw NonWritableChannelException."); 2839 } catch (NonWritableChannelException e) { 2840 // expected 2841 } 2842 2843 // first throws NonWritableChannelException even position out of file 2844 // size. 2845 try { 2846 readWriteFileChannel.transferTo(10, 10, readOnlyFileChannel); 2847 fail("should throw NonWritableChannelException."); 2848 } catch (NonWritableChannelException e) { 2849 // expected 2850 } 2851 2852 // regression test for Harmony-941 2853 // first throws NonWritableChannelException even arguments are illegal. 2854 try { 2855 readWriteFileChannel.transferTo(-1, 10, readOnlyFileChannel); 2856 fail("should throw NonWritableChannelException."); 2857 } catch (NonWritableChannelException e) { 2858 // expected 2859 } 2860 2861 try { 2862 readWriteFileChannel.transferTo(0, -1, readOnlyFileChannel); 2863 fail("should throw NonWritableChannelException."); 2864 } catch (NonWritableChannelException e) { 2865 // expected 2866 } 2867 } 2868 2869 /** 2870 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2871 */ 2872 public void test_transferToJJLWritableByteChannel_PositionBeyondSize() 2873 throws Exception { 2874 // init data to file. 2875 writeDataToFile(fileOfReadOnlyFileChannel); 2876 writeDataToFile(fileOfWriteOnlyFileChannel); 2877 2878 final int WRITEONLYFILECHANNELPOSITION = 2; 2879 writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION); 2880 2881 final int POSITION = CONTENT_AS_BYTES_LENGTH * 2; 2882 final int LENGTH = 5; 2883 long result = readOnlyFileChannel.transferTo(POSITION, LENGTH, 2884 writeOnlyFileChannel); 2885 assertEquals(0, result); 2886 assertEquals(0, readOnlyFileChannel.position()); 2887 assertEquals(WRITEONLYFILECHANNELPOSITION, writeOnlyFileChannel 2888 .position()); 2889 } 2890 2891 /** 2892 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2893 */ 2894 public void test_transferToJJLWritableByteChannel_FileChannel() 2895 throws Exception { 2896 // init data to file. 2897 writeDataToFile(fileOfReadOnlyFileChannel); 2898 writeDataToFile(fileOfWriteOnlyFileChannel); 2899 2900 final int READONLYFILECHANNELPOSITION = 2; 2901 final int WRITEONLYFILECHANNELPOSITION = 4; 2902 readOnlyFileChannel.position(READONLYFILECHANNELPOSITION); 2903 writeOnlyFileChannel.position(WRITEONLYFILECHANNELPOSITION); 2904 2905 final int POSITION = 3; 2906 final int LENGTH = 5; 2907 long result = readOnlyFileChannel.transferTo(POSITION, LENGTH, 2908 writeOnlyFileChannel); 2909 assertEquals(LENGTH, result); 2910 assertEquals(READONLYFILECHANNELPOSITION, readOnlyFileChannel 2911 .position()); 2912 assertEquals(WRITEONLYFILECHANNELPOSITION + LENGTH, 2913 writeOnlyFileChannel.position()); 2914 writeOnlyFileChannel.close(); 2915 2916 final int EXPECTED_LENGTH = WRITEONLYFILECHANNELPOSITION + LENGTH; 2917 fis = new FileInputStream(fileOfWriteOnlyFileChannel); 2918 byte[] resultContent = new byte[EXPECTED_LENGTH]; 2919 fis.read(resultContent); 2920 2921 byte[] expectedContent = new byte[EXPECTED_LENGTH]; 2922 System.arraycopy(CONTENT_AS_BYTES, 0, expectedContent, 0, 2923 WRITEONLYFILECHANNELPOSITION); 2924 System.arraycopy(CONTENT_AS_BYTES, POSITION, expectedContent, 2925 WRITEONLYFILECHANNELPOSITION, LENGTH); 2926 assertTrue(Arrays.equals(expectedContent, resultContent)); 2927 } 2928 2929 /** 2930 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2931 */ 2932 public void test_transferToJJLWritableByteChannel_SocketChannel() 2933 throws Exception { 2934 // inits data into file. 2935 writeDataToFile(fileOfReadOnlyFileChannel); 2936 2937 // connects two socketChannels. 2938 socketChannelReceiver = SocketChannel.open(); 2939 socketChannelReceiver.socket().bind( 2940 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2941 serverSocketChannel = ServerSocketChannel.open(); 2942 serverSocketChannel.socket().bind( 2943 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2944 socketChannelReceiver.socket().setSoTimeout(TIME_OUT); 2945 socketChannelReceiver.connect(serverSocketChannel.socket() 2946 .getLocalSocketAddress()); 2947 serverSocketChannel.socket().setSoTimeout(TIME_OUT); 2948 socketChannelSender = serverSocketChannel.accept(); 2949 socketChannelSender.socket().setSoTimeout(TIME_OUT); 2950 2951 // position here should have no effect on transferTo since it uses 2952 // offset from file_begin 2953 final int POSITION = 10; 2954 readOnlyFileChannel.position(POSITION); 2955 2956 // transfers data from file to socketChannelSender. 2957 final int OFFSET = 2; 2958 long result = readOnlyFileChannel.transferTo(OFFSET, 2959 CONTENT_AS_BYTES_LENGTH * 2, socketChannelSender); 2960 final int LENGTH = CONTENT_AS_BYTES_LENGTH - OFFSET; 2961 assertEquals(LENGTH, result); 2962 assertEquals(POSITION, readOnlyFileChannel.position()); 2963 readOnlyFileChannel.close(); 2964 socketChannelSender.close(); 2965 2966 // gets contents from socketChannelReceiver. 2967 ByteBuffer readBuffer = ByteBuffer.allocate(LENGTH + 1); 2968 int totalRead = 0; 2969 int countRead = 0; 2970 long beginTime = System.currentTimeMillis(); 2971 while ((countRead = socketChannelReceiver.read(readBuffer)) != -1) { 2972 totalRead += countRead; 2973 // TIMEOUT 2974 if (System.currentTimeMillis() - beginTime > TIME_OUT) { 2975 break; 2976 } 2977 } 2978 assertEquals(LENGTH, totalRead); 2979 2980 // compares contents. 2981 readBuffer.flip(); 2982 for (int i = OFFSET; i < CONTENT_AS_BYTES_LENGTH; i++) { 2983 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 2984 } 2985 } 2986 2987 /** 2988 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 2989 */ 2990 public void test_transferToJJLWritableByteChannel_DatagramChannel() 2991 throws Exception { 2992 // inits data to file. 2993 writeDataToFile(fileOfReadOnlyFileChannel); 2994 2995 // connects two datagramChannel 2996 datagramChannelReceiver = DatagramChannel.open(); 2997 datagramChannelReceiver.socket().bind( 2998 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 2999 datagramChannelSender = DatagramChannel.open(); 3000 datagramChannelSender.socket().bind( 3001 new InetSocketAddress(InetAddress.getLocalHost(), 0)); 3002 datagramChannelSender.socket().setSoTimeout(TIME_OUT); 3003 datagramChannelSender.connect(datagramChannelReceiver.socket() 3004 .getLocalSocketAddress()); 3005 datagramChannelReceiver.socket().setSoTimeout(TIME_OUT); 3006 datagramChannelReceiver.connect(datagramChannelSender.socket() 3007 .getLocalSocketAddress()); 3008 3009 // transfers data from fileChannel to datagramChannelSender 3010 long result = readOnlyFileChannel.transferTo(0, 3011 CONTENT_AS_BYTES_LENGTH, datagramChannelSender); 3012 assertEquals(CONTENT_AS_BYTES_LENGTH, result); 3013 assertEquals(0, readOnlyFileChannel.position()); 3014 readOnlyFileChannel.close(); 3015 datagramChannelSender.close(); 3016 3017 // gets contents from datagramChannelReceiver 3018 ByteBuffer readBuffer = ByteBuffer.allocate(CONTENT_AS_BYTES_LENGTH); 3019 long beginTime = System.currentTimeMillis(); 3020 int totalRead = 0; 3021 while (totalRead < CONTENT_AS_BYTES_LENGTH) { 3022 totalRead += datagramChannelReceiver.read(readBuffer); 3023 if (System.currentTimeMillis() - beginTime > TIME_OUT) { 3024 break; 3025 } 3026 } 3027 assertEquals(CONTENT_AS_BYTES_LENGTH, totalRead); 3028 3029 // compares contents. 3030 readBuffer.flip(); 3031 for (int i = 0; i < CONTENT_AS_BYTES_LENGTH; i++) { 3032 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 3033 } 3034 } 3035 3036 /** 3037 * @tests java.nio.channels.FileChannel#transferTo(long,long,WritableByteChannel) 3038 */ 3039 public void test_transferToJJLWritableByteChannel_Pipe() throws Exception { 3040 // inits data in file. 3041 writeDataToFile(fileOfReadOnlyFileChannel); 3042 3043 // inits pipe. 3044 pipe = Pipe.open(); 3045 3046 // transfers data from fileChannel to pipe. 3047 final int OFFSET = 2; 3048 final int LENGTH = 4; 3049 long result = readOnlyFileChannel.transferTo(OFFSET, LENGTH, pipe 3050 .sink()); 3051 assertEquals(LENGTH, result); 3052 assertEquals(0, readOnlyFileChannel.position()); 3053 readOnlyFileChannel.close(); 3054 3055 // gets content from pipe. 3056 ByteBuffer readBuffer = ByteBuffer.allocate(LENGTH); 3057 result = pipe.source().read(readBuffer); 3058 assertEquals(LENGTH, result); 3059 3060 // compares content. 3061 readBuffer.flip(); 3062 for (int i = OFFSET; i < OFFSET + LENGTH; i++) { 3063 assertEquals(CONTENT_AS_BYTES[i], readBuffer.get()); 3064 } 3065 } 3066 3067 /** 3068 * Regression test for Harmony-3324 3069 * Make sure we could delete the file after we called transferTo() method. 3070 */ 3071 public void test_transferTo_couldDelete() throws Exception { 3072 // init data in files 3073 writeDataToFile(fileOfReadOnlyFileChannel); 3074 writeDataToFile(fileOfWriteOnlyFileChannel); 3075 3076 // call transferTo() method 3077 readOnlyFileChannel.transferTo(0 , 2, writeOnlyFileChannel); 3078 3079 // delete both files 3080 readOnlyFileChannel.close(); 3081 writeOnlyFileChannel.close(); 3082 boolean rDel = fileOfReadOnlyFileChannel.delete(); 3083 boolean wDel = fileOfWriteOnlyFileChannel.delete(); 3084 3085 // make sure both files were deleted 3086 assertTrue("File " + readOnlyFileChannel + " exists", rDel); 3087 assertTrue("File " + writeOnlyFileChannel + " exists", wDel); 3088 } 3089 3090 /** 3091 * Regression test for Harmony-3324 3092 * Make sure we could delete the file after we called transferFrom() method. 3093 */ 3094 public void test_transferFrom_couldDelete() throws Exception { 3095 // init data in files 3096 writeDataToFile(fileOfReadOnlyFileChannel); 3097 writeDataToFile(fileOfWriteOnlyFileChannel); 3098 3099 // call transferTo() method 3100 writeOnlyFileChannel.transferFrom(readOnlyFileChannel, 0 , 2); 3101 3102 // delete both files 3103 readOnlyFileChannel.close(); 3104 writeOnlyFileChannel.close(); 3105 boolean rDel = fileOfReadOnlyFileChannel.delete(); 3106 boolean wDel = fileOfWriteOnlyFileChannel.delete(); 3107 3108 // make sure both files were deleted 3109 assertTrue("File " + readOnlyFileChannel + " exists", rDel); 3110 assertTrue("File " + writeOnlyFileChannel + " exists", wDel); 3111 } 3112 3113 private class MockFileChannel extends FileChannel { 3114 3115 private boolean isLockCalled = false; 3116 3117 private boolean isTryLockCalled = false; 3118 3119 private boolean isReadCalled = false; 3120 3121 private boolean isWriteCalled = false; 3122 3123 public void force(boolean arg0) throws IOException { 3124 // do nothing 3125 } 3126 3127 public FileLock lock(long position, long size, boolean shared) 3128 throws IOException { 3129 // verify that calling lock() leads to the method 3130 // lock(0, Long.MAX_VALUE, false). 3131 if (0 == position && Long.MAX_VALUE == size && false == shared) { 3132 isLockCalled = true; 3133 } 3134 return null; 3135 } 3136 3137 public MappedByteBuffer map(MapMode arg0, long arg1, long arg2) 3138 throws IOException { 3139 return null; 3140 } 3141 3142 public long position() throws IOException { 3143 return 0; 3144 } 3145 3146 public FileChannel position(long arg0) throws IOException { 3147 return null; 3148 } 3149 3150 public int read(ByteBuffer arg0) throws IOException { 3151 return 0; 3152 } 3153 3154 public int read(ByteBuffer arg0, long arg1) throws IOException { 3155 return 0; 3156 } 3157 3158 public long read(ByteBuffer[] srcs, int offset, int length) 3159 throws IOException { 3160 // verify that calling read(ByteBuffer[] srcs) leads to the method 3161 // read(srcs, 0, srcs.length) 3162 if (0 == offset && length == srcs.length) { 3163 isReadCalled = true; 3164 } 3165 return 0; 3166 } 3167 3168 public long size() throws IOException { 3169 return 0; 3170 } 3171 3172 public long transferFrom(ReadableByteChannel arg0, long arg1, long arg2) 3173 throws IOException { 3174 return 0; 3175 } 3176 3177 public long transferTo(long arg0, long arg1, WritableByteChannel arg2) 3178 throws IOException { 3179 return 0; 3180 } 3181 3182 public FileChannel truncate(long arg0) throws IOException { 3183 return null; 3184 } 3185 3186 public FileLock tryLock(long position, long size, boolean shared) 3187 throws IOException { 3188 // verify that calling tryLock() leads to the method 3189 // tryLock(0, Long.MAX_VALUE, false). 3190 if (0 == position && Long.MAX_VALUE == size && false == shared) { 3191 isTryLockCalled = true; 3192 } 3193 return null; 3194 } 3195 3196 public int write(ByteBuffer arg0) throws IOException { 3197 return 0; 3198 } 3199 3200 public int write(ByteBuffer arg0, long arg1) throws IOException { 3201 return 0; 3202 } 3203 3204 public long write(ByteBuffer[] srcs, int offset, int length) 3205 throws IOException { 3206 // verify that calling write(ByteBuffer[] srcs) leads to the method 3207 // write(srcs, 0, srcs.length) 3208 if(0 == offset && length == srcs.length){ 3209 isWriteCalled = true; 3210 } 3211 return 0; 3212 } 3213 3214 protected void implCloseChannel() throws IOException { 3215 3216 } 3217 } 3218} 3219