diff options
author | Xavier Ducrohet <xav@android.com> | 2010-02-23 20:31:43 -0800 |
---|---|---|
committer | Xavier Ducrohet <xav@android.com> | 2010-02-23 20:34:35 -0800 |
commit | a07f53e7b163264fc5a03ff0203f00cb905a5439 (patch) | |
tree | b621324c423eb9e8519729c0d6871da323965d3f /sdkmanager | |
parent | 6e2c5ced68efbaaa574c7b9d8a2063571787642e (diff) | |
download | sdk-a07f53e7b163264fc5a03ff0203f00cb905a5439.zip sdk-a07f53e7b163264fc5a03ff0203f00cb905a5439.tar.gz sdk-a07f53e7b163264fc5a03ff0203f00cb905a5439.tar.bz2 |
Improve the IAbstractFile/Folder classes.
- Add setContent to the file class
- add listMembers to the folder class
- extend java.io.File instead of using a delegate.
Change-Id: Ib6434b37c8cceb6661bc6a17ae678a56d2c243f2
Diffstat (limited to 'sdkmanager')
4 files changed, 164 insertions, 54 deletions
diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FileWrapper.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FileWrapper.java index b8af4be..0fe8902 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FileWrapper.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FileWrapper.java @@ -20,59 +20,108 @@ package com.android.sdklib.internal.io; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.net.URI; /** - * An implementation of {@link IAbstractFile} on top of a {@link File} object. - * + * An implementation of {@link IAbstractFile} extending {@link File}. */ -public class FileWrapper implements IAbstractFile { +public class FileWrapper extends File implements IAbstractFile { + private static final long serialVersionUID = 1L; + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(File, String) + */ + public FileWrapper(File parent, String child) { + super(parent, child); + } - private final File mFile; + /** + * Creates a new File instance by converting the given pathname string into an abstract + * pathname. + * @param pathname the pathname + * + * @see File#File(String) + */ + public FileWrapper(String pathname) { + super(pathname); + } + + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(String, String) + */ + public FileWrapper(String parent, String child) { + super(parent, child); + } + + /** + * Creates a new File instance by converting the given <code>file:</code> URI into an + * abstract pathname. + * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path + * component, and undefined authority, query, and fragment components + * + * @see File#File(URI) + */ + public FileWrapper(URI uri) { + super(uri); + } /** - * Constructs a {@link FileWrapper} object. The underlying {@link File} object needs not - * exist or be a valid file. + * Creates a new File instance matching a give {@link File} object. + * @param file the file to match */ public FileWrapper(File file) { - mFile = file; + super(file.getAbsolutePath()); } public InputStream getContents() throws StreamException { try { - return new FileInputStream(mFile); + return new FileInputStream(this); } catch (FileNotFoundException e) { throw new StreamException(e); } } - public String getOsLocation() { - return mFile.getAbsolutePath(); - } - - public String getName() { - return mFile.getName(); - } - - public boolean exists() { - return mFile.isFile(); - } + public void setContents(InputStream source) throws StreamException { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(this); - @Override - public boolean equals(Object obj) { - if (obj instanceof FileWrapper) { - return mFile.equals(((FileWrapper)obj).mFile); + byte[] buffer = new byte[1024]; + int count = 0; + while ((count = source.read(buffer)) != -1) { + fos.write(buffer, 0, count); + } + } catch (IOException e) { + throw new StreamException(e); + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + throw new StreamException(e); + } + } } + } - if (obj instanceof File) { - return mFile.equals(obj); - } - return super.equals(obj); + public String getOsLocation() { + return getAbsolutePath(); } @Override - public int hashCode() { - return mFile.hashCode(); + public boolean exists() { + return isFile(); } } diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FolderWrapper.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FolderWrapper.java index a9269ad..1496216 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FolderWrapper.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/FolderWrapper.java @@ -18,53 +18,102 @@ package com.android.sdklib.internal.io; import java.io.File; +import java.io.FilenameFilter; +import java.net.URI; /** - * An implementation of {@link IAbstractFolder} on top of a {@link File} object. + * An implementation of {@link IAbstractFolder} extending {@link File}. */ -public class FolderWrapper implements IAbstractFolder { +public class FolderWrapper extends File implements IAbstractFolder { - private final File mFolder; + private static final long serialVersionUID = 1L; /** - * Constructs a {@link FileWrapper} object. The underlying {@link File} object needs not exists - * or be a valid directory. + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(File, String) */ - public FolderWrapper(File folder) { - mFolder = folder; + public FolderWrapper(File parent, String child) { + super(parent, child); } - public boolean hasFile(String name) { - return false; + /** + * Creates a new File instance by converting the given pathname string into an abstract + * pathname. + * @param pathname the pathname + * + * @see File#File(String) + */ + public FolderWrapper(String pathname) { + super(pathname); } - public IAbstractFile getFile(String name) { - return new FileWrapper(new File(mFolder, name)); + /** + * Creates a new File instance from a parent abstract pathname and a child pathname string. + * @param parent the parent pathname + * @param child the child name + * + * @see File#File(String, String) + */ + public FolderWrapper(String parent, String child) { + super(parent, child); } - public String getName() { - return mFolder.getName(); + /** + * Creates a new File instance by converting the given <code>file:</code> URI into an + * abstract pathname. + * @param uri An absolute, hierarchical URI with a scheme equal to "file", a non-empty path + * component, and undefined authority, query, and fragment components + * + * @see File#File(URI) + */ + public FolderWrapper(URI uri) { + super(uri); } - public boolean exists() { - return mFolder.isDirectory(); + /** + * Creates a new File instance matching a give {@link File} object. + * @param file the file to match + */ + public FolderWrapper(File file) { + super(file.getAbsolutePath()); } - @Override - public boolean equals(Object obj) { - if (obj instanceof FolderWrapper) { - return mFolder.equals(((FolderWrapper)obj).mFolder); - } + public IAbstractResource[] listMembers() { + File[] files = listFiles(); + final int count = files.length; + IAbstractResource[] afiles = new IAbstractResource[count]; - if (obj instanceof File) { - return mFolder.equals(obj); + for (int i = 0 ; i < count ; i++) { + File f = files[i]; + if (f.isFile()) { + afiles[i] = new FileWrapper(f); + } else { + afiles[i] = new FolderWrapper(f); + } } - return super.equals(obj); + return afiles; + } + + public boolean hasFile(final String name) { + String[] match = list(new FilenameFilter() { + public boolean accept(File dir, String filename) { + return name.equals(filename); + } + }); + + return match.length > 0; + } + + public IAbstractFile getFile(String name) { + return new FileWrapper(this, name); } @Override - public int hashCode() { - return mFolder.hashCode(); + public boolean exists() { + return isDirectory(); } } diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFile.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFile.java index f96ede6..3c9ffd3 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFile.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFile.java @@ -30,6 +30,13 @@ public interface IAbstractFile extends IAbstractResource { InputStream getContents() throws StreamException; /** + * Sets the content of the file. + * @param source the content + * @throws StreamException + */ + void setContents(InputStream source) throws StreamException; + + /** * Returns the OS path of the file location. */ String getOsLocation(); diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFolder.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFolder.java index 22f654b..7751767 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFolder.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/io/IAbstractFolder.java @@ -35,4 +35,9 @@ public interface IAbstractFolder extends IAbstractResource { * @param name the name of the file. */ IAbstractFile getFile(String name); + + /** + * returns a list of existing members in this folder. + */ + IAbstractResource[] listMembers(); } |