| 1 | package tw.nekomimi.nkmr; |
| 2 | |
| 3 | |
| 4 | import org.telegram.messenger.MessageObject; |
| 5 | |
| 6 | import java.io.ByteArrayInputStream; |
| 7 | import java.io.ByteArrayOutputStream; |
| 8 | import java.io.File; |
| 9 | import java.io.FileInputStream; |
| 10 | import java.io.FileNotFoundException; |
| 11 | import java.io.IOException; |
| 12 | import java.io.InputStream; |
| 13 | import java.security.MessageDigest; |
| 14 | import java.security.NoSuchAlgorithmException; |
| 15 | import java.util.zip.GZIPInputStream; |
| 16 | |
| 17 | public class NekomuraUtil { |
| 18 | /* |
| 19 | * Java文件操作 获取不带扩展名的文件名 |
| 20 | * |
| 21 | * Created on: 2011-8-2 |
| 22 | * Author: blueeagle |
| 23 | */ |
| 24 | public static String getFileNameNoEx(String filename) { |
| 25 | if ((filename != null) && (filename.length() > 0)) { |
| 26 | int dot = filename.lastIndexOf('.'); |
| 27 | if ((dot > -1) && (dot < (filename.length()))) { |
| 28 | return filename.substring(0, dot); |
| 29 | } |
| 30 | } |
| 31 | return filename; |
| 32 | } |
| 33 | |
| 34 | // 消息是否为文件 |
| 35 | public static boolean messageObjectIsFile(int type, MessageObject messageObject) { |
| 36 | boolean cansave = (type == 4 || type == 5 || type == 6 || type == 10); |
| 37 | boolean downloading = messageObject.loadedFileSize > 0; |
| 38 | |
| 39 | //图片的问题 |
| 40 | if (type == 4 && messageObject.getDocument() == null) { |
| 41 | return false; |
| 42 | } |
| 43 | return cansave || downloading; |
| 44 | } |
| 45 | |
| 46 | // 当文件有过加载过程,loadedFileSize > 0 ,所以不能用loadedFileSize判断是否正在下载 |
| 47 | public static boolean messageObjectIsDownloading(int type) { |
| 48 | boolean cansave = (type == 4 || type == 5 || type == 6 || type == 10); |
| 49 | return !cansave; |
| 50 | } |
| 51 | |
| 52 | // 取出整数的(二进制)某一位 |
| 53 | public static boolean getBit(int value, int i) { |
| 54 | int mask = 1 << i; |
| 55 | int ii = value & mask; |
| 56 | if (ii == 0) { |
| 57 | return false; |
| 58 | } else { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public static int setBit(int value, int i, boolean v) { |
| 64 | int mask = 1 << i; |
| 65 | if (v) { |
| 66 | value |= mask; |
| 67 | } else { |
| 68 | value &= ~mask; |
| 69 | } |
| 70 | return value; |
| 71 | } |
| 72 | |
| 73 | public static int reverseBit(int value, int i) { |
| 74 | int mask = 1 << i; |
| 75 | return value ^ mask; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * 取两个文本之间的文本值 |
| 80 | * |
| 81 | * @param text 源文本 比如:欲取全文本为 12345 |
| 82 | * @param left 文本前面 |
| 83 | * @param right 后面文本 |
| 84 | * @return 返回 String |
| 85 | */ |
| 86 | public static String getSubString(String text, String left, String right) { |
| 87 | String result = ""; |
| 88 | int zLen; |
| 89 | if (left == null || left.isEmpty()) { |
| 90 | zLen = 0; |
| 91 | } else { |
| 92 | zLen = text.indexOf(left); |
| 93 | if (zLen > -1) { |
| 94 | zLen += left.length(); |
| 95 | } else { |
| 96 | zLen = 0; |
| 97 | } |
| 98 | } |
| 99 | int yLen = text.indexOf(right, zLen); |
| 100 | if (yLen < 0 || right == null || right.isEmpty()) { |
| 101 | yLen = text.length(); |
| 102 | } |
| 103 | result = text.substring(zLen, yLen); |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | private static String convertToHex(byte[] data) { |
| 108 | StringBuilder buf = new StringBuilder(); |
| 109 | for (byte b : data) { |
| 110 | int halfbyte = (b >>> 4) & 0x0F; |
| 111 | int two_halfs = 0; |
| 112 | do { |
| 113 | buf.append((0 <= halfbyte) && (halfbyte <= 9) ? (char) ('0' + halfbyte) : (char) ('a' + (halfbyte - |
| 114 | halfbyte = b & 0x0F; |
| 115 | } while (two_halfs++ < 1); |
| 116 | } |
| 117 | return buf.toString(); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * Read the file and calculate the SHA-1 checksum |
| 123 | * |
| 124 | * @param file the file to read |
| 125 | * @return the hex representation of the SHA-1 using uppercase chars |
| 126 | * @throws FileNotFoundException if the file does not exist, is a directory rather than a |
| 127 | * regular file, or for some other reason cannot be opened for |
| 128 | * reading |
| 129 | * @throws IOException if an I/O error occurs |
| 130 | * @throws NoSuchAlgorithmException should never happen |
| 131 | */ |
| 132 | public static String calcSHA1(File file) throws FileNotFoundException, |
| 133 | IOException, NoSuchAlgorithmException { |
| 134 | |
| 135 | MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); |
| 136 | try (InputStream input = new FileInputStream(file)) { |
| 137 | |
| 138 | byte[] buffer = new byte[8192]; |
| 139 | int len = input.read(buffer); |
| 140 | |
| 141 | while (len != -1) { |
| 142 | sha1.update(buffer, 0, len); |
| 143 | len = input.read(buffer); |
| 144 | } |
| 145 | |
| 146 | return convertToHex(sha1.digest()); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public static byte[] uncompress(byte[] bytes) { |
| 151 | if (bytes == null || bytes.length == 0) { |
| 152 | return null; |
| 153 | } |
| 154 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 155 | ByteArrayInputStream in = new ByteArrayInputStream(bytes); |
| 156 | try { |
| 157 | GZIPInputStream ungzip = new GZIPInputStream(in); |
| 158 | byte[] buffer = new byte[256]; |
| 159 | int n; |
| 160 | while ((n = ungzip.read(buffer)) >= 0) { |
| 161 | out.write(buffer, 0, n); |
| 162 | } |
| 163 | } catch (Exception e) { |
| 164 | e.printStackTrace(); |
| 165 | } |
| 166 | return out.toByteArray(); |
| 167 | } |
| 168 | |
| 169 | public static boolean deleteDirectory(File directoryToBeDeleted) { |
| 170 | File[] allContents = directoryToBeDeleted.listFiles(); |
| 171 | if (allContents != null) { |
| 172 | for (File file : allContents) { |
| 173 | deleteDirectory(file); |
| 174 | } |
| 175 | } |
| 176 | return directoryToBeDeleted.delete(); |
| 177 | } |
| 178 | |
| 179 | } |