Android Open Source - EvokeFramework Util






From Project

Back to project page EvokeFramework.

License

The source code is released under:

Apache License

If you think the Android project EvokeFramework 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 org.fs.net.evoke.util;
//from ww  w  .j  av a  2s .  c  o  m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ThreadFactory;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by Fatih on 28/01/15.
 * as org.fs.net.evoke.util.Util
 */
public final class Util {

    private Util() { }

    /**
     * don't find Math.pow() function useful.
     * @param x element to get power of n
     * @param n number of the power
     * @return n times x
     */
    public static int pow(int x, int n) {
        int m;
        if(n == 0) {
            return  1;
        } else if(n % 2 == 0) {
            m = pow(x, n / 2);
            return m * m;
        } else  {
            return x * pow(x, n - 1);
        }
    }

    /**
     * Copy file a to b and remove a. 
     * @param source source file
     * @param destination destination file
     * @return true if success, false if fail.
     */
    public static boolean move(File source, File destination) {
        try {
            FileInputStream in = new FileInputStream(source);
            FileOutputStream out = new FileOutputStream(destination);
            byte[] buffer = new byte[1024 * 8];
            int size;
            while ((size = in.read(buffer)) >= 0) {
                out.write(buffer, 0, size);
            }
            in.close();
            out.close();
            source.delete();
            return true;
        } catch (Exception exp) {
            exp.printStackTrace();   
        }        
        return false;
    }

    /**
     * Zip files will be extracted this way.
     * @param source
     * @param destination
     */
    public static void extract(File source, File destination) {
        FileInputStream     in  = null;
        ZipInputStream      zin = null;
        FileOutputStream    out = null;
        try {
            in = new FileInputStream(source);
            zin = new ZipInputStream(in);
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                File file = new File(destination, entry.getName());
                if(entry.isDirectory()) {
                    file.mkdirs();
                } 
                else {
                    File parent = file.getParentFile();
                    if(!parent.exists()) {
                        parent.mkdirs();
                    }
                    out = new FileOutputStream(file);
                    byte[] buffer = new byte[8192];
                    int seed;
                    while ((seed = zin.read(buffer, 0, buffer.length)) != -1) {
                        out.write(buffer, 0, seed);
                    }
                    out.close();
                    out = null;
                }
            }
        }
        catch (IOException ioe) {
            if(LogUtil.isLogEnabled()) {
                ioe.printStackTrace();
            }
        }
        finally {
            try {
                zin.close();
                in.close();
                out.close();
            } catch (Exception e) {
                if(LogUtil.isLogEnabled()) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    /**
     * New thread creation factory
     * @param name name of thread
     * @param daemon if its daemon or not.
     * @return ThreadFactory instance initialized for given values.
     */
    public static ThreadFactory threadFactory(final String name, final boolean daemon) {
        return new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r, name);
                thread.setDaemon(daemon);
                return thread;
            }
        };
    }
}




Java Source Code List

org.fs.net.ApplicationTest.java
org.fs.net.MainActivity.java
org.fs.net.evoke.ApplicationTest.java
org.fs.net.evoke.DownloadManager.java
org.fs.net.evoke.core.AbstractObject.java
org.fs.net.evoke.data.Download.java
org.fs.net.evoke.data.Error.java
org.fs.net.evoke.data.HeadObject.java
org.fs.net.evoke.data.PartObject.java
org.fs.net.evoke.data.RequestObject.java
org.fs.net.evoke.database.DatabaseHelper.java
org.fs.net.evoke.listener.HeadCallback.java
org.fs.net.evoke.listener.PartCallback.java
org.fs.net.evoke.request.HeadRequest.java
org.fs.net.evoke.request.PartRequest.java
org.fs.net.evoke.th.AbstractRunnable.java
org.fs.net.evoke.util.JsonUtility.java
org.fs.net.evoke.util.LogUtil.java
org.fs.net.evoke.util.RequestUtility.java
org.fs.net.evoke.util.StringUtility.java
org.fs.net.evoke.util.Util.java