1 /* 2 * Copyright (C) 2008 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 17 package com.android.io; 18 19 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.FileNotFoundException; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.OutputStream; 27 import java.net.URI; 28 29 /** 30 * An implementation of {@link IAbstractFile} extending {@link File}. 31 */ 32 public class FileWrapper extends File implements IAbstractFile { 33 private static final long serialVersionUID = 1L; 34 35 /** 36 * Creates a new File instance matching a given {@link File} object. 37 * @param file the file to match 38 */ FileWrapper(File file)39 public FileWrapper(File file) { 40 super(file.getAbsolutePath()); 41 } 42 43 /** 44 * Creates a new File instance from a parent abstract pathname and a child pathname string. 45 * @param parent the parent pathname 46 * @param child the child name 47 * 48 * @see File#File(File, String) 49 */ FileWrapper(File parent, String child)50 public FileWrapper(File parent, String child) { 51 super(parent, child); 52 } 53 54 /** 55 * Creates a new File instance by converting the given pathname string into an abstract 56 * pathname. 57 * @param osPathname the OS pathname 58 * 59 * @see File#File(String) 60 */ FileWrapper(String osPathname)61 public FileWrapper(String osPathname) { 62 super(osPathname); 63 } 64 65 /** 66 * Creates a new File instance from a parent abstract pathname and a child pathname string. 67 * @param parent the parent pathname 68 * @param child the child name 69 * 70 * @see File#File(String, String) 71 */ FileWrapper(String parent, String child)72 public FileWrapper(String parent, String child) { 73 super(parent, child); 74 } 75 76 /** 77 * Creates a new File instance by converting the given <code>file:</code> URI into an 78 * abstract pathname. 79 * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path 80 * component, and undefined authority, query, and fragment components 81 * 82 * @see File#File(URI) 83 */ FileWrapper(URI uri)84 public FileWrapper(URI uri) { 85 super(uri); 86 } 87 getContents()88 public InputStream getContents() throws StreamException { 89 try { 90 return new FileInputStream(this); 91 } catch (FileNotFoundException e) { 92 throw new StreamException(e); 93 } 94 } 95 setContents(InputStream source)96 public void setContents(InputStream source) throws StreamException { 97 FileOutputStream fos = null; 98 try { 99 fos = new FileOutputStream(this); 100 101 byte[] buffer = new byte[1024]; 102 int count = 0; 103 while ((count = source.read(buffer)) != -1) { 104 fos.write(buffer, 0, count); 105 } 106 } catch (IOException e) { 107 throw new StreamException(e); 108 } finally { 109 if (fos != null) { 110 try { 111 fos.close(); 112 } catch (IOException e) { 113 throw new StreamException(e); 114 } 115 } 116 } 117 } 118 getOutputStream()119 public OutputStream getOutputStream() throws StreamException { 120 try { 121 return new FileOutputStream(this); 122 } catch (FileNotFoundException e) { 123 throw new StreamException(e); 124 } 125 } 126 getPreferredWriteMode()127 public PreferredWriteMode getPreferredWriteMode() { 128 return PreferredWriteMode.OUTPUTSTREAM; 129 } 130 getOsLocation()131 public String getOsLocation() { 132 return getAbsolutePath(); 133 } 134 135 @Override exists()136 public boolean exists() { 137 return isFile(); 138 } 139 getModificationStamp()140 public long getModificationStamp() { 141 return lastModified(); 142 } 143 getParentFolder()144 public IAbstractFolder getParentFolder() { 145 String p = this.getParent(); 146 if (p == null) { 147 return null; 148 } 149 return new FolderWrapper(p); 150 } 151 } 152