1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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.FileOutputStream;
22import java.nio.ByteBuffer;
23import java.nio.channels.FileChannel;
24import libcore.io.IoUtils;
25
26public class FileChannelTest extends junit.framework.TestCase {
27    public void testReadOnlyByteArrays() throws Exception {
28        ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
29        File tmp = File.createTempFile("FileChannelTest", "tmp");
30
31        // You can't read into a read-only buffer...
32        FileChannel fc = new FileInputStream(tmp).getChannel();
33        try {
34            fc.read(readOnly);
35            fail();
36        } catch (IllegalArgumentException expected) {
37        }
38        try {
39            fc.read(new ByteBuffer[] { readOnly });
40            fail();
41        } catch (IllegalArgumentException expected) {
42        }
43        try {
44            fc.read(new ByteBuffer[] { readOnly }, 0, 1);
45            fail();
46        } catch (IllegalArgumentException expected) {
47        }
48        try {
49            fc.read(readOnly, 0L);
50            fail();
51        } catch (IllegalArgumentException expected) {
52        }
53        fc.close();
54
55
56        // But you can write from a read-only buffer...
57        fc = new FileOutputStream(tmp).getChannel();
58        fc.write(readOnly);
59        fc.write(new ByteBuffer[] { readOnly });
60        fc.write(new ByteBuffer[] { readOnly }, 0, 1);
61        fc.write(readOnly, 0L);
62        fc.close();
63    }
64
65    public void test_readv() throws Exception {
66        File tmp = File.createTempFile("FileChannelTest", "tmp");
67        FileChannel fc = new FileOutputStream(tmp).getChannel();
68        fc.write(ByteBuffer.wrap("abcdABCD".getBytes("US-ASCII")));
69        fc.close();
70        // Check that both direct and non-direct buffers work.
71        fc = new FileInputStream(tmp).getChannel();
72        ByteBuffer[] buffers = new ByteBuffer[] { ByteBuffer.allocateDirect(4), ByteBuffer.allocate(4) };
73        assertEquals(8, fc.read(buffers));
74        fc.close();
75        assertEquals(8, buffers[0].limit() + buffers[1].limit());
76        byte[] bytes = new byte[4];
77        buffers[0].flip();
78        buffers[0].get(bytes);
79        assertEquals("abcd", new String(bytes, "US-ASCII"));
80        buffers[1].flip();
81        buffers[1].get(bytes);
82        assertEquals("ABCD", new String(bytes, "US-ASCII"));
83    }
84
85    public void test_writev() throws Exception {
86        File tmp = File.createTempFile("FileChannelTest", "tmp");
87        FileChannel fc = new FileOutputStream(tmp).getChannel();
88        // Check that both direct and non-direct buffers work.
89        ByteBuffer[] buffers = new ByteBuffer[] { ByteBuffer.allocateDirect(4), ByteBuffer.allocate(4) };
90        buffers[0].put("abcd".getBytes("US-ASCII")).flip();
91        buffers[1].put("ABCD".getBytes("US-ASCII")).flip();
92        assertEquals(8, fc.write(buffers));
93        fc.close();
94        assertEquals(8, tmp.length());
95        assertEquals("abcdABCD", new String(IoUtils.readFileAsString(tmp.getPath())));
96    }
97
98    public void test_append() throws Exception {
99        File tmp = File.createTempFile("FileChannelTest", "tmp");
100        FileOutputStream fos = new FileOutputStream(tmp, true);
101        FileChannel fc = fos.getChannel();
102
103        fc.write(ByteBuffer.wrap("hello".getBytes("US-ASCII")));
104        fc.position(0);
105        // The RI reports whatever position you set...
106        assertEquals(0, fc.position());
107        // ...but writes to the end of the file.
108        fc.write(ByteBuffer.wrap(" world".getBytes("US-ASCII")));
109        fos.close();
110
111        assertEquals("hello world", new String(IoUtils.readFileAsString(tmp.getPath())));
112    }
113}
114