12620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes/*
22620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * Copyright (C) 2010 The Android Open Source Project
3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
42620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * you may not use this file except in compliance with the License.
62620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * You may obtain a copy of the License at
7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
82620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
102620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * Unless required by applicable law or agreed to in writing, software
112620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * See the License for the specific language governing permissions and
142620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes * limitations under the License.
152620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes */
162620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes
174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.nio.channels;
182620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes
192620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughesimport java.io.File;
202620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughesimport java.io.FileInputStream;
214af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughesimport java.io.FileOutputStream;
222620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughesimport java.nio.ByteBuffer;
234557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.nio.channels.FileChannel;
24bbac92e691de7d570928ddfba639067978e55b06Elliott Hughesimport libcore.io.IoUtils;
252620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes
262620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughespublic class FileChannelTest extends junit.framework.TestCase {
27bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public void testReadOnlyByteArrays() throws Exception {
282620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        ByteBuffer readOnly = ByteBuffer.allocate(1).asReadOnlyBuffer();
294af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        File tmp = File.createTempFile("FileChannelTest", "tmp");
304af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes
314af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        // You can't read into a read-only buffer...
322620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        FileChannel fc = new FileInputStream(tmp).getChannel();
332620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        try {
342620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fc.read(readOnly);
352620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fail();
362620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        } catch (IllegalArgumentException expected) {
372620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        }
382620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        try {
392620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fc.read(new ByteBuffer[] { readOnly });
402620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fail();
412620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        } catch (IllegalArgumentException expected) {
422620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        }
432620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        try {
442620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fc.read(new ByteBuffer[] { readOnly }, 0, 1);
452620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fail();
462620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        } catch (IllegalArgumentException expected) {
472620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        }
482620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        try {
492620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fc.read(readOnly, 0L);
502620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            fail();
512620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        } catch (IllegalArgumentException expected) {
522620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes        }
53bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.close();
54bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
554af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes
564af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        // But you can write from a read-only buffer...
574af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        fc = new FileOutputStream(tmp).getChannel();
584af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        fc.write(readOnly);
594af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        fc.write(new ByteBuffer[] { readOnly });
604af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        fc.write(new ByteBuffer[] { readOnly }, 0, 1);
614af0d8c99d68bcacff182699527d983a1d34fdbeElliott Hughes        fc.write(readOnly, 0L);
62bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.close();
63bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
64bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
65bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public void test_readv() throws Exception {
66bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        File tmp = File.createTempFile("FileChannelTest", "tmp");
67bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        FileChannel fc = new FileOutputStream(tmp).getChannel();
68bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.write(ByteBuffer.wrap("abcdABCD".getBytes("US-ASCII")));
69bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.close();
70bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // Check that both direct and non-direct buffers work.
71bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc = new FileInputStream(tmp).getChannel();
72bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ByteBuffer[] buffers = new ByteBuffer[] { ByteBuffer.allocateDirect(4), ByteBuffer.allocate(4) };
73bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals(8, fc.read(buffers));
74bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.close();
75bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals(8, buffers[0].limit() + buffers[1].limit());
76bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        byte[] bytes = new byte[4];
77bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[0].flip();
78bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[0].get(bytes);
79bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals("abcd", new String(bytes, "US-ASCII"));
80bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[1].flip();
81bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[1].get(bytes);
82bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals("ABCD", new String(bytes, "US-ASCII"));
83bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
84bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes
85bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public void test_writev() throws Exception {
86bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        File tmp = File.createTempFile("FileChannelTest", "tmp");
87bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        FileChannel fc = new FileOutputStream(tmp).getChannel();
88bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        // Check that both direct and non-direct buffers work.
89bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ByteBuffer[] buffers = new ByteBuffer[] { ByteBuffer.allocateDirect(4), ByteBuffer.allocate(4) };
90bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[0].put("abcd".getBytes("US-ASCII")).flip();
91bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        buffers[1].put("ABCD".getBytes("US-ASCII")).flip();
92bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals(8, fc.write(buffers));
93bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        fc.close();
94bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        assertEquals(8, tmp.length());
95dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        assertEquals("abcdABCD", new String(IoUtils.readFileAsString(tmp.getPath())));
96dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes    }
97dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes
98dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes    public void test_append() throws Exception {
99dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        File tmp = File.createTempFile("FileChannelTest", "tmp");
100dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        FileOutputStream fos = new FileOutputStream(tmp, true);
101dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        FileChannel fc = fos.getChannel();
102dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes
103dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        fc.write(ByteBuffer.wrap("hello".getBytes("US-ASCII")));
104dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        fc.position(0);
105dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        // The RI reports whatever position you set...
106dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        assertEquals(0, fc.position());
107dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        // ...but writes to the end of the file.
108dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        fc.write(ByteBuffer.wrap(" world".getBytes("US-ASCII")));
109dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        fos.close();
110dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes
111dbde5bd893290c02bb289460fc61e48eda63daa2Elliott Hughes        assertEquals("hello world", new String(IoUtils.readFileAsString(tmp.getPath())));
1122620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    }
1132620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes}
114