2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.sdklib.io;
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;
30 * An implementation of {@link IAbstractFile} extending {@link File}.
32 public class FileWrapper extends File implements IAbstractFile {
33 private static final long serialVersionUID = 1L;
36 * Creates a new File instance from a parent abstract pathname and a child pathname string.
37 * @param parent the parent pathname
38 * @param child the child name
40 * @see File#File(File, String)
42 public FileWrapper(File parent, String child) {
47 * Creates a new File instance by converting the given pathname string into an abstract
49 * @param pathname the pathname
51 * @see File#File(String)
53 public FileWrapper(String pathname) {
58 * Creates a new File instance from a parent abstract pathname and a child pathname string.
59 * @param parent the parent pathname
60 * @param child the child name
62 * @see File#File(String, String)
64 public FileWrapper(String parent, String child) {
69 * Creates a new File instance by converting the given <code>file:</code> URI into an
71 * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path
72 * component, and undefined authority, query, and fragment components
76 public FileWrapper(URI uri) {
81 * Creates a new File instance matching a give {@link File} object.
82 * @param file the file to match
84 public FileWrapper(File file) {
85 super(file.getAbsolutePath());
88 public InputStream getContents() throws StreamException {
90 return new FileInputStream(this);
91 } catch (FileNotFoundException e) {
92 throw new StreamException(e);
96 public void setContents(InputStream source) throws StreamException {
97 FileOutputStream fos = null;
99 fos = new FileOutputStream(this);
101 byte[] buffer = new byte[1024];
103 while ((count = source.read(buffer)) != -1) {
104 fos.write(buffer, 0, count);
106 } catch (IOException e) {
107 throw new StreamException(e);
112 } catch (IOException e) {
113 throw new StreamException(e);
119 public OutputStream getOutputStream() throws StreamException {
121 return new FileOutputStream(this);
122 } catch (FileNotFoundException e) {
123 throw new StreamException(e);
127 public PreferredWriteMode getPreferredWriteMode() {
128 return PreferredWriteMode.OUTPUTSTREAM;
131 public String getOsLocation() {
132 return getAbsolutePath();
136 public boolean exists() {
140 public IAbstractFolder getParentFolder() {
141 String p = this.getParent();
145 return new FolderWrapper(p);