1 /* 2 * Copyright (C) 2011 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.internal.editors.binaryxml; 18 19 import com.android.ide.eclipse.adt.AdtPlugin; 20 21 import org.eclipse.core.resources.IStorage; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.core.runtime.Status; 27 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.InputStream; 31 32 /** 33 * Implementation of storage for a local file 34 * (<code>java.io.File</code>). 35 * 36 * @see org.eclipse.core.resources.IStorage 37 */ 38 39 public class FileStorage implements IStorage { 40 41 /** 42 * The file this storage refers to. 43 */ 44 private File mFile = null; 45 46 /** 47 * Constructs and returns storage for the given file. 48 * 49 * @param file a local file 50 */ FileStorage(File file)51 public FileStorage(File file) { 52 mFile = file; 53 } 54 55 /* (non-Javadoc) 56 * @see org.eclipse.core.resources.IStorage#getContents() 57 */ 58 @Override getContents()59 public InputStream getContents() throws CoreException { 60 InputStream stream = null; 61 try { 62 stream = new FileInputStream(mFile); 63 } catch (Exception e) { 64 throw new CoreException(new Status(IStatus.ERROR, AdtPlugin.getDefault().getBundle() 65 .getSymbolicName(), IStatus.ERROR, mFile.getAbsolutePath(), e)); 66 } 67 return stream; 68 } 69 70 /* (non-Javadoc) 71 * @see org.eclipse.core.resources.IStorage#getFullPath() 72 */ 73 @Override getFullPath()74 public IPath getFullPath() { 75 return new Path(mFile.getAbsolutePath()); 76 } 77 78 /* (non-Javadoc) 79 * @see org.eclipse.core.resources.IStorage#getName() 80 */ 81 @Override getName()82 public String getName() { 83 return mFile.getName(); 84 } 85 86 /* (non-Javadoc) 87 * @see org.eclipse.core.resources.IStorage#isReadOnly() 88 */ 89 @Override isReadOnly()90 public boolean isReadOnly() { 91 return true; 92 } 93 94 /* (non-Javadoc) 95 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class) 96 */ 97 @Override getAdapter(Class adapter)98 public Object getAdapter(Class adapter) { 99 return null; 100 } 101 102 /* (non-Javadoc) 103 * @see java.lang.Object#equals(java.lang.Object) 104 */ 105 @Override equals(Object obj)106 public boolean equals(Object obj) { 107 if (obj instanceof FileStorage) { 108 return mFile.equals(((FileStorage) obj).mFile); 109 } 110 return super.equals(obj); 111 } 112 113 /* (non-Javadoc) 114 * @see java.lang.Object#hashCode() 115 */ 116 @Override hashCode()117 public int hashCode() { 118 return mFile.hashCode(); 119 } 120 } 121