Search in sources :

Example 1 with FileSystemStorage

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;
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 2 with FileSystemStorage

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);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 3 with FileSystemStorage

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);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage) TarInputStream(com.codename1.io.tar.TarInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) TarEntry(com.codename1.io.tar.TarEntry)

Example 4 with FileSystemStorage

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);
    }
}
Also used : FileSystemStorage(com.codename1.io.FileSystemStorage)

Example 5 with FileSystemStorage

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;
}
Also used : BufferedInputStream(com.codename1.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(com.codename1.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Paint(android.graphics.Paint)

Aggregations

FileSystemStorage (com.codename1.io.FileSystemStorage)7 OutputStream (java.io.OutputStream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Paint (android.graphics.Paint)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 TarEntry (com.codename1.io.tar.TarEntry)1 TarInputStream (com.codename1.io.tar.TarInputStream)1 ParseException (com.codename1.l10n.ParseException)1 EncodedImage (com.codename1.ui.EncodedImage)1 ActionListener (com.codename1.ui.events.ActionListener)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1