Back to project page Resonos-Android-Framework.
The source code is released under:
Apache License
If you think the Android project Resonos-Android-Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.resonos.apps.library.file; //from w w w . j a va 2s .c o m import java.io.File; import java.io.IOException; import java.io.InputStream; import android.content.res.AssetManager; /** * @author mzechner * @author Nathan Sweet */ public class AltAndroidFileHandle extends AltFileHandle { // The asset manager, or null if this is not an internal file. transient AssetManager assets; public AltAndroidFileHandle(AssetManager assets, String fileName, AltFileType type) { super(fileName, type); this.assets = assets; } public AltAndroidFileHandle(AssetManager assets, File file, AltFileType type) { super(file, type); this.assets = assets; } public AltFileHandle child(String name) { if (file.getPath().length() == 0) return new AltAndroidFileHandle(assets, new File(name), type); return new AltAndroidFileHandle(assets, new File(file, name), type); } public AltFileHandle parent() { File parent = file.getParentFile(); if (parent == null) { if (type == AltFileType.Absolute) parent = new File("/"); else parent = new File(""); } return new AltAndroidFileHandle(assets, parent, type); } public InputStream read() { if (type == AltFileType.Internal) { try { return assets.open(file.getPath()); } catch (IOException ex) { throw new RuntimeException("Error reading file: " + file + " (" + type + ")", ex); } } return super.read(); } public AltFileHandle[] list() { if (type == AltFileType.Internal) { try { String[] relativePaths = assets.list(file.getPath()); AltFileHandle[] handles = new AltFileHandle[relativePaths.length]; for (int i = 0, n = handles.length; i < n; i++) handles[i] = new AltAndroidFileHandle(assets, new File(file, relativePaths[i]), type); return handles; } catch (Exception ex) { throw new RuntimeException("Error listing children: " + file + " (" + type + ")", ex); } } return super.list(); } public void setAssets(AssetManager assets) { this.assets = assets; } public AltFileHandle[] list(String suffix) { if (type == AltFileType.Internal) { try { String[] relativePaths = assets.list(file.getPath()); AltFileHandle[] handles = new AltFileHandle[relativePaths.length]; int count = 0; for (int i = 0, n = handles.length; i < n; i++) { String path = relativePaths[i]; if (!path.endsWith(suffix)) continue; handles[count] = new AltAndroidFileHandle(assets, new File( file, path), type); count++; } if (count < relativePaths.length) { AltFileHandle[] newHandles = new AltFileHandle[count]; System.arraycopy(handles, 0, newHandles, 0, count); handles = newHandles; } return handles; } catch (Exception ex) { throw new RuntimeException("Error listing children: " + file + " (" + type + ")", ex); } } return super.list(); } public boolean isDirectory() { if (type == AltFileType.Internal) { try { return assets.list(file.getPath()).length > 0; } catch (IOException ex) { return false; } } return super.isDirectory(); } public boolean exists() { if (type == AltFileType.Internal) { String fileName = file.getPath(); try { assets.open(fileName).close(); // Check if file exists. return true; } catch (Exception ex) { // This is SUPER slow! // try { // return assets.list(fileName).length > 0; // } catch (Exception ignored) { // } return false; } } return super.exists(); } public long length() { if (type == AltFileType.Internal) { try { return assets.openFd(file.getPath()).getLength(); } catch (IOException ignored) { } } return super.length(); } public long lastModified() { return super.lastModified(); } }