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.sdklib.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 from a parent abstract pathname and a child pathname string. 37 * @param parent the parent pathname 38 * @param child the child name 39 * 40 * @see File#File(File, String) 41 */ 42 public FileWrapper(File parent, String child) { 43 super(parent, child); 44 } 45 46 /** 47 * Creates a new File instance by converting the given pathname string into an abstract 48 * pathname. 49 * @param pathname the pathname 50 * 51 * @see File#File(String) 52 */ 53 public FileWrapper(String pathname) { 54 super(pathname); 55 } 56 57 /** 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 61 * 62 * @see File#File(String, String) 63 */ 64 public FileWrapper(String parent, String child) { 65 super(parent, child); 66 } 67 68 /** 69 * Creates a new File instance by converting the given <code>file:</code> URI into an 70 * abstract pathname. 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 73 * 74 * @see File#File(URI) 75 */ 76 public FileWrapper(URI uri) { 77 super(uri); 78 } 79 80 /** 81 * Creates a new File instance matching a give {@link File} object. 82 * @param file the file to match 83 */ 84 public FileWrapper(File file) { 85 super(file.getAbsolutePath()); 86 } 87 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 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 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 127 public PreferredWriteMode getPreferredWriteMode() { 128 return PreferredWriteMode.OUTPUTSTREAM; 129 } 130 131 public String getOsLocation() { 132 return getAbsolutePath(); 133 } 134 135 @Override 136 public boolean exists() { 137 return isFile(); 138 } 139 140 public IAbstractFolder getParentFolder() { 141 String p = this.getParent(); 142 if (p == null) { 143 return null; 144 } 145 return new FolderWrapper(p); 146 } 147 } 148