173298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes/* 273298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * Copyright (C) 2011 The Android Open Source Project 373298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * 473298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 573298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * you may not use this file except in compliance with the License. 673298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * You may obtain a copy of the License at 773298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * 873298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 973298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * 1073298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * Unless required by applicable law or agreed to in writing, software 1173298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1273298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1373298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * See the License for the specific language governing permissions and 1473298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes * limitations under the License. 1573298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes */ 1673298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes 1773298fc8f21a2440b4848912e95a072713b93a1aElliott Hughespackage libcore.java.io; 1873298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes 1973298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.File; 2073298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.FileDescriptor; 2173298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.FileInputStream; 2273298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.FileNotFoundException; 2373298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.FileOutputStream; 2473298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport java.io.IOException; 2573298fc8f21a2440b4848912e95a072713b93a1aElliott Hughesimport junit.framework.TestCase; 2673298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes 2773298fc8f21a2440b4848912e95a072713b93a1aElliott Hughespublic class FileOutputStreamTest extends TestCase { 2873298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes public void testFileDescriptorOwnership() throws Exception { 2973298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes File tmp = File.createTempFile("FileOutputStreamTest", "tmp"); 3073298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes FileOutputStream fos1 = new FileOutputStream(tmp); 3173298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes FileOutputStream fos2 = new FileOutputStream(fos1.getFD()); 32b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes 33b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes // Close the second FileDescriptor and check we can't use it... 3473298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes fos2.close(); 35b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes try { 36b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fos2.write(1); 37b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fail(); 38b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } catch (IOException expected) { 39b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } 40b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes try { 41b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fos2.write(new byte[1], 0, 1); 42b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fail(); 43b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } catch (IOException expected) { 44b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } 45b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes // ...but that we can still use the first. 4673298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes fos1.write(1); 47b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes 48b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes // Close the first FileDescriptor and check we can't use it... 4973298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes fos1.close(); 5073298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes try { 5173298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes fos1.write(1); 5273298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes fail(); 5373298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes } catch (IOException expected) { 5473298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes } 55b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes try { 56b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fos1.write(new byte[1], 0, 1); 57b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fail(); 58b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } catch (IOException expected) { 59b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes } 6073298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes } 6167e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes 6267e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes public void testClose() throws Exception { 6367e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes FileOutputStream fos = new FileOutputStream(File.createTempFile("FileOutputStreamTest", "tmp")); 6467e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes 6567e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes // Closing an already-closed stream is a no-op... 6667e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fos.close(); 67b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fos.close(); 6867e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes // ...as is flushing... 6967e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fos.flush(); 7067e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes 7167e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes // ...but any explicit write is an error. 7267e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes byte[] bytes = "hello".getBytes(); 7367e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes try { 7467e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fos.write(bytes); 7567e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fail(); 7667e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } catch (IOException expected) { 7767e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } 7867e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes try { 7967e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fos.write(bytes, 0, 2); 8067e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fail(); 8167e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } catch (IOException expected) { 8267e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } 8367e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes try { 8467e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fos.write(42); 8567e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes fail(); 8667e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } catch (IOException expected) { 8767e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } 88b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes 89b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes // ...except a 0-byte write. 90b1f55cb6f95928be969a8fe5c7447e13f14d0a68Elliott Hughes fos.write(new byte[0], 0, 0); 9167e5c528c6c81210e7ede35f7c137050e5c346b1Elliott Hughes } 9273298fc8f21a2440b4848912e95a072713b93a1aElliott Hughes} 93