1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.diyefi.openlogviewer.utils;
25
26 import java.io.BufferedInputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33 public final class Utilities {
34
35
36
37 private Utilities() {
38 }
39
40 public static String getExtension(final File f) {
41 String ext = null;
42 final String s = f.getName();
43 final int i = s.lastIndexOf('.');
44
45 if (i > 0 && i < s.length() - 1) {
46 ext = s.substring(i + 1).toLowerCase();
47 }
48 return ext;
49 }
50
51 public static int countBytes(final File f, final byte b) throws IOException {
52 InputStream is = new BufferedInputStream(new FileInputStream(f));
53 try {
54 byte[] data = new byte[8192];
55 int lines = 0;
56 int readCount = 0;
57 boolean empty = true;
58 while ((readCount = is.read(data)) != -1) {
59 empty = false;
60 for (int i = 0; i < readCount; ++i) {
61 if (data[i] == b) {
62 lines++;
63 }
64 }
65 }
66 return (lines == 0 && !empty) ? 1 : lines;
67 } finally {
68 is.close();
69 }
70 }
71
72 }