|
1 | 1 | package com.samsung.microbit.utils;
|
2 | 2 |
|
| 3 | +import android.content.Context; |
3 | 4 | import android.content.res.Resources;
|
| 5 | +import android.database.Cursor; |
| 6 | +import android.net.Uri; |
4 | 7 | import android.os.Environment;
|
| 8 | +import android.provider.DocumentsContract; |
5 | 9 | import android.util.Log;
|
6 | 10 |
|
7 | 11 | import com.samsung.microbit.MBApp;
|
8 | 12 | import com.samsung.microbit.data.constants.FileConstants;
|
9 | 13 |
|
10 | 14 | import java.io.BufferedOutputStream;
|
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.ByteArrayInputStream; |
11 | 17 | import java.io.File;
|
| 18 | +import java.io.FileInputStream; |
12 | 19 | import java.io.FileOutputStream;
|
13 | 20 | import java.io.IOException;
|
| 21 | +import java.io.InputStream; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.io.OutputStream; |
| 24 | +import java.net.URLDecoder; |
14 | 25 | import java.util.zip.ZipEntry;
|
15 | 26 | import java.util.zip.ZipInputStream;
|
16 | 27 |
|
|
19 | 30 | * file operations, such as file renaming, deleting and etc.
|
20 | 31 | */
|
21 | 32 | public class FileUtils {
|
| 33 | + private static final String TAG = FileUtils.class.getSimpleName(); |
22 | 34 |
|
23 | 35 | /**
|
24 | 36 | * Represents common results of rename operation.
|
@@ -110,4 +122,276 @@ public static String getFileSize(String filePath) {
|
110 | 122 | }
|
111 | 123 | return size;
|
112 | 124 | }
|
| 125 | + |
| 126 | + public static String fileNameFromPath( String fullPathOfFile) { |
| 127 | + String[] path = fullPathOfFile.split("/"); |
| 128 | + if ( path.length > 0) { |
| 129 | + String name = path[ path.length - 1]; |
| 130 | + if ( name.endsWith(".hex")) { |
| 131 | + return name; |
| 132 | + } |
| 133 | + } |
| 134 | + return null; |
| 135 | + } |
| 136 | + |
| 137 | + public static String fileNameFromUri(Uri uri, Context ctx) { |
| 138 | + if (uri != null) { |
| 139 | + String scheme = uri.getScheme(); |
| 140 | + if ( scheme != null && scheme.equals("file")) { |
| 141 | + String encodedPath = uri.getEncodedPath(); |
| 142 | + if (encodedPath != null) { |
| 143 | + String fullPathOfFile = URLDecoder.decode(encodedPath); |
| 144 | + if (fullPathOfFile != null) { |
| 145 | + return fileNameFromPath( fullPathOfFile); |
| 146 | + } |
| 147 | + } |
| 148 | + } else if ( scheme != null && scheme.equals("content")) { |
| 149 | + Cursor cursor = ctx.getContentResolver().query( uri, null, null, null, null); |
| 150 | + if ( cursor != null) |
| 151 | + { |
| 152 | + if ( cursor.moveToFirst()) { |
| 153 | + int index = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_DISPLAY_NAME); |
| 154 | + if ( index >= 0) { |
| 155 | + String name = cursor.getString( index); |
| 156 | + cursor.close(); |
| 157 | + return name; |
| 158 | + } |
| 159 | + } |
| 160 | + cursor.close(); |
| 161 | + } |
| 162 | + } else { |
| 163 | + Log.e( TAG, "Unsupported scheme " + scheme); |
| 164 | + } |
| 165 | + } |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + public static long fileSizeFromUri( Uri uri, Context ctx) { |
| 170 | + if (uri != null) { |
| 171 | + String scheme = uri.getScheme(); |
| 172 | + if ( scheme != null && scheme.equals("file")) { |
| 173 | + String path = uri.getPath(); |
| 174 | + if ( path != null) { |
| 175 | + File file = new File(path); |
| 176 | + return file.length(); |
| 177 | + } |
| 178 | + } else if ( scheme != null && scheme.equals("content")) { |
| 179 | + Cursor cursor = ctx.getContentResolver().query( uri, null, null, null, null); |
| 180 | + if ( cursor != null) |
| 181 | + { |
| 182 | + if ( cursor.moveToFirst()) { |
| 183 | + int index = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_SIZE); |
| 184 | + if ( index >= 0) { |
| 185 | + long size = cursor.getLong( index); |
| 186 | + cursor.close(); |
| 187 | + return size; |
| 188 | + } |
| 189 | + } |
| 190 | + cursor.close(); |
| 191 | + } |
| 192 | + } else { |
| 193 | + Log.e( TAG, "Unsupported scheme " + scheme); |
| 194 | + } |
| 195 | + } |
| 196 | + return -1; |
| 197 | + } |
| 198 | + |
| 199 | + public static byte[] readBytesFromInputStream(InputStream is, final int size) { |
| 200 | + byte[] bytes = null; |
| 201 | + try { |
| 202 | + bytes = new byte[size]; |
| 203 | + int remain = size; |
| 204 | + int offset = 0; |
| 205 | + while ( remain > 0) { |
| 206 | + int read = is.read( bytes, offset, remain); |
| 207 | + remain -= read; |
| 208 | + offset += read; |
| 209 | + } |
| 210 | + } catch ( Exception e){ |
| 211 | + bytes = null; |
| 212 | + } |
| 213 | + return bytes; |
| 214 | + } |
| 215 | + |
| 216 | + public static byte[] readBytesFromFile( File file) { |
| 217 | + byte[] bytes = null; |
| 218 | + try { |
| 219 | + FileInputStream is = new FileInputStream( file); |
| 220 | + int size = (int) file.length(); |
| 221 | + bytes = readBytesFromInputStream(is, size); |
| 222 | + is.close(); |
| 223 | + } catch ( Exception e){ |
| 224 | + bytes = null; |
| 225 | + } |
| 226 | + return bytes; |
| 227 | + } |
| 228 | + |
| 229 | + public static byte[] readBytesFromUri( Uri uri, Context ctx) { |
| 230 | + byte[] bytes = null; |
| 231 | + try { |
| 232 | + InputStream is = ctx.getContentResolver().openInputStream( uri); |
| 233 | + if ( is != null) { |
| 234 | + int size = (int) fileSizeFromUri( uri, ctx); |
| 235 | + bytes = readBytesFromInputStream( is, size); |
| 236 | + is.close(); |
| 237 | + } |
| 238 | + } catch ( Exception e){ |
| 239 | + bytes = null; |
| 240 | + } |
| 241 | + return bytes; |
| 242 | + } |
| 243 | + |
| 244 | + |
| 245 | + public static boolean stringBuilderAddStream( StringBuilder sb, InputStream is) { |
| 246 | + boolean ok = true; |
| 247 | + try { |
| 248 | + BufferedReader reader = new BufferedReader( new InputStreamReader( is)); |
| 249 | + try { |
| 250 | + while (true) { |
| 251 | + String line = reader.readLine(); |
| 252 | + if ( line == null) break; |
| 253 | + sb.append( line); |
| 254 | + sb.append("\n"); |
| 255 | + } |
| 256 | + } catch ( Exception e) { |
| 257 | + ok = false; |
| 258 | + } |
| 259 | + reader.close(); |
| 260 | + } catch ( Exception e) { |
| 261 | + ok = false; |
| 262 | + } |
| 263 | + return true; |
| 264 | + } |
| 265 | + |
| 266 | + public static boolean stringBuilderAddFile( StringBuilder sb, File file) { |
| 267 | + boolean ok = false; |
| 268 | + try { |
| 269 | + FileInputStream is = new FileInputStream( file); |
| 270 | + ok = stringBuilderAddStream( sb, is); |
| 271 | + is.close(); |
| 272 | + } catch ( Exception e) { |
| 273 | + ok = false; |
| 274 | + } |
| 275 | + return ok; |
| 276 | + } |
| 277 | + |
| 278 | + public static boolean stringBuilderAddUri( StringBuilder sb, Uri uri, Context ctx) { |
| 279 | + boolean ok = false; |
| 280 | + try { |
| 281 | + InputStream is = ctx.getContentResolver().openInputStream( uri); |
| 282 | + if ( is != null) { |
| 283 | + ok = stringBuilderAddStream(sb, is); |
| 284 | + is.close(); |
| 285 | + } |
| 286 | + } catch ( Exception e) { |
| 287 | + ok = false; |
| 288 | + } |
| 289 | + return ok; |
| 290 | + } |
| 291 | + |
| 292 | + public static String readStringFromHexFile( File file) { |
| 293 | + String hex = ""; |
| 294 | + StringBuilder sb = new StringBuilder(); |
| 295 | + if ( !stringBuilderAddFile(sb, file)) |
| 296 | + return null; |
| 297 | + return sb.toString(); |
| 298 | + } |
| 299 | + |
| 300 | + public static String readStringFromHexUri( Uri uri, Context ctx) { |
| 301 | + String hex = ""; |
| 302 | + StringBuilder sb = new StringBuilder(); |
| 303 | + if ( !stringBuilderAddUri(sb, uri, ctx)) |
| 304 | + return null; |
| 305 | + return sb.toString(); |
| 306 | + } |
| 307 | + |
| 308 | + public static boolean writeBytesToOutputStream( OutputStream os, byte [] bytes) |
| 309 | + { |
| 310 | + boolean ok = true; |
| 311 | + |
| 312 | + try { |
| 313 | + os.write( bytes); |
| 314 | + os.flush(); |
| 315 | + } catch ( Exception e) { |
| 316 | + ok = false; |
| 317 | + } |
| 318 | + return ok; |
| 319 | + } |
| 320 | + |
| 321 | + public static boolean writeBytesToFile( File file, byte [] bytes) |
| 322 | + { |
| 323 | + boolean ok = true; |
| 324 | + |
| 325 | + try { |
| 326 | + if ( file.exists()) { |
| 327 | + file.delete(); |
| 328 | + } |
| 329 | + file.createNewFile(); |
| 330 | + |
| 331 | + FileOutputStream os = new FileOutputStream(file); |
| 332 | + ok = writeBytesToOutputStream( os, bytes); |
| 333 | + os.close(); |
| 334 | + } catch ( Exception e) { |
| 335 | + ok = false; |
| 336 | + } |
| 337 | + return ok; |
| 338 | + } |
| 339 | + |
| 340 | + public static boolean writeBytesToUri( Uri uri, byte [] bytes, Context ctx) |
| 341 | + { |
| 342 | + boolean ok = true; |
| 343 | + |
| 344 | + try { |
| 345 | + OutputStream os = ctx.getContentResolver().openOutputStream( uri);; |
| 346 | + ok = writeBytesToOutputStream( os, bytes); |
| 347 | + os.close(); |
| 348 | + } catch ( Exception e) { |
| 349 | + ok = false; |
| 350 | + } |
| 351 | + return ok; |
| 352 | + } |
| 353 | + |
| 354 | + public static boolean isHex( byte [] bytes) { |
| 355 | + boolean ok = true; |
| 356 | + boolean found = false; |
| 357 | + for ( int i = 0; !found && i < 1024 && i < bytes.length - 1; i++) { |
| 358 | + if ( bytes[i] == '\n' && bytes[ i + 1] == ':') { |
| 359 | + found = true; |
| 360 | + } |
| 361 | + } |
| 362 | + if ( !found) { |
| 363 | + return false; |
| 364 | + } |
| 365 | + try { |
| 366 | + BufferedReader reader = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( bytes))); |
| 367 | + String line = null; |
| 368 | + while ( ok) { |
| 369 | + line = reader.readLine(); |
| 370 | + if ( line == null) { |
| 371 | + break; |
| 372 | + } |
| 373 | + if ( !line.isEmpty()) { |
| 374 | + if ( !line.startsWith(":")) { |
| 375 | + ok = false; |
| 376 | + } else { |
| 377 | + byte[] hex = line.getBytes(); |
| 378 | + int chk = irmHexUtils.lineCheck( hex,0); |
| 379 | + if ( chk < 0) { |
| 380 | + ok = false; |
| 381 | + } else { |
| 382 | + int sum = irmHexUtils.calcSum( hex,0); |
| 383 | + sum = ( chk + sum) % 256; |
| 384 | + if ( sum != 0 ) { |
| 385 | + ok = false; |
| 386 | + } |
| 387 | + } |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + } reader.close(); |
| 392 | + } catch (IOException e) { |
| 393 | + return false; |
| 394 | + } |
| 395 | + return ok; |
| 396 | + } |
113 | 397 | }
|
0 commit comments