• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
88     @Override
getContents()89     public InputStream getContents() throws StreamException {
90         try {
91             return new FileInputStream(this);
92         } catch (FileNotFoundException e) {
93             throw new StreamException(e, this, StreamException.Error.FILENOTFOUND);
94         }
95     }
96 
97     @Override
setContents(InputStream source)98     public void setContents(InputStream source) throws StreamException {
99         FileOutputStream fos = null;
100         try {
101             fos = new FileOutputStream(this);
102 
103             byte[] buffer = new byte[1024];
104             int count = 0;
105             while ((count = source.read(buffer)) != -1) {
106                 fos.write(buffer, 0, count);
107             }
108         } catch (IOException e) {
109             throw new StreamException(e, this);
110         } finally {
111             if (fos != null) {
112                 try {
113                     fos.close();
114                 } catch (IOException e) {
115                     throw new StreamException(e, this);
116                 }
117             }
118         }
119     }
120 
121     @Override
getOutputStream()122     public OutputStream getOutputStream() throws StreamException {
123         try {
124             return new FileOutputStream(this);
125         } catch (FileNotFoundException e) {
126             throw new StreamException(e, this);
127         }
128     }
129 
130     @Override
getPreferredWriteMode()131     public PreferredWriteMode getPreferredWriteMode() {
132         return PreferredWriteMode.OUTPUTSTREAM;
133     }
134 
135     @Override
getOsLocation()136     public String getOsLocation() {
137         return getAbsolutePath();
138     }
139 
140     @Override
exists()141     public boolean exists() {
142         return isFile();
143     }
144 
145     @Override
getModificationStamp()146     public long getModificationStamp() {
147         return lastModified();
148     }
149 
150     @Override
getParentFolder()151     public IAbstractFolder getParentFolder() {
152         String p = this.getParent();
153         if (p == null) {
154             return null;
155         }
156         return new FolderWrapper(p);
157     }
158 }
159