Android Open Source - loopback-sdk-android File






From Project

Back to project page loopback-sdk-android.

License

The source code is released under:

MIT License

If you think the Android project loopback-sdk-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.strongloop.android.loopback;
/* www .  j a  va2s. co  m*/
import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files;
import com.strongloop.android.loopback.callbacks.EmptyResponseParser;
import com.strongloop.android.loopback.callbacks.VoidCallback;
import com.strongloop.android.remoting.Transient;
import com.strongloop.android.remoting.VirtualObject;
import com.strongloop.android.remoting.adapters.Adapter;

import java.io.IOException;
import java.util.Map;

public class File extends VirtualObject {

    private String name;
    public void setName(String name) { this.name = name; }

    /**
     * The name of the file, e.g. "image.gif"
     * @return the name
     */
    public String getName() { return name; }
    
    private String url;
    public void setUrl(String url) { this.url = url; }

    /**
     * The URL of the file.
     * @return the URL
     */
    public String getUrl() {
        return url;
    }
    
    private Container container;
    @Transient
    public void setContainerRef(Container container) { this.container = container; }
    @Transient
    public Container getContainerRef() { return container; }

    /**
     * Name of the container this file belongs to.
     * @return the container name
     */
    public String getContainer() { return getContainerRef().getName(); }

    public static interface DownloadCallback {
        public void onSuccess(byte[] content, String contentType);
        public void onError(Throwable error);
    }

    /**
     * Download content of this file.
     * @param callback The callback to be executed when finished.
     */
    public void download(final DownloadCallback callback) {
        invokeMethod("download", getCommonParams(), new Adapter.BinaryCallback() {
            @Override
            public void onSuccess(byte[] content, String contentType) {
                callback.onSuccess(content, contentType);
            }

            @Override
            public void onError(Throwable t) {
                callback.onError(t);
            }
        });
    }

    /**
     * Download content of this file to a local file.
     * @param localFile Path to the local file.
     * @param callback The callback to be executed when finished.
     */
    public void download(final java.io.File localFile, final VoidCallback callback) {
        download(new DownloadCallback() {
            @Override
            public void onSuccess(byte[] content, String contentType) {
                try {
                    Files.write(content, localFile);
                    callback.onSuccess();
                } catch (IOException ex) {
                    callback.onError(ex);
                }
            }

            @Override
            public void onError(Throwable error) {
                callback.onError(error);

            }
        });
    }

    /**
     * Delete this file.
     * @param callback The callback to be executed when finished.
     */
    public void delete(final VoidCallback callback) {
        invokeMethod("delete", getCommonParams(), new EmptyResponseParser(callback));
    }

    private Map<String, String> getCommonParams() {
        return ImmutableMap.of(
                "container", getContainer(),
                "name", getName());
    }
}




Java Source Code List

com.strongloop.android.loopback.AccessTokenRepository.java
com.strongloop.android.loopback.AccessToken.java
com.strongloop.android.loopback.ContainerRepository.java
com.strongloop.android.loopback.Container.java
com.strongloop.android.loopback.FileRepository.java
com.strongloop.android.loopback.File.java
com.strongloop.android.loopback.LocalInstallation.java
com.strongloop.android.loopback.ModelRepository.java
com.strongloop.android.loopback.Model.java
com.strongloop.android.loopback.RestAdapter.java
com.strongloop.android.loopback.RestRepository.java
com.strongloop.android.loopback.UserRepository.java
com.strongloop.android.loopback.User.java
com.strongloop.android.loopback.callbacks.EmptyResponseParser.java
com.strongloop.android.loopback.callbacks.JsonArrayParser.java
com.strongloop.android.loopback.callbacks.JsonObjectParser.java
com.strongloop.android.loopback.callbacks.ListCallback.java
com.strongloop.android.loopback.callbacks.ObjectCallback.java
com.strongloop.android.loopback.callbacks.VoidCallback.java
com.strongloop.android.remoting.BeanUtil.java
com.strongloop.android.remoting.JsonUtil.java
com.strongloop.android.remoting.Repository.java
com.strongloop.android.remoting.Transient.java
com.strongloop.android.remoting.VirtualObject.java
com.strongloop.android.remoting.adapters.Adapter.java
com.strongloop.android.remoting.adapters.RestAdapter.java
com.strongloop.android.remoting.adapters.RestContractItem.java
com.strongloop.android.remoting.adapters.RestContract.java
com.strongloop.android.remoting.adapters.StreamParam.java