Android Open Source - mcdroid Mc I O






From Project

Back to project page mcdroid.

License

The source code is released under:

Apache License

If you think the Android project mcdroid 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

/**
 * /*  w ww .ja  v  a2  s. c om*/
 */
package cn.mibcxb.util;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cn.mibcxb.android.util.Logger;

/**
 * @author mibcxb
 * 
 */
public class McIO {
    private static final String TAG = Logger.getSimpleTag(McIO.class);

    public static final int BUFFER_SIZE = 8 * 1024;

    public static final void closeQuietly(Closeable is) {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    public static final int copy(InputStream is, OutputStream os)
            throws IOException {
        return copy(is, os, BUFFER_SIZE);
    }

    public static final int copy(InputStream is, OutputStream os, int bufferSize)
            throws IOException {
        if (is == null || os == null) {
            return -1;
        }
        if (bufferSize <= 0) {
            bufferSize = BUFFER_SIZE;
        }

        int count = 0;
        int length = 0;
        byte[] buffer = new byte[bufferSize];
        while ((length = is.read(buffer)) != -1) {
            os.write(buffer, 0, length);
            count += length;
        }
        os.flush();

        return count;
    }

    public static boolean createFile(File file) throws IOException {
        if (file == null) {
            Logger.e(TAG, "The file cannot be NULL.");
            return false;
        }

        if (file.exists() && file.isFile()) {
            Logger.e(TAG, "Already exists: " + file.getAbsolutePath());
            return true;
        } else {
            File parentFile = file.getParentFile();
            if (parentFile == null) {
                return file.createNewFile();
            } else {
                if (createDirectory(parentFile)) {
                    return file.createNewFile();
                } else {
                    Logger.e(TAG, "Create parent directory failed.");
                }
            }
        }
        return false;
    }

    public static boolean createDirectory(File dir) {
        if (dir == null) {
            Logger.e(TAG, "The dir cannot be NULL.");
            return false;
        }

        if (dir.exists() && dir.isDirectory()) {
            return true;
        } else {
            return dir.mkdirs();
        }
    }

    public static boolean deleteFile(File file) {
        if (file == null || !file.exists()) {
            Logger.e(TAG, "The file cannot be NULL.");
            return false;
        }

        if (file.isFile()) {
            return file.delete();
        } else {
            File[] children = file.listFiles();
            for (File child : children) {
                if (!deleteFile(child)) {
                    Logger.e(
                            TAG,
                            "Delete child file failed: "
                                    + child.getAbsolutePath());
                    return false;
                }
            }
            return file.delete();
        }
    }

    private McIO() {
    }
}




Java Source Code List

cn.mibcxb.acra.CrashReportWriter.java
cn.mibcxb.android.app.McApplication.java
cn.mibcxb.android.os.McHandler.java
cn.mibcxb.android.util.Logger.java
cn.mibcxb.android.util.NetworkWatcher.java
cn.mibcxb.util.McIO.java
cn.mibcxb.util.McMath.java