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 libcore.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.nio.ByteBuffer;
27import java.nio.MappedByteBuffer;
28import java.nio.channels.ClosedChannelException;
29import java.nio.channels.DatagramChannel;
30import java.nio.channels.FileChannel;
31import java.nio.channels.FileLock;
32import java.nio.channels.NonWritableChannelException;
33import java.nio.channels.OverlappingFileLockException;
34import java.nio.channels.ReadableByteChannel;
35import java.nio.channels.WritableByteChannel;
36import java.util.Arrays;
37import junit.framework.TestCase;
38
39public final class OldFileChannelTest extends TestCase {
40
41    private static final int CAPACITY = 100;
42
43    private static final String CONTENT = "MYTESTSTRING needs to be a little long";
44
45    private static final byte[] TEST_BYTES;
46
47    static {
48        try {
49            TEST_BYTES = "test".getBytes("iso8859-1");
50        } catch (UnsupportedEncodingException e) {
51            throw new Error(e);
52        }
53    }
54
55    private static final int CONTENT_LENGTH = CONTENT.length();
56
57    private static final byte[] CONTENT_AS_BYTES = CONTENT.getBytes();
58
59    private static final int CONTENT_AS_BYTES_LENGTH = CONTENT_AS_BYTES.length;
60
61    private FileChannel readOnlyFileChannel;
62
63    private FileChannel writeOnlyFileChannel;
64
65    private FileChannel readWriteFileChannel;
66
67    private File fileOfReadOnlyFileChannel;
68
69    private File fileOfWriteOnlyFileChannel;
70
71    private File fileOfReadWriteFileChannel;
72
73    // to read content from FileChannel
74    private FileInputStream fis;
75
76    private FileLock fileLock;
77
78    protected void setUp() throws Exception {
79        fileOfReadOnlyFileChannel = File.createTempFile(
80                "File_of_readOnlyFileChannel", "tmp");
81        fileOfReadOnlyFileChannel.deleteOnExit();
82        fileOfWriteOnlyFileChannel = File.createTempFile(
83                "File_of_writeOnlyFileChannel", "tmp");
84        fileOfWriteOnlyFileChannel.deleteOnExit();
85        fileOfReadWriteFileChannel = File.createTempFile(
86                "File_of_readWriteFileChannel", "tmp");
87        fileOfReadWriteFileChannel.deleteOnExit();
88        fis = null;
89        fileLock = null;
90        readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
91                .getChannel();
92        writeOnlyFileChannel = new FileOutputStream(fileOfWriteOnlyFileChannel)
93                .getChannel();
94        readWriteFileChannel = new RandomAccessFile(fileOfReadWriteFileChannel,
95                "rw").getChannel();
96    }
97
98    protected void tearDown() {
99        if (null != readOnlyFileChannel) {
100            try {
101                readOnlyFileChannel.close();
102            } catch (IOException e) {
103                // do nothing
104            }
105        }
106        if (null != writeOnlyFileChannel) {
107            try {
108                writeOnlyFileChannel.close();
109            } catch (IOException e) {
110                // do nothing
111            }
112        }
113        if (null != readWriteFileChannel) {
114            try {
115                readWriteFileChannel.close();
116            } catch (IOException e) {
117                // do nothing
118            }
119        }
120        if (null != fis) {
121            try {
122                fis.close();
123            } catch (IOException e) {
124                // do nothing
125            }
126        }
127
128        if (null != fileLock) {
129            try {
130                fileLock.release();
131            } catch (IOException e) {
132                // do nothing
133            }
134        }
135
136        if (null != fileOfReadOnlyFileChannel) {
137            fileOfReadOnlyFileChannel.delete();
138        }
139        if (null != fileOfWriteOnlyFileChannel) {
140            fileOfWriteOnlyFileChannel.delete();
141        }
142        if (null != fileOfReadWriteFileChannel) {
143            fileOfReadWriteFileChannel.delete();
144        }
145    }
146
147    public void test_forceZ() throws Exception {
148        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
149        writeOnlyFileChannel.write(writeBuffer);
150        writeOnlyFileChannel.force(true);
151
152        byte[] readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
153        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
154        fis.read(readBuffer);
155        assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
156
157        writeOnlyFileChannel.write(writeBuffer);
158        writeOnlyFileChannel.force(false);
159        fis.close();
160
161        readBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
162        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
163        fis.read(readBuffer);
164        assertTrue(Arrays.equals(CONTENT_AS_BYTES, readBuffer));
165        fis.close();
166    }
167
168
169    /**
170     * Initializes test file.
171     *
172     * @param file
173     * @throws FileNotFoundException
174     * @throws IOException
175     */
176    private void writeDataToFile(File file) throws FileNotFoundException,
177            IOException {
178        FileOutputStream fos = new FileOutputStream(file);
179        try {
180            fos.write(CONTENT_AS_BYTES);
181        } finally {
182            fos.close();
183        }
184    }
185
186    /**
187     * Initializes large test file.
188     *
189     * @param file the file to be written
190     * @param size the content size to be written
191     * @throws FileNotFoundException
192     * @throws IOException
193     */
194    private void writeLargeDataToFile(File file, int size)
195            throws FileNotFoundException, IOException {
196        FileOutputStream fos = new FileOutputStream(file);
197        byte[] buf = new byte[size];
198
199        try {
200            // we don't care about content - just need a particular file size
201            fos.write(buf);
202        } finally {
203            fos.close();
204        }
205    }
206
207    public void test_tryLockJJZ_IllegalArgument() throws Exception {
208        try {
209            writeOnlyFileChannel.tryLock(0, -1, false);
210            fail("should throw IllegalArgumentException");
211        } catch (IllegalArgumentException e) {
212            // expected
213        }
214
215        try {
216            writeOnlyFileChannel.tryLock(-1, 0, false);
217            fail("should throw IllegalArgumentException");
218        } catch (IllegalArgumentException e) {
219            // expected
220        }
221
222        try {
223            readWriteFileChannel.tryLock(-1, -1, false);
224            fail("should throw IllegalArgumentException");
225        } catch (IllegalArgumentException e) {
226            // expected
227        }
228
229        try {
230            readWriteFileChannel.tryLock(Long.MAX_VALUE, 1, false);
231            fail("should throw IllegalArgumentException");
232        } catch (IllegalArgumentException e) {
233            // expected
234        }
235    }
236
237    public void testTryLockVeryLarge() throws IOException {
238        long tooBig = Integer.MAX_VALUE + 1L;
239        FileLock lock = readWriteFileChannel.tryLock(tooBig, 1, false);
240        assertLockFails(tooBig, 1);
241        lock.release();
242
243        lock = readWriteFileChannel.tryLock(0, tooBig, false);
244        assertLockFails(0, 1);
245        lock.release();
246    }
247
248    public void testTryLockOverlapping() throws IOException {
249        FileLock lockOne = readWriteFileChannel.tryLock(0, 10, false);
250        FileLock lockTwo = readWriteFileChannel.tryLock(10, 20, false);
251        assertLockFails(0, 10);
252        lockOne.release();
253        assertLockFails(5, 10);
254        lockOne = readWriteFileChannel.tryLock(0, 10, false);
255        lockTwo.release();
256        lockOne.release();
257    }
258
259    public void test_readLByteBufferJ_IllegalArgument() throws Exception {
260        ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
261
262        try {
263            readOnlyFileChannel.read(readBuffer, -1);
264            fail("should throw IllegalArgumentException");
265        } catch (IllegalArgumentException e) {
266            // expected
267        }
268
269        try {
270            writeOnlyFileChannel.read(readBuffer, -1);
271            fail("should throw IllegalArgumentException");
272        } catch (IllegalArgumentException e) {
273            // expected
274        }
275
276        try {
277            readWriteFileChannel.read(readBuffer, -1);
278            fail("should throw IllegalArgumentException");
279        } catch (IllegalArgumentException e) {
280            // expected
281        }
282
283        // throws IllegalArgumentException first.
284        readOnlyFileChannel.close();
285        try {
286            readOnlyFileChannel.read(readBuffer, -1);
287            fail("should throw IllegalArgumentException");
288        } catch (IllegalArgumentException e) {
289            // expected
290        }
291
292        writeOnlyFileChannel.close();
293        try {
294            writeOnlyFileChannel.read(readBuffer, -1);
295            fail("should throw IllegalArgumentException");
296        } catch (IllegalArgumentException e) {
297            // expected
298        }
299
300        readWriteFileChannel.close();
301        try {
302            readWriteFileChannel.read(readBuffer, -1);
303            fail("should throw IllegalArgumentException");
304        } catch (IllegalArgumentException e) {
305            // expected
306        }
307    }
308
309    public void test_read$LByteBufferII_Null() throws Exception {
310
311        try {
312            readOnlyFileChannel.read(null, 0, 1);
313            fail("should throw NullPointerException");
314        } catch (NullPointerException e) {
315            // expected
316        }
317        try {
318            readOnlyFileChannel.read(null, 0, 3);
319            fail("should throw NullPointerException");
320        } catch (NullPointerException e) {
321            // expected
322        }
323        try {
324            readOnlyFileChannel.read(null, 1, 2);
325            fail("should throw NullPointerException");
326        } catch (NullPointerException e) {
327            // expected
328        }
329        try {
330            readOnlyFileChannel.read(null, 2, 1);
331            fail("should throw NullPointerException");
332        } catch (NullPointerException e) {
333            // expected
334        }
335        try {
336            readOnlyFileChannel.read(null, 3, 0);
337            fail("should throw NullPointerException");
338        } catch (NullPointerException e) {
339            // expected
340        }
341
342        try {
343            writeOnlyFileChannel.read(null, 0, 1);
344            fail("should throw NullPointerException");
345        } catch (NullPointerException e) {
346            // expected
347        }
348        try {
349            writeOnlyFileChannel.read(null, 0, 3);
350            fail("should throw NullPointerException");
351        } catch (NullPointerException e) {
352            // expected
353        }
354        try {
355            writeOnlyFileChannel.read(null, 1, 2);
356            fail("should throw NullPointerException");
357        } catch (NullPointerException e) {
358            // expected
359        }
360        try {
361            writeOnlyFileChannel.read(null, 2, 1);
362            fail("should throw NullPointerException");
363        } catch (NullPointerException e) {
364            // expected
365        }
366        try {
367            writeOnlyFileChannel.read(null, 3, 0);
368            fail("should throw NullPointerException");
369        } catch (NullPointerException e) {
370            // expected
371        }
372
373        try {
374            readWriteFileChannel.read(null, 0, 1);
375            fail("should throw NullPointerException");
376        } catch (NullPointerException e) {
377            // expected
378        }
379        try {
380            readWriteFileChannel.read(null, 0, 3);
381            fail("should throw NullPointerException");
382        } catch (NullPointerException e) {
383            // expected
384        }
385        try {
386            readWriteFileChannel.read(null, 1, 2);
387            fail("should throw NullPointerException");
388        } catch (NullPointerException e) {
389            // expected
390        }
391        try {
392            readWriteFileChannel.read(null, 2, 1);
393            fail("should throw NullPointerException");
394        } catch (NullPointerException e) {
395            // expected
396        }
397        try {
398            readWriteFileChannel.read(null, 3, 0);
399            fail("should throw NullPointerException");
400        } catch (NullPointerException e) {
401            // expected
402        }
403
404        // first throws NullPointerException
405        readOnlyFileChannel.close();
406        try {
407            readOnlyFileChannel.read(null, 0, 1);
408            fail("should throw NullPointerException");
409        } catch (NullPointerException e) {
410            // expected
411        }
412        try {
413            readOnlyFileChannel.read(null, 0, 3);
414            fail("should throw NullPointerException");
415        } catch (NullPointerException e) {
416            // expected
417        }
418        try {
419            readOnlyFileChannel.read(null, 1, 2);
420            fail("should throw NullPointerException");
421        } catch (NullPointerException e) {
422            // expected
423        }
424        try {
425            readOnlyFileChannel.read(null, 2, 1);
426            fail("should throw NullPointerException");
427        } catch (NullPointerException e) {
428            // expected
429        }
430        try {
431            readOnlyFileChannel.read(null, 3, 0);
432            fail("should throw NullPointerException");
433        } catch (NullPointerException e) {
434            // expected
435        }
436
437        readWriteFileChannel.close();
438        try {
439            readWriteFileChannel.read(null, 0, 1);
440            fail("should throw NullPointerException");
441        } catch (NullPointerException e) {
442            // expected
443        }
444        try {
445            readWriteFileChannel.read(null, 0, 3);
446            fail("should throw NullPointerException");
447        } catch (NullPointerException e) {
448            // expected
449        }
450        try {
451            readWriteFileChannel.read(null, 1, 2);
452            fail("should throw NullPointerException");
453        } catch (NullPointerException e) {
454            // expected
455        }
456        try {
457            readWriteFileChannel.read(null, 2, 1);
458            fail("should throw NullPointerException");
459        } catch (NullPointerException e) {
460            // expected
461        }
462        try {
463            readWriteFileChannel.read(null, 3, 0);
464            fail("should throw NullPointerException");
465        } catch (NullPointerException e) {
466            // expected
467        }
468
469        writeOnlyFileChannel.close();
470        try {
471            writeOnlyFileChannel.read(null, 0, 1);
472            fail("should throw NullPointerException");
473        } catch (NullPointerException e) {
474            // expected
475        }
476        try {
477            writeOnlyFileChannel.read(null, 0, 3);
478            fail("should throw NullPointerException");
479        } catch (NullPointerException e) {
480            // expected
481        }
482        try {
483            writeOnlyFileChannel.read(null, 1, 2);
484            fail("should throw NullPointerException");
485        } catch (NullPointerException e) {
486            // expected
487        }
488        try {
489            writeOnlyFileChannel.read(null, 2, 1);
490            fail("should throw NullPointerException");
491        } catch (NullPointerException e) {
492            // expected
493        }
494        try {
495            writeOnlyFileChannel.read(null, 3, 0);
496            fail("should throw NullPointerException");
497        } catch (NullPointerException e) {
498            // expected
499        }
500    }
501
502
503    private void doTestForIOOBException(FileChannel channel,
504            ByteBuffer[] buffer) throws IOException{
505        try {
506            channel.read(buffer, -1, 0);
507            fail("should throw IndexOutOfBoundException");
508        } catch (IndexOutOfBoundsException e) {
509            // expected
510        }
511        try {
512            channel.read(buffer, 0, -1);
513            fail("should throw IndexOutOfBoundException");
514        } catch (IndexOutOfBoundsException e) {
515            // expected
516        }
517        try {
518            channel.read(buffer, 0, 3);
519            fail("should throw IndexOutOfBoundException");
520        } catch (IndexOutOfBoundsException e) {
521            // expected
522        }
523        try {
524            channel.read(buffer, 1, 2);
525            fail("should throw IndexOutOfBoundException");
526        } catch (IndexOutOfBoundsException e) {
527            // expected
528        }
529        try {
530            channel.read(buffer, 2, 1);
531            fail("should throw IndexOutOfBoundException");
532        } catch (IndexOutOfBoundsException e) {
533            // expected
534        }
535        try {
536            channel.read(buffer, 3, 0);
537            fail("should throw IndexOutOfBoundException");
538        } catch (IndexOutOfBoundsException e) {
539            // expected
540        }
541    }
542
543    public void test_read$LByteBufferII_IndexOutOfBound() throws Exception {
544        ByteBuffer[] readBuffers = new ByteBuffer[2];
545        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
546        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
547        ByteBuffer[] readBuffersNull = new ByteBuffer[2];
548
549        doTestForIOOBException(readOnlyFileChannel, readBuffers);
550        doTestForIOOBException(readWriteFileChannel, readBuffers);
551        doTestForIOOBException(writeOnlyFileChannel, readBuffers);
552
553        doTestForIOOBException(readOnlyFileChannel, readBuffersNull);
554        doTestForIOOBException(readWriteFileChannel, readBuffersNull);
555        doTestForIOOBException(writeOnlyFileChannel, readBuffersNull);
556        try {
557            readOnlyFileChannel.read(null, -1, 0);
558            fail("should throw IndexOutOfBoundException");
559        } catch (NullPointerException expected) {
560        } catch (IndexOutOfBoundsException expected) {
561        }
562        try {
563            readOnlyFileChannel.read(null, 0, -1);
564            fail("should throw IndexOutOfBoundException");
565        } catch (NullPointerException expected) {
566        } catch (IndexOutOfBoundsException expected) {
567        }
568
569        try {
570            readWriteFileChannel.read(null, -1, 0);
571            fail("should throw IndexOutOfBoundException");
572        } catch (NullPointerException expected) {
573        } catch (IndexOutOfBoundsException expected) {
574        }
575        try {
576            readWriteFileChannel.read(null, 0, -1);
577            fail("should throw IndexOutOfBoundException");
578        } catch (NullPointerException expected) {
579        } catch (IndexOutOfBoundsException expected) {
580        }
581
582        try {
583            writeOnlyFileChannel.read(null, -1, 0);
584            fail("should throw IndexOutOfBoundException");
585        } catch (NullPointerException expected) {
586        } catch (IndexOutOfBoundsException expected) {
587        }
588        try {
589            writeOnlyFileChannel.read(null, 0, -1);
590            fail("should throw IndexOutOfBoundException");
591        } catch (NullPointerException expected) {
592        } catch (IndexOutOfBoundsException expected) {
593        }
594
595        readOnlyFileChannel.close();
596        doTestForIOOBException(readOnlyFileChannel, readBuffers);
597        doTestForIOOBException(readOnlyFileChannel, readBuffersNull);
598
599        readWriteFileChannel.close();
600        doTestForIOOBException(readWriteFileChannel, readBuffers);
601        doTestForIOOBException(readWriteFileChannel, readBuffersNull);
602
603        writeOnlyFileChannel.close();
604        doTestForIOOBException(writeOnlyFileChannel, readBuffers);
605        doTestForIOOBException(writeOnlyFileChannel, readBuffersNull);
606    }
607
608    public void test_read$LByteBufferII_EmptyFile() throws Exception {
609        ByteBuffer[] readBuffers = new ByteBuffer[2];
610        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
611        readBuffers[1] = ByteBuffer.allocate(CAPACITY);
612        long result = readOnlyFileChannel.read(readBuffers, 0, 2);
613        assertEquals(-1, result);
614        assertEquals(0, readBuffers[0].position());
615        assertEquals(0, readBuffers[1].position());
616    }
617
618    public void test_read$LByteBufferII_EmptyBuffers() throws Exception {
619        ByteBuffer[] readBuffers = new ByteBuffer[2];
620        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
621
622        try {
623            readOnlyFileChannel.read(readBuffers, 0, 2);
624        } catch (NullPointerException e) {
625            // expected
626        }
627
628        writeDataToFile(fileOfReadOnlyFileChannel);
629        readBuffers[0] = ByteBuffer.allocate(CAPACITY);
630        try {
631            readOnlyFileChannel.read(readBuffers, 0, 2);
632        } catch (NullPointerException e) {
633            // expected
634        }
635
636        long result = readOnlyFileChannel.read(readBuffers, 0, 1);
637        assertEquals(CONTENT_AS_BYTES_LENGTH, result);
638    }
639
640    public void test_isOpen() throws Exception {
641        // Regression for HARMONY-40
642        File logFile = File.createTempFile("out", "tmp");
643        logFile.deleteOnExit();
644        FileOutputStream out = new FileOutputStream(logFile, true);
645        FileChannel channel = out.getChannel();
646        out.write(1);
647        assertTrue("Assert 0: Channel is not open", channel.isOpen());
648        out.close();
649        assertFalse("Assert 0: Channel is still open", channel.isOpen());
650    }
651
652    public void test_writeLByteBuffer_Closed() throws Exception {
653        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
654
655        readOnlyFileChannel.close();
656        try {
657            readOnlyFileChannel.write(writeBuffer);
658            fail("should throw ClosedChannelException");
659        } catch (ClosedChannelException e) {
660            // expected
661        }
662
663        writeOnlyFileChannel.close();
664        try {
665            writeOnlyFileChannel.write(writeBuffer);
666            fail("should throw ClosedChannelException");
667        } catch (ClosedChannelException e) {
668            // expected
669        }
670
671        readWriteFileChannel.close();
672        try {
673            readWriteFileChannel.write(writeBuffer);
674            fail("should throw ClosedChannelException");
675        } catch (ClosedChannelException e) {
676            // expected
677        }
678
679        // should throw ClosedChannelException first
680        try {
681            readWriteFileChannel.read((ByteBuffer) null);
682            fail("should throw ClosedChannelException");
683        } catch (NullPointerException e) {
684        } catch (ClosedChannelException e) {
685        }
686
687        try {
688            readOnlyFileChannel.write((ByteBuffer) null);
689            fail("should throw ClosedChannelException");
690        } catch (NullPointerException e) {
691        } catch (ClosedChannelException e) {
692        }
693
694        writeOnlyFileChannel.close();
695        try {
696            writeOnlyFileChannel.write((ByteBuffer) null);
697            fail("should throw ClosedChannelException");
698        } catch (NullPointerException e) {
699        } catch (ClosedChannelException e) {
700        }
701    }
702
703    public void test_writeLByteBufferJ_Postion_As_Long() throws Exception {
704        ByteBuffer writeBuffer = ByteBuffer.wrap(TEST_BYTES);
705        try {
706            writeOnlyFileChannel.write(writeBuffer, Long.MAX_VALUE);
707        } catch (IOException e) {
708            // expected
709        }
710    }
711
712    public void test_writeLByteBufferJ_IllegalArgument() throws Exception {
713        ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
714
715        try {
716            readOnlyFileChannel.write(writeBuffer, -1);
717            fail("should throw IllegalArgumentException");
718        } catch (IllegalArgumentException e) {
719            // expected
720        }
721
722        try {
723            writeOnlyFileChannel.write(writeBuffer, -1);
724            fail("should throw IllegalArgumentException");
725        } catch (IllegalArgumentException e) {
726            // expected
727        }
728
729        try {
730            readWriteFileChannel.write(writeBuffer, -1);
731            fail("should throw IllegalArgumentException");
732        } catch (IllegalArgumentException e) {
733            // expected
734        }
735
736        // throws IllegalArgumentException first.
737        readOnlyFileChannel.close();
738        try {
739            readOnlyFileChannel.write(writeBuffer, -1);
740            fail("should throw IllegalArgumentException");
741        } catch (IllegalArgumentException e) {
742            // expected
743        }
744
745        writeOnlyFileChannel.close();
746        try {
747            writeOnlyFileChannel.write(writeBuffer, -1);
748            fail("should throw IllegalArgumentException");
749        } catch (IllegalArgumentException e) {
750            // expected
751        }
752
753        readWriteFileChannel.close();
754        try {
755            readWriteFileChannel.write(writeBuffer, -1);
756            fail("should throw IllegalArgumentException");
757        } catch (IllegalArgumentException e) {
758            // expected
759        }
760    }
761
762    public void test_writeLByteBufferJ_NonZeroPosition() throws Exception {
763        final int pos = 5;
764        ByteBuffer writeBuffer = ByteBuffer.wrap(CONTENT_AS_BYTES);
765        writeBuffer.position(pos);
766        int result = writeOnlyFileChannel.write(writeBuffer, pos);
767        assertEquals(CONTENT_AS_BYTES_LENGTH - pos, result);
768        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffer.position());
769        writeOnlyFileChannel.close();
770
771        assertEquals(CONTENT_AS_BYTES_LENGTH, fileOfWriteOnlyFileChannel
772                .length());
773
774        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
775        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH - pos];
776        fis.skip(pos);
777        fis.read(inputBuffer);
778        String test = CONTENT.substring(pos);
779        assertTrue(Arrays.equals(test.getBytes(), inputBuffer));
780    }
781
782    public void test_write$LByteBuffer_Closed() throws Exception {
783        ByteBuffer[] writeBuffers = new ByteBuffer[2];
784        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
785        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
786
787        readOnlyFileChannel.close();
788        try {
789            readOnlyFileChannel.write(writeBuffers);
790            fail("should throw ClosedChannelException");
791        } catch (ClosedChannelException e) {
792            // expected
793        }
794
795        writeOnlyFileChannel.close();
796        try {
797            writeOnlyFileChannel.write(writeBuffers);
798            fail("should throw ClosedChannelException");
799        } catch (ClosedChannelException e) {
800            // expected
801        }
802
803        readWriteFileChannel.close();
804        try {
805            readWriteFileChannel.write(writeBuffers);
806            fail("should throw ClosedChannelException");
807        } catch (ClosedChannelException e) {
808            // expected
809        }
810    }
811
812    public void test_write$LByteBuffer_ReadOnly() throws Exception {
813        ByteBuffer[] writeBuffers = new ByteBuffer[2];
814        writeBuffers[0] = ByteBuffer.allocate(CAPACITY);
815        writeBuffers[1] = ByteBuffer.allocate(CAPACITY);
816
817        try {
818            readOnlyFileChannel.write(writeBuffers);
819            fail("should throw NonWritableChannelException");
820        } catch (NonWritableChannelException e) {
821            // expected
822        }
823    }
824
825    public void test_write$LByteBuffer_EmptyBuffers() throws Exception {
826        ByteBuffer[] writeBuffers = new ByteBuffer[2];
827        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
828        try {
829            writeOnlyFileChannel.write(writeBuffers);
830            fail("should throw NullPointerException");
831        } catch (NullPointerException e) {
832            // expected
833        }
834
835        try {
836            readWriteFileChannel.write(writeBuffers);
837            fail("should throw NullPointerException");
838        } catch (NullPointerException e) {
839            // expected
840        }
841    }
842
843    public void test_write$LByteBuffer() throws Exception {
844        ByteBuffer[] writeBuffers = new ByteBuffer[2];
845        writeBuffers[0] = ByteBuffer.wrap(CONTENT_AS_BYTES);
846        writeBuffers[1] = ByteBuffer.wrap(CONTENT_AS_BYTES);
847
848        long result = writeOnlyFileChannel.write(writeBuffers);
849        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, result);
850        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[0].position());
851        assertEquals(CONTENT_AS_BYTES_LENGTH, writeBuffers[1].position());
852        writeOnlyFileChannel.close();
853
854        assertEquals(CONTENT_AS_BYTES_LENGTH * 2, fileOfWriteOnlyFileChannel
855                .length());
856
857        fis = new FileInputStream(fileOfWriteOnlyFileChannel);
858        byte[] inputBuffer = new byte[CONTENT_AS_BYTES_LENGTH];
859        fis.read(inputBuffer);
860        byte[] expectedResult = new byte[CONTENT_AS_BYTES_LENGTH * 2];
861        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult, 0,
862                CONTENT_AS_BYTES_LENGTH);
863        System.arraycopy(CONTENT_AS_BYTES, 0, expectedResult,
864                CONTENT_AS_BYTES_LENGTH, CONTENT_AS_BYTES_LENGTH);
865        assertTrue(Arrays.equals(CONTENT_AS_BYTES, inputBuffer));
866    }
867
868    public void test_write$LByteBufferII_Null() throws Exception {
869        ByteBuffer[] writeBuffers = null;
870
871        try {
872            readOnlyFileChannel.write(writeBuffers, 1, 2);
873            fail("should throw NullPointerException");
874        } catch (NullPointerException e) {
875            // expected
876        }
877
878        try {
879            writeOnlyFileChannel.write(writeBuffers, 1, 2);
880            fail("should throw NullPointerException");
881        } catch (NullPointerException e) {
882            // expected
883        }
884
885        try {
886            readWriteFileChannel.write(writeBuffers, 1, 2);
887            fail("should throw NullPointerException");
888        } catch (NullPointerException e) {
889            // expected
890        }
891
892        // first throws NullPointerException
893        readOnlyFileChannel.close();
894        try {
895            readOnlyFileChannel.write(writeBuffers, 1, 2);
896            fail("should throw NullPointerException");
897        } catch (NullPointerException e) {
898            // expected
899        }
900
901        writeOnlyFileChannel.close();
902        try {
903            writeOnlyFileChannel.write(writeBuffers, 1, 2);
904            fail("should throw NullPointerException");
905        } catch (NullPointerException e) {
906            // expected
907        }
908
909        readWriteFileChannel.close();
910        try {
911            readWriteFileChannel.write(writeBuffers, 1, 2);
912            fail("should throw NullPointerException");
913        } catch (NullPointerException e) {
914            // expected
915        }
916    }
917
918    public void test_write$LByteBufferII_IndexOutOfBound() throws Exception {
919        ByteBuffer[] writeBuffers = new ByteBuffer[2];
920        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
921        writeBuffers[1] = ByteBuffer.allocate(this.CONTENT_LENGTH);
922
923        try {
924            writeOnlyFileChannel.write(writeBuffers, -1, 0);
925            fail("should throw IndexOutOfBoundsException");
926        } catch (IndexOutOfBoundsException e) {
927            // expected
928        }
929        try {
930            writeOnlyFileChannel.write(writeBuffers, 0, -1);
931            fail("should throw IndexOutOfBoundsException");
932        } catch (IndexOutOfBoundsException e) {
933            // expected
934        }
935        try {
936            writeOnlyFileChannel.write(writeBuffers, 0, 3);
937            fail("should throw IndexOutOfBoundsException");
938        } catch (IndexOutOfBoundsException e) {
939            // expected
940        }
941        try {
942            writeOnlyFileChannel.write(writeBuffers, 1, 2);
943            fail("should throw IndexOutOfBoundsException");
944        } catch (IndexOutOfBoundsException e) {
945            // expected
946        }
947        try {
948            writeOnlyFileChannel.write(writeBuffers, 2, 1);
949            fail("should throw IndexOutOfBoundsException");
950        } catch (IndexOutOfBoundsException e) {
951            // expected
952        }
953        try {
954            writeOnlyFileChannel.write(writeBuffers, 3, 0);
955            fail("should throw IndexOutOfBoundsException");
956        } catch (IndexOutOfBoundsException e) {
957            // expected
958        }
959
960        try {
961            readWriteFileChannel.write(writeBuffers, -1, 0);
962            fail("should throw IndexOutOfBoundsException");
963        } catch (IndexOutOfBoundsException e) {
964            // expected
965        }
966        try {
967            readWriteFileChannel.write(writeBuffers, 0, -1);
968            fail("should throw IndexOutOfBoundsException");
969        } catch (IndexOutOfBoundsException e) {
970            // expected
971        }
972        try {
973            readWriteFileChannel.write(writeBuffers, 0, 3);
974            fail("should throw IndexOutOfBoundsException");
975        } catch (IndexOutOfBoundsException e) {
976            // expected
977        }
978        try {
979            readWriteFileChannel.write(writeBuffers, 1, 2);
980            fail("should throw IndexOutOfBoundsException");
981        } catch (IndexOutOfBoundsException e) {
982            // expected
983        }
984        try {
985            readWriteFileChannel.write(writeBuffers, 2, 1);
986            fail("should throw IndexOutOfBoundsException");
987        } catch (IndexOutOfBoundsException e) {
988            // expected
989        }
990        try {
991            readWriteFileChannel.write(writeBuffers, 3, 0);
992            fail("should throw IndexOutOfBoundsException");
993        } catch (IndexOutOfBoundsException e) {
994            // expected
995        }
996
997        try {
998            readOnlyFileChannel.write(writeBuffers, -1, 0);
999            fail("should throw IndexOutOfBoundsException");
1000        } catch (IndexOutOfBoundsException e) {
1001            // expected
1002        }
1003        try {
1004            readOnlyFileChannel.write(writeBuffers, 0, -1);
1005            fail("should throw IndexOutOfBoundsException");
1006        } catch (IndexOutOfBoundsException e) {
1007            // expected
1008        }
1009        try {
1010            readOnlyFileChannel.write(writeBuffers, 0, 3);
1011            fail("should throw IndexOutOfBoundsException");
1012        } catch (IndexOutOfBoundsException e) {
1013            // expected
1014        }
1015        try {
1016            readOnlyFileChannel.write(writeBuffers, 1, 2);
1017            fail("should throw IndexOutOfBoundsException");
1018        } catch (IndexOutOfBoundsException e) {
1019            // expected
1020        }
1021        try {
1022            readOnlyFileChannel.write(writeBuffers, 2, 1);
1023            fail("should throw IndexOutOfBoundsException");
1024        } catch (IndexOutOfBoundsException e) {
1025            // expected
1026        }
1027        try {
1028            readOnlyFileChannel.write(writeBuffers, 3, 0);
1029            fail("should throw IndexOutOfBoundsException");
1030        } catch (IndexOutOfBoundsException e) {
1031            // expected
1032        }
1033    }
1034
1035    public void test_write$LByteBufferII_EmptyBuffers() throws Exception {
1036        ByteBuffer[] writeBuffers = new ByteBuffer[2];
1037        writeBuffers[0] = ByteBuffer.allocate(this.CONTENT_LENGTH);
1038        try {
1039            writeOnlyFileChannel.write(writeBuffers, 0, 2);
1040            fail("should throw NullPointerException");
1041        } catch (NullPointerException e) {
1042            // expected
1043        }
1044
1045        try {
1046            readWriteFileChannel.write(writeBuffers, 0, 2);
1047            fail("should throw NullPointerException");
1048        } catch (NullPointerException e) {
1049            // expected
1050        }
1051    }
1052
1053    public void test_transferToJJLWritableByteChannel_IllegalArgument()
1054            throws Exception {
1055        WritableByteChannel writableByteChannel = DatagramChannel.open();
1056        try {
1057            readOnlyFileChannel.transferTo(10, -1, writableByteChannel);
1058            fail("should throw IllegalArgumentException.");
1059        } catch (IllegalArgumentException e) {
1060            // expected
1061        }
1062
1063        try {
1064            readWriteFileChannel.transferTo(-1, 10, writableByteChannel);
1065            fail("should throw IllegalArgumentException.");
1066        } catch (IllegalArgumentException e) {
1067            // expected
1068        }
1069    }
1070
1071    private void assertLockFails(long position, long size) throws IOException {
1072        try {
1073            readWriteFileChannel.tryLock(position, size, false);
1074            fail();
1075        } catch (OverlappingFileLockException expected) {
1076        }
1077    }
1078}
1079