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