content: /*
   *   Copyright (C)  2016 android@19code.com
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
   *   Unless required by applicable law or agreed to in writing, software
   *   distributed under the License is distributed on an "AS IS" BASIS,
   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *   See the License for the specific language governing permissions and
   *   limitations under the License.
   */
  
  package com.rsen.github.common;
  
  import android.content.Context;
  import android.content.Intent;
  import android.net.Uri;
  import android.text.format.Formatter;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.Closeable;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.nio.channels.FileChannel;
  import java.util.zip.GZIPInputStream;
  import java.util.zip.GZIPOutputStream;
  
  /**
   * Create by h4de5ing 2016/5/7 007
   */
  public class FileUtils {
      public static void closeIO(Closeable... closeables) {
          if (null == closeables || closeables.length <= 0) {
              return;
          }
          for (Closeable cb : closeables) {
              try {
                  if (null == cb) {
                      continue;
                  }
                  cb.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      public static boolean deleteFile(String filename) {
          return new File(filename).delete();
      }
  
      public static boolean isFileExist(String filePath) {
          return new File(filePath).exists();
      }
  
      public static boolean writeFile(String filename, String content) {
          boolean isSuccess = false;
          BufferedWriter bufferedWriter = null;
          try {
              bufferedWriter = new BufferedWriter(new FileWriter(filename, false));
              bufferedWriter.write(content);
              isSuccess = true;
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              closeIO(bufferedWriter);
          }
          return isSuccess;
      }
  
      public static String readFile(String filename) {
          File file = new File(filename);
          BufferedReader bufferedReader = null;
          String str = null;
          try {
              if (file.exists()) {
                  bufferedReader = new BufferedReader(new FileReader(filename));
                  str = bufferedReader.readLine();
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              closeIO(bufferedReader);
          }
          return str;
      }
  
      public static void copyFileFast(FileInputStream is, FileOutputStream os) throws IOException {
          FileChannel in = is.getChannel();
          FileChannel out = os.getChannel();
          in.transferTo(0, in.size(), out);
      }
  
      public static void shareFile(Context context, String title, String filePath) {
          Intent intent = new Intent(Intent.ACTION_SEND);
          Uri uri = Uri.parse("file://" + filePath);
          intent.setType("*/*");
          intent.putExtra(Intent.EXTRA_STREAM, uri);
          context.startActivity(Intent.createChooser(intent, title));
      }
  
      public static void zip(InputStream is, OutputStream os) {
          GZIPOutputStream gzip = null;
          try {
              gzip = new GZIPOutputStream(os);
              byte[] buf = new byte[1024];
              int len;
              while ((len = is.read(buf)) != -1) {
                  gzip.write(buf, 0, len);
                  gzip.flush();
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              closeIO(is);
              closeIO(gzip);
          }
      }
  
      public static void unzip(InputStream is, OutputStream os) {
          GZIPInputStream gzip = null;
          try {
              gzip = new GZIPInputStream(is);
              byte[] buf = new byte[1024];
              int len;
              while ((len = gzip.read(buf)) != -1) {
                  os.write(buf, 0, len);
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              closeIO(gzip);
              closeIO(os);
          }
      }
  
      public static String formatFileSize(Context context, long size) {
          return Formatter.formatFileSize(context, size);
      }
  
      public static void Stream2File(InputStream is, String fileName) {
          byte[] b = new byte[1024];
          int len;
          FileOutputStream os = null;
          try {
              os = new FileOutputStream(new File(fileName));
              while ((len = is.read(b)) != -1) {
                  os.write(b, 0, len);
                  os.flush();
              }
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              closeIO(is);
              closeIO(os);
          }
      }
  
      public static boolean createFolder(String filePath) {
          return createFolder(filePath, false);
      }
  
      public static boolean createFolder(String filePath, boolean recreate) {
          String folderName = getFolderName(filePath);
          if (folderName == null || folderName.length() == 0 || folderName.trim().length() == 0) {
              return false;
          }
          File folder = new File(folderName);
          if (folder.exists()) {
              if (recreate) {
                  deleteFile(folderName);
                  return folder.mkdirs();
              } else {
                  return true;
              }
          } else {
              return folder.mkdirs();
          }
      }
  
      public static String getFolderName(String filePath) {
          if (filePath == null || filePath.length() == 0 || filePath.trim().length() == 0) {
              return filePath;
          }
          int filePos = filePath.lastIndexOf(File.separator);
          return (filePos == -1) ? "" : filePath.substring(0, filePos);
      }
  
      public static boolean deleteFiles(String folder) {
          if (folder == null || folder.length() == 0 || folder.trim().length() == 0) {
              return true;
          }
          File file = new File(folder);
          if (!file.exists()) {
              return true;
          }
          if (file.isFile()) {
              return file.delete();
          }
          if (!file.isDirectory()) {
              return false;
          }
          for (File f : file.listFiles()) {
              if (f.isFile()) {
                  f.delete();
              } else if (f.isDirectory()) {
                  deleteFile(f.getAbsolutePath());
              }
          }
          return file.delete();
      }
  
      public static void openImage(Context mContext, String imagePath) {
          Intent intent = new Intent("android.intent.action.VIEW");
          intent.addCategory("android.intent.category.DEFAULT");
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          Uri uri = Uri.fromFile(new File(imagePath));
          intent.setDataAndType(uri, "image/*");
          mContext.startActivity(intent);
      }
  
      public static void openVideo(Context mContext, String videoPath) {
          Intent intent = new Intent("android.intent.action.VIEW");
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          intent.putExtra("oneshot", 0);
          intent.putExtra("configchange", 0);
          Uri uri = Uri.fromFile(new File(videoPath));
          intent.setDataAndType(uri, "video/*");
          mContext.startActivity(intent);
      }
  
      public static void openURL(Context mContext, String url) {
          Uri uri = Uri.parse(url);
          Intent intent = new Intent(Intent.ACTION_VIEW, uri);
          mContext.startActivity(intent);
      }
  
  }
  