|
4 | 4 | * Copyright 2007, FreeInternals.org. All rights reserved. |
5 | 5 | * Use is subject to license terms. |
6 | 6 | */ |
7 | | -package org.freeinternals.commonlib.util; |
| 7 | +package org.freeinternals.commonlib.core; |
8 | 8 |
|
9 | 9 | import java.io.File; |
10 | 10 | import java.io.IOException; |
|
16 | 16 | import java.util.logging.Logger; |
17 | 17 | import java.util.zip.ZipEntry; |
18 | 18 | import java.util.zip.ZipFile; |
19 | | -import javax.swing.tree.DefaultMutableTreeNode; |
20 | | -import org.freeinternals.commonlib.ui.JTreeNodeFileComponent; |
21 | 19 |
|
22 | 20 | /** |
23 | 21 | * Utility Class. |
24 | 22 | * |
25 | 23 | * @author Amos Shi |
26 | 24 | */ |
27 | | -public final class Tool { |
| 25 | +public final class BytesTool { |
28 | 26 |
|
29 | | - /** |
30 | | - * Returns byte array from the {@code file} |
31 | | - * |
32 | | - * @param file The file |
33 | | - * @return Byte array of the {@code file}, or {@code null} if error |
34 | | - * happened. |
35 | | - */ |
36 | | - public static byte[] readFileAsBytes(final File file) { |
37 | | - byte[] fileBytes = null; |
38 | | - try { |
39 | | - fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath())); |
40 | | - } catch (IOException ex) { |
41 | | - Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex); |
42 | | - } |
43 | | - |
44 | | - return fileBytes; |
45 | | - } |
46 | | - |
47 | | - /** |
48 | | - * Read byte array from {@code zipFile} of entry {@code zipEntry} |
49 | | - * |
50 | | - * @param zipFile The {@code jar} or {@code zip} file |
51 | | - * @param zipEntry The entry to be read |
52 | | - * @return Byte array of the class file, or {@code null} if error happened. |
53 | | - * @throws java.io.IOException Error happened when reading the zip file |
54 | | - */ |
55 | | - public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException { |
56 | | - if (zipFile == null) { |
57 | | - throw new IllegalArgumentException("Parameter 'zipFile' is null."); |
58 | | - } |
59 | | - if (zipEntry == null) { |
60 | | - throw new IllegalArgumentException("Parameter 'zipEntry' is null."); |
61 | | - } |
62 | | - |
63 | | - final long fileSize = zipEntry.getSize(); |
64 | | - final byte contents[] = new byte[(int) fileSize]; |
65 | | - ByteBuffer byteBuf = ByteBuffer.allocate(contents.length); |
66 | | - InputStream is = null; |
67 | | - int bytesRead = 0; |
68 | | - int bytesAll = 0; |
| 27 | + public static boolean isByteArrayEmpty(byte[] buff, int startPos, int length) { |
| 28 | + boolean result = false; |
69 | 29 |
|
70 | | - try { |
71 | | - is = zipFile.getInputStream(zipEntry); |
72 | | - while (true) { |
73 | | - bytesRead = is.read(contents); |
74 | | - if (bytesRead != -1) { |
75 | | - byteBuf.put(contents, 0, bytesRead); |
76 | | - bytesAll += bytesRead; |
77 | | - } else { |
| 30 | + if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) { |
| 31 | + result = true; |
| 32 | + for (int i = 1; i <= length; i++) { |
| 33 | + if (buff[startPos + i] != buff[startPos]) { |
| 34 | + result = false; |
78 | 35 | break; |
79 | 36 | } |
80 | 37 | } |
81 | | - } catch (IOException ex) { |
82 | | - Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex); |
83 | | - throw ex; |
84 | | - } |
85 | | - |
86 | | - if (bytesAll == fileSize) { |
87 | | - return byteBuf.array(); |
88 | | - } else { |
89 | | - throw new IOException(String.format( |
90 | | - "File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s", |
91 | | - fileSize, |
92 | | - byteBuf.array().length, |
93 | | - zipFile.getName(), |
94 | | - zipEntry.getName())); |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - /** |
99 | | - * Get a string for the {@code hex} view of byte array {@code data}. |
100 | | - * |
101 | | - * @param data Byte array |
102 | | - * @return A string representing the {@code hex} version of {@code data} |
103 | | - */ |
104 | | - public static String getByteDataHexView(final byte[] data) { |
105 | | - if (data == null) { |
106 | | - return ""; |
107 | | - } |
108 | | - if (data.length < 1) { |
109 | | - return ""; |
110 | | - } |
111 | | - |
112 | | - final StringBuilder sb = new StringBuilder(data.length * 5); |
113 | | - final int length = data.length; |
114 | | - int i; |
115 | | - int lineBreakCounter = 0; |
116 | | - for (i = 0; i < length; i++) { |
117 | | - sb.append(String.format(" %02X", data[i])); |
118 | | - lineBreakCounter++; |
119 | | - if (lineBreakCounter == 16) { |
120 | | - sb.append('\n'); |
121 | | - lineBreakCounter = 0; |
122 | | - } |
123 | 38 | } |
124 | | - sb.append('\n'); |
125 | 39 |
|
126 | | - return sb.toString(); |
| 40 | + return result; |
127 | 41 | } |
128 | 42 |
|
129 | 43 | /** |
@@ -195,44 +109,103 @@ public static boolean isByteArraySame(byte[] bin1, byte[] bin2, int start) { |
195 | 109 | } |
196 | 110 |
|
197 | 111 | /** |
198 | | - * |
199 | | - * @param parentNode |
200 | | - * @param lastEnd |
201 | | - * @param diff |
202 | | - * @param buff |
203 | | - * @param buffStartPos |
| 112 | + * Get a string for the {@code hex} view of byte array {@code data}. |
| 113 | + * |
| 114 | + * @param data Byte array |
| 115 | + * @return A string representing the {@code hex} version of {@code data} |
204 | 116 | */ |
205 | | - public static void generateTreeNode_Diff( |
206 | | - DefaultMutableTreeNode parentNode, |
207 | | - int lastEnd, |
208 | | - int diff, |
209 | | - byte[] buff, int buffStartPos) { |
210 | | - String diffStr; |
211 | | - |
212 | | - if (Tool.isBuffEmpty(buff, lastEnd - buffStartPos, diff - 1)) { |
213 | | - diffStr = String.format("Empty [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff); |
214 | | - } else { |
215 | | - diffStr = String.format("Unknown [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff); |
| 117 | + public static String getByteDataHexView(final byte[] data) { |
| 118 | + if (data == null) { |
| 119 | + return ""; |
| 120 | + } |
| 121 | + if (data.length < 1) { |
| 122 | + return ""; |
| 123 | + } |
| 124 | + |
| 125 | + final StringBuilder sb = new StringBuilder(data.length * 5); |
| 126 | + final int length = data.length; |
| 127 | + int i; |
| 128 | + int lineBreakCounter = 0; |
| 129 | + for (i = 0; i < length; i++) { |
| 130 | + sb.append(String.format(" %02X", data[i])); |
| 131 | + lineBreakCounter++; |
| 132 | + if (lineBreakCounter == 16) { |
| 133 | + sb.append('\n'); |
| 134 | + lineBreakCounter = 0; |
| 135 | + } |
216 | 136 | } |
217 | | - parentNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent( |
218 | | - lastEnd, |
219 | | - diff, |
220 | | - diffStr))); |
| 137 | + sb.append('\n'); |
| 138 | + |
| 139 | + return sb.toString(); |
221 | 140 | } |
222 | 141 |
|
223 | | - private static boolean isBuffEmpty(byte[] buff, int startPos, int length) { |
224 | | - boolean result = false; |
| 142 | + /** |
| 143 | + * Returns byte array from the {@code file} |
| 144 | + * |
| 145 | + * @param file The file |
| 146 | + * @return Byte array of the {@code file}, or {@code null} if error |
| 147 | + * happened. |
| 148 | + */ |
| 149 | + public static byte[] readFileAsBytes(final File file) { |
| 150 | + byte[] fileBytes = null; |
| 151 | + try { |
| 152 | + fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath())); |
| 153 | + } catch (IOException ex) { |
| 154 | + Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex); |
| 155 | + } |
225 | 156 |
|
226 | | - if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) { |
227 | | - result = true; |
228 | | - for (int i = 1; i <= length; i++) { |
229 | | - if (buff[startPos + i] != buff[startPos]) { |
230 | | - result = false; |
| 157 | + return fileBytes; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Read byte array from {@code zipFile} of entry {@code zipEntry} |
| 162 | + * |
| 163 | + * @param zipFile The {@code jar} or {@code zip} file |
| 164 | + * @param zipEntry The entry to be read |
| 165 | + * @return Byte array of the class file, or {@code null} if error happened. |
| 166 | + * @throws java.io.IOException Error happened when reading the zip file |
| 167 | + */ |
| 168 | + public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException { |
| 169 | + if (zipFile == null) { |
| 170 | + throw new IllegalArgumentException("Parameter 'zipFile' is null."); |
| 171 | + } |
| 172 | + if (zipEntry == null) { |
| 173 | + throw new IllegalArgumentException("Parameter 'zipEntry' is null."); |
| 174 | + } |
| 175 | + |
| 176 | + final long fileSize = zipEntry.getSize(); |
| 177 | + final byte contents[] = new byte[(int) fileSize]; |
| 178 | + ByteBuffer byteBuf = ByteBuffer.allocate(contents.length); |
| 179 | + InputStream is = null; |
| 180 | + int bytesRead = 0; |
| 181 | + int bytesAll = 0; |
| 182 | + |
| 183 | + try { |
| 184 | + is = zipFile.getInputStream(zipEntry); |
| 185 | + while (true) { |
| 186 | + bytesRead = is.read(contents); |
| 187 | + if (bytesRead != -1) { |
| 188 | + byteBuf.put(contents, 0, bytesRead); |
| 189 | + bytesAll += bytesRead; |
| 190 | + } else { |
231 | 191 | break; |
232 | 192 | } |
233 | 193 | } |
| 194 | + } catch (IOException ex) { |
| 195 | + Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex); |
| 196 | + throw ex; |
234 | 197 | } |
235 | 198 |
|
236 | | - return result; |
| 199 | + if (bytesAll == fileSize) { |
| 200 | + return byteBuf.array(); |
| 201 | + } else { |
| 202 | + throw new IOException(String.format( |
| 203 | + "File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s", |
| 204 | + fileSize, |
| 205 | + byteBuf.array().length, |
| 206 | + zipFile.getName(), |
| 207 | + zipEntry.getName())); |
| 208 | + } |
237 | 209 | } |
| 210 | + |
238 | 211 | } |
0 commit comments