1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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.ide.eclipse.adt.io; 18 19 import com.android.io.IAbstractFile; 20 import com.android.io.IAbstractFolder; 21 import com.android.io.StreamException; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.NullProgressMonitor; 28 29 import java.io.ByteArrayInputStream; 30 import java.io.ByteArrayOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.OutputStream; 34 35 /** 36 * An implementation of {@link IAbstractFile} on top of an {@link IFile} object. 37 */ 38 public class IFileWrapper implements IAbstractFile { 39 40 private final IFile mFile; 41 42 public IFileWrapper(IFile file) { 43 mFile = file; 44 } 45 46 @Override 47 public InputStream getContents() throws StreamException { 48 try { 49 return mFile.getContents(); 50 } catch (CoreException e) { 51 StreamException.Error error = StreamException.Error.DEFAULT; 52 if (mFile.isSynchronized(IResource.DEPTH_ZERO) == false) { 53 error = StreamException.Error.OUTOFSYNC; 54 } 55 throw new StreamException(e, this, error); 56 } 57 } 58 59 @Override 60 public void setContents(InputStream source) throws StreamException { 61 try { 62 mFile.setContents(source, IResource.FORCE, null); 63 } catch (CoreException e) { 64 throw new StreamException(e, this); 65 } 66 } 67 68 @Override 69 public OutputStream getOutputStream() throws StreamException { 70 return new ByteArrayOutputStream() { 71 @Override 72 public void close() throws IOException { 73 super.close(); 74 75 byte[] data = toByteArray(); 76 try { 77 setContents(new ByteArrayInputStream(data)); 78 } catch (StreamException e) { 79 throw new IOException(); 80 } 81 } 82 }; 83 } 84 85 @Override 86 public PreferredWriteMode getPreferredWriteMode() { 87 return PreferredWriteMode.INPUTSTREAM; 88 } 89 90 @Override 91 public String getOsLocation() { 92 return mFile.getLocation().toOSString(); 93 } 94 95 @Override 96 public String getName() { 97 return mFile.getName(); 98 } 99 100 @Override 101 public boolean exists() { 102 return mFile.exists(); 103 } 104 105 @Override 106 public boolean delete() { 107 try { 108 mFile.delete(true /*force*/, new NullProgressMonitor()); 109 return true; 110 } catch (CoreException e) { 111 return false; 112 } 113 } 114 115 /** 116 * Returns the {@link IFile} object that the receiver could represent. Can be <code>null</code> 117 */ 118 public IFile getIFile() { 119 return mFile; 120 } 121 122 @Override 123 public long getModificationStamp() { 124 return mFile.getModificationStamp(); 125 } 126 127 @Override 128 public boolean equals(Object obj) { 129 if (obj instanceof IFileWrapper) { 130 return mFile.equals(((IFileWrapper)obj).mFile); 131 } 132 133 if (obj instanceof IFile) { 134 return mFile.equals(obj); 135 } 136 137 return super.equals(obj); 138 } 139 140 @Override 141 public int hashCode() { 142 return mFile.hashCode(); 143 } 144 145 @Override 146 public IAbstractFolder getParentFolder() { 147 IContainer p = mFile.getParent(); 148 if (p != null) { 149 return new IFolderWrapper(p); 150 } 151 152 return null; 153 } 154 155 @Override 156 public String toString() { 157 return mFile.toString(); 158 } 159 } 160