Util.java
Go to the documentation of this file.
1 package com.introlab.rtabmap;
2 
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.FilenameFilter;
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.Comparator;
14 import java.util.List;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipOutputStream;
17 
18 import android.content.Context;
19 import android.net.ConnectivityManager;
20 import android.net.NetworkInfo;
21 import android.util.Log;
22 
23 public class Util {
24 
25  public static final int ZIP_BUFFER_SIZE = 1<<20; // 1MB
26 
27  public static void zip(String file, String zipFile) throws IOException {
28  Log.i(RTABMapActivity.TAG, "Zipping " + file +" to " + zipFile);
29  String[] files = new String[1];
30  files[0] = file;
31  zip(files, zipFile);
32  }
33 
34  public static void zip(String[] files, String zipFile) throws IOException {
35  Log.i(RTABMapActivity.TAG, "Zipping " + String.valueOf(files.length) +" files to " + zipFile);
36  BufferedInputStream origin = null;
37  ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
38  try {
39  byte data[] = new byte[ZIP_BUFFER_SIZE];
40 
41  for (int i = 0; i < files.length; i++) {
42  FileInputStream fi = new FileInputStream(files[i]);
43  origin = new BufferedInputStream(fi, ZIP_BUFFER_SIZE);
44  try {
45  ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
46  out.putNextEntry(entry);
47  int count;
48  while ((count = origin.read(data, 0, ZIP_BUFFER_SIZE)) != -1) {
49  out.write(data, 0, count);
50  }
51  }
52  finally {
53  origin.close();
54  }
55  }
56  }
57  finally {
58  out.close();
59  }
60  }
61 
62  public static String[] loadFileList(final String directory, final boolean databasesOnly) {
63  File path = new File(directory);
64  String fileList[];
65  try {
66  path.mkdirs();
67  }
68  catch(SecurityException e) {
69  Log.e(RTABMapActivity.TAG, "unable to write on the sd card " + e.toString());
70  }
71  if(path.exists()) {
72  FilenameFilter filter = new FilenameFilter() {
73 
74  @Override
75  public boolean accept(File dir, String filename) {
76  File sel = new File(dir, filename);
77  if(databasesOnly)
78  {
79  return filename.compareTo(RTABMapActivity.RTABMAP_TMP_DB) != 0 && filename.endsWith(".db");
80  }
81  else
82  {
83  return sel.isFile();
84  }
85  }
86 
87  };
88  fileList = path.list(filter);
89  Arrays.sort(fileList);
90  List<String> fileListt = new ArrayList<String>(Arrays.asList(fileList));
91  Collections.sort(fileListt, new Comparator<String>() {
92 
93  @Override
94  public int compare(String filename1, String filename2) {
95  File file1 = new File(directory+"/"+filename1);
96  File file2 = new File(directory+"/"+filename2);
97  long k = file1.lastModified() - file2.lastModified();
98  if(k > 0){
99  return -1;
100  }else if(k == 0){
101  return 0;
102  }else{
103  return 1;
104  }
105  }
106  });
107  fileListt.toArray(fileList);
108 
109  }
110  else {
111  fileList = new String[0];
112  }
113  return fileList;
114  }
115 
131  public static int versionCompare(String str1, String str2) {
132  String[] vals1 = str1.split("\\.");
133  String[] vals2 = str2.split("\\.");
134  int i = 0;
135  // set index to first non-equal ordinal or length of shortest version string
136  while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
137  i++;
138  }
139  // compare first non-equal ordinal number
140  if (i < vals1.length && i < vals2.length) {
141  int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
142  return Integer.signum(diff);
143  }
144  // the strings are equal or one string is a substring of the other
145  // e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
146  return Integer.signum(vals1.length - vals2.length);
147  }
148 
149 }
static String [] loadFileList(final String directory, final boolean databasesOnly)
Definition: Util.java:62
data
GLM_FUNC_DECL genType e()
static void zip(String file, String zipFile)
Definition: Util.java:27
static void zip(String[] files, String zipFile)
Definition: Util.java:34
diff
unsigned char byte
static final int ZIP_BUFFER_SIZE
Definition: Util.java:25
static int versionCompare(String str1, String str2)
Definition: Util.java:131


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:58