aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2011-02-24 19:41:49 -0800
committerXavier Ducrohet <xav@android.com>2011-02-24 19:53:15 -0800
commit9aa538ffaf7abdcf4fe56c51da75666e60c67a90 (patch)
treeb99f71c19ecb59e1a235b6e137b8bbbee829dc49 /common
parent81637b879c81622d1b3ea8d6ec80e619ae906953 (diff)
downloadsdk-9aa538ffaf7abdcf4fe56c51da75666e60c67a90.zip
sdk-9aa538ffaf7abdcf4fe56c51da75666e60c67a90.tar.gz
sdk-9aa538ffaf7abdcf4fe56c51da75666e60c67a90.tar.bz2
Move the sdk io classes to common.jar
Change-Id: I59a7b770071707ed058aa104bab8a16aa8950d56
Diffstat (limited to 'common')
-rw-r--r--common/src/com/android/io/FileWrapper.java147
-rw-r--r--common/src/com/android/io/FolderWrapper.java154
-rw-r--r--common/src/com/android/io/IAbstractFile.java53
-rw-r--r--common/src/com/android/io/IAbstractFolder.java77
-rw-r--r--common/src/com/android/io/IAbstractResource.java45
-rw-r--r--common/src/com/android/io/StreamException.java28
6 files changed, 504 insertions, 0 deletions
diff --git a/common/src/com/android/io/FileWrapper.java b/common/src/com/android/io/FileWrapper.java
new file mode 100644
index 0000000..c1e8f81
--- /dev/null
+++ b/common/src/com/android/io/FileWrapper.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.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.io.OutputStream;
+import java.net.URI;
+
+/**
+ * An implementation of {@link IAbstractFile} extending {@link File}.
+ */
+public class FileWrapper extends File implements IAbstractFile {
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Creates a new File instance matching a given {@link File} object.
+ * @param file the file to match
+ */
+ public FileWrapper(File file) {
+ super(file.getAbsolutePath());
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * Creates a new File instance by converting the given pathname string into an abstract
+ * pathname.
+ * @param osPathname the OS pathname
+ *
+ * @see File#File(String)
+ */
+ public FileWrapper(String osPathname) {
+ super(osPathname);
+ }
+
+ /**
+ * 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);
+ }
+
+ public InputStream getContents() throws StreamException {
+ try {
+ return new FileInputStream(this);
+ } catch (FileNotFoundException e) {
+ throw new StreamException(e);
+ }
+ }
+
+ public void setContents(InputStream source) throws StreamException {
+ FileOutputStream fos = null;
+ try {
+ fos = new FileOutputStream(this);
+
+ 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);
+ }
+ }
+ }
+ }
+
+ public OutputStream getOutputStream() throws StreamException {
+ try {
+ return new FileOutputStream(this);
+ } catch (FileNotFoundException e) {
+ throw new StreamException(e);
+ }
+ }
+
+ public PreferredWriteMode getPreferredWriteMode() {
+ return PreferredWriteMode.OUTPUTSTREAM;
+ }
+
+ public String getOsLocation() {
+ return getAbsolutePath();
+ }
+
+ @Override
+ public boolean exists() {
+ return isFile();
+ }
+
+ public IAbstractFolder getParentFolder() {
+ String p = this.getParent();
+ if (p == null) {
+ return null;
+ }
+ return new FolderWrapper(p);
+ }
+}
diff --git a/common/src/com/android/io/FolderWrapper.java b/common/src/com/android/io/FolderWrapper.java
new file mode 100644
index 0000000..26ed9cf
--- /dev/null
+++ b/common/src/com/android/io/FolderWrapper.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.io;
+
+
+import java.io.File;
+import java.net.URI;
+import java.util.ArrayList;
+
+/**
+ * An implementation of {@link IAbstractFolder} extending {@link File}.
+ */
+public class FolderWrapper extends File implements IAbstractFolder {
+
+ 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 FolderWrapper(File parent, String child) {
+ super(parent, child);
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * 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);
+ }
+
+ /**
+ * Creates a new File instance matching a give {@link File} object.
+ * @param file the file to match
+ */
+ public FolderWrapper(File file) {
+ super(file.getAbsolutePath());
+ }
+
+ public IAbstractResource[] listMembers() {
+ File[] files = listFiles();
+ final int count = files == null ? 0 : files.length;
+ IAbstractResource[] afiles = new IAbstractResource[count];
+
+ if (files != null) {
+ for (int i = 0 ; i < count ; i++) {
+ File f = files[i];
+ if (f.isFile()) {
+ afiles[i] = new FileWrapper(f);
+ } else if (f.isDirectory()) {
+ afiles[i] = new FolderWrapper(f);
+ }
+ }
+ }
+
+ return afiles;
+ }
+
+ public boolean hasFile(final String name) {
+ String[] match = list(new FilenameFilter() {
+ public boolean accept(IAbstractFolder dir, String filename) {
+ return name.equals(filename);
+ }
+ });
+
+ return match.length > 0;
+ }
+
+ public IAbstractFile getFile(String name) {
+ return new FileWrapper(this, name);
+ }
+
+ public IAbstractFolder getFolder(String name) {
+ return new FolderWrapper(this, name);
+ }
+
+ public IAbstractFolder getParentFolder() {
+ String p = this.getParent();
+ if (p == null) {
+ return null;
+ }
+ return new FolderWrapper(p);
+ }
+
+ public String getOsLocation() {
+ return getAbsolutePath();
+ }
+
+ @Override
+ public boolean exists() {
+ return isDirectory();
+ }
+
+ public String[] list(FilenameFilter filter) {
+ File[] files = listFiles();
+ if (files != null && files.length > 0) {
+ ArrayList<String> list = new ArrayList<String>();
+
+ for (File file : files) {
+ if (filter.accept(this, file.getName())) {
+ list.add(file.getName());
+ }
+ }
+
+ return list.toArray(new String[list.size()]);
+ }
+
+ return new String[0];
+ }
+}
diff --git a/common/src/com/android/io/IAbstractFile.java b/common/src/com/android/io/IAbstractFile.java
new file mode 100644
index 0000000..d8d794d
--- /dev/null
+++ b/common/src/com/android/io/IAbstractFile.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.io;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * A file.
+ */
+public interface IAbstractFile extends IAbstractResource {
+ public static enum PreferredWriteMode {
+ INPUTSTREAM, OUTPUTSTREAM;
+ }
+
+ /**
+ * Returns an {@link InputStream} object on the file content.
+ * @throws StreamException
+ */
+ InputStream getContents() throws StreamException;
+
+ /**
+ * Sets the content of the file.
+ * @param source the content
+ * @throws StreamException
+ */
+ void setContents(InputStream source) throws StreamException;
+
+ /**
+ * Returns an {@link OutputStream} to write into the file.
+ * @throws StreamException
+ */
+ OutputStream getOutputStream() throws StreamException;
+
+ /**
+ * Returns the preferred mode to write into the file.
+ */
+ PreferredWriteMode getPreferredWriteMode();
+}
diff --git a/common/src/com/android/io/IAbstractFolder.java b/common/src/com/android/io/IAbstractFolder.java
new file mode 100644
index 0000000..8335ef9
--- /dev/null
+++ b/common/src/com/android/io/IAbstractFolder.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.io;
+
+import java.io.File;
+
+/**
+ * A folder.
+ */
+public interface IAbstractFolder extends IAbstractResource {
+ /**
+ * Instances of classes that implement this interface are used to
+ * filter filenames.
+ */
+ public interface FilenameFilter {
+ /**
+ * Tests if a specified file should be included in a file list.
+ *
+ * @param dir the directory in which the file was found.
+ * @param name the name of the file.
+ * @return <code>true</code> if and only if the name should be
+ * included in the file list; <code>false</code> otherwise.
+ */
+ boolean accept(IAbstractFolder dir, String name);
+ }
+
+ /**
+ * Returns true if the receiver contains a file with a given name
+ * @param name the name of the file. This is the name without the path leading to the
+ * parent folder.
+ */
+ boolean hasFile(String name);
+
+ /**
+ * Returns an {@link IAbstractFile} representing a child of the current folder with the
+ * given name. The file may not actually exist.
+ * @param name the name of the file.
+ */
+ IAbstractFile getFile(String name);
+
+ /**
+ * Returns an {@link IAbstractFolder} representing a child of the current folder with the
+ * given name. The folder may not actually exist.
+ * @param name the name of the folder.
+ */
+ IAbstractFolder getFolder(String name);
+
+ /**
+ * Returns a list of all existing file and directory members in this folder.
+ * The returned array can be empty but is never null.
+ */
+ IAbstractResource[] listMembers();
+
+ /**
+ * Returns a list of all existing file and directory members in this folder
+ * that satisfy the specified filter.
+ *
+ * @param filter A filename filter instance. Must not be null.
+ * @return An array of file names (generated using {@link File#getName()}).
+ * The array can be empty but is never null.
+ */
+ String[] list(FilenameFilter filter);
+}
diff --git a/common/src/com/android/io/IAbstractResource.java b/common/src/com/android/io/IAbstractResource.java
new file mode 100644
index 0000000..3d762eb
--- /dev/null
+++ b/common/src/com/android/io/IAbstractResource.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.io;
+
+/**
+ * Base representation of a file system resource.<p/>
+ * This somewhat limited interface is designed to let classes use file-system resources, without
+ * having the manually handle either the standard Java file or the Eclipse file API..
+ */
+public interface IAbstractResource {
+
+ /**
+ * Returns the name of the resource.
+ */
+ String getName();
+
+ /**
+ * Returns the OS path of the folder location.
+ */
+ String getOsLocation();
+
+ /**
+ * Returns whether the resource actually exists.
+ */
+ boolean exists();
+
+ /**
+ * Returns the parent folder or null if there is no parent.
+ */
+ IAbstractFolder getParentFolder();
+}
diff --git a/common/src/com/android/io/StreamException.java b/common/src/com/android/io/StreamException.java
new file mode 100644
index 0000000..f67c7a8
--- /dev/null
+++ b/common/src/com/android/io/StreamException.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.io;
+
+/**
+ * Exception thrown when {@link IAbstractFile#getContents()} fails.
+ */
+public class StreamException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public StreamException(Exception e) {
+ super(e);
+ }
+}