1 /* 2 * Copyright (C) 2009 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 package android.content.res.cts; 17 18 import java.io.File; 19 import java.io.FileInputStream; 20 import java.io.IOException; 21 22 import android.content.res.AssetFileDescriptor; 23 import android.os.ParcelFileDescriptor; 24 import android.test.AndroidTestCase; 25 26 public class AssetFileDescriptor_AutoCloseOutputStreamTest extends AndroidTestCase { 27 28 private static final String FILE_NAME = "testAssertFileDescriptorAutoCloseOutputStream"; 29 private static final int FILE_LENGTH = 100; 30 private static final int FILE_END = -1; 31 private static final byte[] FILE_DATA = new byte[] { 32 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 33 }; 34 35 private AssetFileDescriptor mAssetFileDes; 36 37 @Override 38 protected void tearDown() throws Exception { 39 super.tearDown(); 40 // As {@link AssetFileDescripter#createOutputStream()} 41 // and {@link AssetFileDescripter#createInputStream()} doc, 42 // the input and output stream will be auto closed when the AssetFileDescriptor closed. 43 // Here is no need to close AutoCloseOutputStream, as AssetFileDescriptor will do it for us. 44 if(mAssetFileDes != null) { 45 mAssetFileDes.close(); 46 } 47 getContext().deleteFile(FILE_NAME); 48 } 49 50 /* 51 * Test AutoCloseOutputStream life circle. 52 * 1. Write file data into test file. 53 */ 54 public void testAutoCloseOutputStream() throws IOException { 55 File file = new File(getContext().getFilesDir(), FILE_NAME); 56 file.createNewFile(); 57 ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, 58 ParcelFileDescriptor.MODE_READ_WRITE); 59 mAssetFileDes = new AssetFileDescriptor(fd, 0, FILE_LENGTH); 60 AssetFileDescriptor.AutoCloseOutputStream outputStream = 61 new AssetFileDescriptor.AutoCloseOutputStream(mAssetFileDes); 62 outputStream.write(FILE_DATA[0]); 63 outputStream.write(FILE_DATA, 1, 5); 64 outputStream.write(FILE_DATA); 65 outputStream.flush(); 66 mAssetFileDes.close(); 67 mAssetFileDes = null; 68 69 fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); 70 mAssetFileDes = new AssetFileDescriptor(fd, 0, FILE_LENGTH); 71 FileInputStream inputStream = mAssetFileDes.createInputStream(); 72 for (int i = 0; i < 6; i++) { 73 assertEquals(FILE_DATA[i], inputStream.read()); 74 } 75 for (int i = 0; i < FILE_DATA.length; i++) { 76 assertEquals(FILE_DATA[i], inputStream.read()); 77 } 78 assertEquals(FILE_END, inputStream.read()); 79 mAssetFileDes.close(); 80 mAssetFileDes = null; 81 } 82 } 83