use of com.codename1.io.FileSystemStorage in project CodenameOne by codenameone.
the class TarUtils method tarSize.
private static long tarSize(String path) throws IOException {
long size = 0;
FileSystemStorage fileSystem = FileSystemStorage.getInstance();
if (fileSystem.isDirectory(path)) {
String[] subFiles = fileSystem.listFiles(path);
if (subFiles != null && subFiles.length > 0) {
for (String file : subFiles) {
if (fileSystem.isDirectory(file)) {
size += tarSize(file);
} else {
size += entrySize(fileSystem.getLength(file));
}
}
} else {
// Empty folder header
return TarConstants.HEADER_BLOCK;
}
} else {
return entrySize(fileSystem.getLength(path));
}
return size;
}
use of com.codename1.io.FileSystemStorage in project CodenameOne by codenameone.
the class CodenameOneImplementation method downloadImageToCache.
/**
* Downloads an image from a URL to the cache. Platforms
* that support a native image cache {@link #supportsNativeImageCache() } (e.g. Javascript) override this method to defer to the
* platform's handling of cached images. Platforms that have a caches directory ({@link FileSystemStorage#hasCachesDir() }
* will use that directory to cache the image. Other platforms will just download to storage.
*
* @param url The URL of the image to download.
* @param onSuccess Callback on success.
* @param onFail Callback on fail.
*
* @see URLImage#createToCache(com.codename1.ui.EncodedImage, java.lang.String, com.codename1.ui.URLImage.ImageAdapter)
*/
public void downloadImageToCache(String url, SuccessCallback<Image> onSuccess, final FailureCallback<Image> onFail) {
FileSystemStorage fs = FileSystemStorage.getInstance();
if (fs.hasCachesDir()) {
String name = "cn1_image_cache[" + url + "]";
name = StringUtil.replaceAll(name, "/", "_");
name = StringUtil.replaceAll(name, "\\", "_");
name = StringUtil.replaceAll(name, "%", "_");
name = StringUtil.replaceAll(name, "?", "_");
name = StringUtil.replaceAll(name, "*", "_");
name = StringUtil.replaceAll(name, ":", "_");
name = StringUtil.replaceAll(name, "=", "_");
String filePath = fs.getCachesDir() + fs.getFileSystemSeparator() + name;
// We use Util.downloadImageToFileSystem rather than CodenameOneImplementation.downloadImageToFileSystem
// because we want it to try to load from file system first.
Util.downloadImageToFileSystem(url, filePath, onSuccess, onFail);
} else {
// We use Util.downloadImageToStorage rather than CodenameOneImplementation.downloadImageToStorage
// because we want it to try to load from storage first.
Util.downloadImageToStorage(url, "cn1_image_cache[" + url + "]", onSuccess, onFail);
}
}
use of com.codename1.io.FileSystemStorage in project CodenameOne by codenameone.
the class CodenameOneImplementation method installTar.
/**
* Installs a tar file from the build server into the file system storage so it can be used with respect for hierarchy
*/
public void installTar() throws IOException {
String p = Preferences.get("cn1$InstallKey", null);
String buildKey = Display.getInstance().getProperty("build_key", null);
if (p == null || !p.equals(buildKey)) {
FileSystemStorage fs = FileSystemStorage.getInstance();
String tardir = fs.getAppHomePath() + "cn1html/";
fs.mkdir(tardir);
TarInputStream is = new TarInputStream(Display.getInstance().getResourceAsStream(getClass(), "/html.tar"));
TarEntry t = is.getNextEntry();
byte[] data = new byte[8192];
while (t != null) {
String name = t.getName();
if (t.isDirectory()) {
fs.mkdir(tardir + name);
} else {
String path = tardir + name;
String dir = path.substring(0, path.lastIndexOf('/'));
if (!fs.exists(dir)) {
mkdirs(fs, dir);
}
OutputStream os = fs.openOutputStream(tardir + name);
int count;
while ((count = is.read(data)) != -1) {
os.write(data, 0, count);
}
os.close();
}
t = is.getNextEntry();
}
Util.cleanup(is);
Preferences.set("cn1$InstallKey", buildKey);
}
}
use of com.codename1.io.FileSystemStorage in project CodenameOne by codenameone.
the class CodenameOneImplementation method setBrowserPageInHierarchy.
/**
* Sets a relative URL from the html hierarchy
*
* @param browserPeer the peer component
* @param url the url relative to the HTML directory
*/
public void setBrowserPageInHierarchy(PeerComponent browserPeer, String url) throws IOException {
installTar();
FileSystemStorage fs = FileSystemStorage.getInstance();
String tardir = fs.getAppHomePath() + "cn1html";
if (tardir.startsWith("/")) {
tardir = "file://" + tardir;
}
if (url.startsWith("/")) {
setBrowserURL(browserPeer, tardir + url);
} else {
setBrowserURL(browserPeer, tardir + "/" + url);
}
}
use of com.codename1.io.FileSystemStorage in project CodenameOne by codenameone.
the class AndroidImplementation method fixAttachmentPath.
private String fixAttachmentPath(String attachment) {
if (attachment.contains(getAppHomePath())) {
FileSystemStorage fs = FileSystemStorage.getInstance();
final char sep = fs.getFileSystemSeparator();
String fileName = attachment.substring(attachment.lastIndexOf(sep) + 1);
String[] roots = FileSystemStorage.getInstance().getRoots();
// iOS doesn't have an SD card
String root = roots[0];
for (int i = 0; i < roots.length; i++) {
// media_rw is a protected system lib
if (FileSystemStorage.getInstance().getRootType(roots[i]) == FileSystemStorage.ROOT_TYPE_SDCARD && !roots[i].contains("media_rw")) {
root = roots[i];
break;
}
}
// might happen if only the media_rw is of type ROOT_TYPE_SDCARD
if (root.contains("media_rw")) {
// try again without checking the root type
for (int i = 0; i < roots.length; i++) {
// media_rw is a protected system lib
if (!roots[i].contains("media_rw")) {
root = roots[i];
break;
}
}
}
String fileUri = root + sep + "tmp" + sep + fileName;
FileSystemStorage.getInstance().mkdir(root + sep + "tmp");
try {
InputStream is = FileSystemStorage.getInstance().openInputStream(attachment);
OutputStream os = FileSystemStorage.getInstance().openOutputStream(fileUri);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > -1) {
os.write(buf, 0, len);
}
is.close();
os.close();
} catch (IOException ex) {
Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
}
attachment = fileUri;
}
if (attachment.indexOf(":") < 0) {
attachment = "file://" + attachment;
}
return attachment;
}
Aggregations