Util.java
Go to the documentation of this file.
00001 package com.introlab.rtabmap;
00002 
00003 import java.io.BufferedInputStream;
00004 import java.io.BufferedOutputStream;
00005 import java.io.File;
00006 import java.io.FileInputStream;
00007 import java.io.FileOutputStream;
00008 import java.io.FilenameFilter;
00009 import java.io.IOException;
00010 import java.util.Arrays;
00011 import java.util.zip.ZipEntry;
00012 import java.util.zip.ZipOutputStream;
00013 
00014 import android.content.Context;
00015 import android.net.ConnectivityManager;
00016 import android.net.NetworkInfo;
00017 import android.util.Log;
00018 
00019 public class Util {
00020         
00021         public static final int ZIP_BUFFER_SIZE = 1<<20; // 1MB
00022 
00023         public static void zip(String file, String zipFile) throws IOException {
00024                 Log.i(RTABMapActivity.TAG, "Zipping " + file +" to " + zipFile);
00025                 String[] files = new String[1];
00026                 files[0] = file;
00027                 zip(files, zipFile);
00028         }
00029 
00030         public static void zip(String[] files, String zipFile) throws IOException {
00031                 Log.i(RTABMapActivity.TAG, "Zipping " + String.valueOf(files.length) +" files to " + zipFile);
00032                 BufferedInputStream origin = null;
00033                 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
00034                 try { 
00035                         byte data[] = new byte[ZIP_BUFFER_SIZE];
00036 
00037                         for (int i = 0; i < files.length; i++) {
00038                                 FileInputStream fi = new FileInputStream(files[i]);    
00039                                 origin = new BufferedInputStream(fi, ZIP_BUFFER_SIZE);
00040                                 try {
00041                                         ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
00042                                         out.putNextEntry(entry);
00043                                         int count;
00044                                         while ((count = origin.read(data, 0, ZIP_BUFFER_SIZE)) != -1) {
00045                                                 out.write(data, 0, count);
00046                                         }
00047                                 }
00048                                 finally {
00049                                         origin.close();
00050                                 }
00051                         }
00052                 }
00053                 finally {
00054                         out.close();
00055                 }
00056         }
00057 
00058         public static String[] loadFileList(String directory, final boolean databasesOnly) {
00059                 File path = new File(directory); 
00060                 String fileList[];
00061                 try {
00062                         path.mkdirs();
00063                 }
00064                 catch(SecurityException e) {
00065                         Log.e(RTABMapActivity.TAG, "unable to write on the sd card " + e.toString());
00066                 }
00067                 if(path.exists()) {
00068                         FilenameFilter filter = new FilenameFilter() {
00069 
00070                                 @Override
00071                                 public boolean accept(File dir, String filename) {
00072                                         File sel = new File(dir, filename);
00073                                         if(databasesOnly)
00074                                         {
00075                                                 return filename.compareTo(RTABMapActivity.RTABMAP_TMP_DB) != 0 && filename.endsWith(".db");
00076                                         }
00077                                         else
00078                                         {
00079                                                 return sel.isFile();
00080                                         }
00081                                 }
00082 
00083                         };
00084                         fileList = path.list(filter);
00085                         Arrays.sort(fileList);
00086                 }
00087                 else {
00088                         fileList = new String[0];
00089                 }
00090                 return fileList;
00091         }
00092         
00108         public static int versionCompare(String str1, String str2) {
00109             String[] vals1 = str1.split("\\.");
00110             String[] vals2 = str2.split("\\.");
00111             int i = 0;
00112             // set index to first non-equal ordinal or length of shortest version string
00113             while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
00114               i++;
00115             }
00116             // compare first non-equal ordinal number
00117             if (i < vals1.length && i < vals2.length) {
00118                 int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
00119                 return Integer.signum(diff);
00120             }
00121             // the strings are equal or one string is a substring of the other
00122             // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
00123             return Integer.signum(vals1.length - vals2.length);
00124         }
00125         
00126 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:32