提交 | 用户 | 时间
|
ce910c
|
1 |
package com.netsdk.lib; |
H |
2 |
|
|
3 |
import java.awt.event.ActionEvent; |
|
4 |
import java.awt.event.KeyEvent; |
|
5 |
import java.awt.event.KeyListener; |
|
6 |
import java.awt.image.BufferedImage; |
|
7 |
import java.io.File; |
|
8 |
import java.io.FileInputStream; |
|
9 |
import java.io.FileOutputStream; |
|
10 |
import java.io.IOException; |
|
11 |
import java.io.UnsupportedEncodingException; |
|
12 |
import java.text.SimpleDateFormat; |
|
13 |
import java.util.Date; |
|
14 |
|
|
15 |
import javax.imageio.ImageIO; |
|
16 |
import javax.swing.JButton; |
|
17 |
import javax.swing.JFileChooser; |
|
18 |
import javax.swing.JFrame; |
|
19 |
import javax.swing.JTextField; |
|
20 |
import javax.swing.UIManager; |
|
21 |
import javax.swing.filechooser.FileFilter; |
|
22 |
|
|
23 |
import com.netsdk.common.ErrorCode; |
|
24 |
import com.netsdk.common.PaintPanel; |
|
25 |
import com.netsdk.demo.module.LoginModule; |
|
26 |
import com.netsdk.lib.NetSDKLib.LLong; |
|
27 |
|
|
28 |
import com.sun.jna.Memory; |
|
29 |
import com.sun.jna.Pointer; |
|
30 |
import com.sun.jna.Structure; |
|
31 |
import com.sun.jna.ptr.IntByReference; |
|
32 |
|
|
33 |
public class ToolKits { |
|
34 |
static NetSDKLib netsdkapi = NetSDKLib.NETSDK_INSTANCE; |
|
35 |
static NetSDKLib configapi = NetSDKLib.CONFIG_INSTANCE; |
|
36 |
|
|
37 |
/*************************************************************************************************** |
|
38 |
* 工具方法 * |
|
39 |
***************************************************************************************************/ |
|
40 |
public static void GetPointerData(Pointer pNativeData, Structure pJavaStu) |
|
41 |
{ |
|
42 |
GetPointerDataToStruct(pNativeData, 0, pJavaStu); |
|
43 |
} |
|
44 |
|
|
45 |
public static void GetPointerDataToStruct(Pointer pNativeData, long OffsetOfpNativeData, Structure pJavaStu) { |
|
46 |
pJavaStu.write(); |
|
47 |
Pointer pJavaMem = pJavaStu.getPointer(); |
|
48 |
pJavaMem.write(0, pNativeData.getByteArray(OffsetOfpNativeData, pJavaStu.size()), 0, |
|
49 |
pJavaStu.size()); |
|
50 |
pJavaStu.read(); |
|
51 |
} |
|
52 |
|
|
53 |
public static void GetPointerDataToStructArr(Pointer pNativeData, Structure []pJavaStuArr) { |
|
54 |
long offset = 0; |
|
55 |
for (int i=0; i<pJavaStuArr.length; ++i) |
|
56 |
{ |
|
57 |
GetPointerDataToStruct(pNativeData, offset, pJavaStuArr[i]); |
|
58 |
offset += pJavaStuArr[i].size(); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* 将结构体数组拷贝到内存 |
|
64 |
* @param pNativeData |
|
65 |
* @param pJavaStuArr |
|
66 |
*/ |
|
67 |
public static void SetStructArrToPointerData(Structure []pJavaStuArr, Pointer pNativeData) { |
|
68 |
long offset = 0; |
|
69 |
for (int i = 0; i < pJavaStuArr.length; ++i) { |
|
70 |
SetStructDataToPointer(pJavaStuArr[i], pNativeData, offset); |
|
71 |
offset += pJavaStuArr[i].size(); |
|
72 |
} |
|
73 |
} |
|
74 |
|
|
75 |
public static void SetStructDataToPointer(Structure pJavaStu, Pointer pNativeData, long OffsetOfpNativeData){ |
|
76 |
pJavaStu.write(); |
|
77 |
Pointer pJavaMem = pJavaStu.getPointer(); |
|
78 |
pNativeData.write(OffsetOfpNativeData, pJavaMem.getByteArray(0, pJavaStu.size()), 0, pJavaStu.size()); |
|
79 |
} |
|
80 |
|
|
81 |
public static void savePicture(byte[] pBuf, String sDstFile) throws IOException |
|
82 |
{ |
|
83 |
FileOutputStream fos=null; |
|
84 |
try |
|
85 |
{ |
|
86 |
fos = new FileOutputStream(sDstFile); |
|
87 |
fos.write(pBuf); |
|
88 |
} catch (Exception e){ |
|
89 |
e.printStackTrace(); |
|
90 |
}finally{ |
|
91 |
fos.close(); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
public static void savePicture(byte[] pBuf, int dwBufOffset, int dwBufSize, String sDstFile) throws IOException |
|
96 |
{ |
|
97 |
FileOutputStream fos=null; |
|
98 |
try |
|
99 |
{ |
|
100 |
fos = new FileOutputStream(sDstFile); |
|
101 |
fos.write(pBuf, dwBufOffset, dwBufSize); |
|
102 |
} catch (Exception e){ |
|
103 |
e.printStackTrace(); |
|
104 |
}finally{ |
|
105 |
fos.close(); |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
public static void savePicture(Pointer pBuf, int dwBufSize, String sDstFile) throws IOException |
|
110 |
{ |
|
111 |
FileOutputStream fos=null; |
|
112 |
try |
|
113 |
{ |
|
114 |
fos = new FileOutputStream(sDstFile); |
|
115 |
fos.write(pBuf.getByteArray(0, dwBufSize), 0, dwBufSize); |
|
116 |
} catch (Exception e){ |
|
117 |
e.printStackTrace(); |
|
118 |
}finally{ |
|
119 |
fos.close(); |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
public static void savePicture(Pointer pBuf, int dwBufOffset, int dwBufSize, String sDstFile) throws IOException |
|
124 |
{ |
|
125 |
FileOutputStream fos=null; |
|
126 |
try |
|
127 |
{ |
|
128 |
fos = new FileOutputStream(sDstFile); |
|
129 |
fos.write(pBuf.getByteArray(dwBufOffset, dwBufSize), 0, dwBufSize); |
|
130 |
} catch (Exception e){ |
|
131 |
e.printStackTrace(); |
|
132 |
}finally{ |
|
133 |
fos.close(); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
// 将Pointer值转为byte[] |
|
138 |
public static String GetPointerDataToByteArr(Pointer pointer) { |
|
139 |
String str = ""; |
|
140 |
if(pointer == null) { |
|
141 |
return str; |
|
142 |
} |
|
143 |
|
|
144 |
int length = 0; |
|
145 |
byte[] bufferPlace = new byte[1]; |
|
146 |
|
|
147 |
for(int i = 0; i < 2048; i++) { |
|
148 |
pointer.read(i, bufferPlace, 0, 1); |
|
149 |
if(bufferPlace[0] == '\0') { |
|
150 |
length = i; |
|
151 |
break; |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
if(length > 0) { |
|
156 |
byte[] buffer = new byte[length]; |
|
157 |
pointer.read(0, buffer, 0, length); |
|
158 |
try { |
|
159 |
str = new String(buffer, "GBK").trim(); |
|
160 |
} catch (UnsupportedEncodingException e) { |
|
161 |
return str; |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
return str; |
|
166 |
} |
|
167 |
|
|
168 |
// 获取当前时间 |
|
169 |
public static String getDate() { |
|
170 |
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
171 |
String date = simpleDate.format(new Date()).replace(" ", "_").replace(":", "-"); |
|
172 |
|
|
173 |
return date; |
|
174 |
} |
|
175 |
|
|
176 |
// 获取当前时间 |
|
177 |
public static String getDate(Date curDate) { |
|
178 |
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|
179 |
String date = simpleDate.format(curDate).replace(" ", "_").replace(":", "-"); |
|
180 |
|
|
181 |
return date; |
|
182 |
} |
|
183 |
|
|
184 |
// 获取当前时间 |
|
185 |
public static String getDay() { |
|
186 |
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd"); |
|
187 |
String date = simpleDate.format(new Date()); |
|
188 |
return date; |
|
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
// 限制JTextField 长度,以及内容 |
|
193 |
public static void limitTextFieldLength(final JTextField jTextField, final int size) { |
|
194 |
jTextField.addKeyListener(new KeyListener() { |
|
195 |
|
|
196 |
@Override |
|
197 |
public void keyTyped(KeyEvent e) { |
|
198 |
String number = "0123456789" + (char)8; |
|
199 |
if(number.indexOf(e.getKeyChar()) < 0 || jTextField.getText().trim().length() >= size) { |
|
200 |
e.consume(); |
|
201 |
return; |
|
202 |
} |
|
203 |
} |
|
204 |
|
|
205 |
@Override |
|
206 |
public void keyReleased(KeyEvent e) { |
|
207 |
} |
|
208 |
|
|
209 |
@Override |
|
210 |
public void keyPressed(KeyEvent e) { |
|
211 |
} |
|
212 |
}); |
|
213 |
} |
|
214 |
|
|
215 |
// 获取当前窗口 |
|
216 |
public static JFrame getFrame(ActionEvent e) { |
|
217 |
JButton btn = (JButton)e.getSource(); |
|
218 |
JFrame frame = (JFrame)btn.getRootPane().getParent(); |
|
219 |
|
|
220 |
return frame; |
|
221 |
} |
|
222 |
|
|
223 |
// 获取操作平台信息 |
|
224 |
public static String getLoadLibrary(String library) { |
|
225 |
String path = ""; |
|
226 |
String os = System.getProperty("os.name"); |
|
227 |
if(os.toLowerCase().startsWith("win")) { |
|
228 |
path = "./libs/"; |
|
229 |
} else if(os.toLowerCase().startsWith("linux")) { |
|
230 |
path = ""; |
|
231 |
} |
|
232 |
|
|
233 |
return (path + library); |
|
234 |
} |
|
235 |
|
|
236 |
public static String getOsName() { |
|
237 |
String osName = ""; |
|
238 |
String os = System.getProperty("os.name"); |
|
239 |
if(os.toLowerCase().startsWith("win")) { |
|
240 |
osName = "win"; |
|
241 |
} else if(os.toLowerCase().startsWith("linux")) { |
|
242 |
osName = "linux"; |
|
243 |
} |
|
244 |
|
|
245 |
return osName; |
|
246 |
} |
|
247 |
|
|
248 |
/** |
|
249 |
* 读取图片大小 |
|
250 |
* @param filePath 图片路径 |
|
251 |
* @return |
|
252 |
*/ |
|
253 |
public static long GetFileSize(String filePath) { |
|
254 |
File f = new File(filePath); |
|
255 |
if (f.exists() && f.isFile()) { |
|
256 |
return f.length(); |
|
257 |
} |
|
258 |
else |
|
259 |
{ |
|
260 |
return 0; |
|
261 |
} |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* 读取图片数据 |
|
266 |
* @param file 图片路径 |
|
267 |
* @param memory 图片数据缓存 |
|
268 |
* @return |
|
269 |
* @throws IOException |
|
270 |
*/ |
|
271 |
public static boolean ReadAllFileToMemory(String file, Memory memory) throws IOException { |
|
272 |
if (memory != Memory.NULL) |
|
273 |
{ |
|
274 |
long fileLen = GetFileSize(file); |
|
275 |
if (fileLen <= 0) |
|
276 |
{ |
|
277 |
return false; |
|
278 |
} |
|
279 |
FileInputStream in =null; |
|
280 |
try { |
|
281 |
File infile = new File(file); |
|
282 |
if (infile.canRead()) |
|
283 |
{ |
|
284 |
in = new FileInputStream(infile); |
|
285 |
int buffLen = 1024; |
|
286 |
byte[] buffer = new byte[buffLen]; |
|
287 |
long currFileLen = 0; |
|
288 |
int readLen = 0; |
|
289 |
while (currFileLen < fileLen) |
|
290 |
{ |
|
291 |
readLen = in.read(buffer); |
|
292 |
memory.write(currFileLen, buffer, 0, readLen); |
|
293 |
currFileLen += readLen; |
|
294 |
} |
|
295 |
return true; |
|
296 |
} |
|
297 |
else |
|
298 |
{ |
|
299 |
System.err.println("Failed to open file %s for read!!!\n"); |
|
300 |
return false; |
|
301 |
} |
|
302 |
}catch (Exception e) |
|
303 |
{ |
|
304 |
System.err.println("Failed to open file %s for read!!!\n"); |
|
305 |
e.printStackTrace(); |
|
306 |
}finally{ |
|
307 |
if(in!=null){ |
|
308 |
in.close(); |
|
309 |
} |
|
310 |
} |
|
311 |
} |
|
312 |
|
|
313 |
return false; |
|
314 |
} |
|
315 |
|
|
316 |
static class JPGFilter extends FileFilter { |
|
317 |
public boolean accept(File f) { |
|
318 |
if(f.getName().toLowerCase().endsWith(".JPG") |
|
319 |
|| f.getName().toLowerCase().endsWith(".jpg") |
|
320 |
|| f.isDirectory()) { |
|
321 |
return true; |
|
322 |
} |
|
323 |
return false; |
|
324 |
} |
|
325 |
|
|
326 |
@Override |
|
327 |
public String getDescription() { |
|
328 |
return "*.jpg; *.JPG"; |
|
329 |
} |
|
330 |
} |
|
331 |
|
|
332 |
/* |
|
333 |
* 用选择器选择图片, 获取图片路径,并在界面显示 |
|
334 |
*/ |
|
335 |
public static String openPictureFile(PaintPanel paintPanel) { |
|
336 |
try { |
|
337 |
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
|
338 |
} catch (Exception e) { |
|
339 |
e.printStackTrace(); |
|
340 |
} |
|
341 |
|
|
342 |
String picPath = ""; |
|
343 |
|
|
344 |
// 读取图片 |
|
345 |
JFileChooser jfc = new JFileChooser("d:/"); |
|
346 |
jfc.setMultiSelectionEnabled(false); // 不可以拖选多个文件 |
|
347 |
jfc.setAcceptAllFileFilterUsed(false); // 关闭显示所有 |
|
348 |
|
|
349 |
//添加过滤器 |
|
350 |
JPGFilter filter = new JPGFilter(); |
|
351 |
jfc.addChoosableFileFilter(filter); |
|
352 |
jfc.setFileFilter(filter); |
|
353 |
|
|
354 |
if( jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { |
|
355 |
picPath = jfc.getSelectedFile().getAbsolutePath(); |
|
356 |
|
|
357 |
/* |
|
358 |
* 读取本地图片, 并在面板上显示 |
|
359 |
*/ |
|
360 |
BufferedImage bufferedImage = null; |
|
361 |
if(picPath == null || picPath.equals("")) { |
|
362 |
return ""; |
|
363 |
} |
|
364 |
|
|
365 |
File file = new File(picPath); |
|
366 |
if(!file.exists()) { |
|
367 |
return ""; |
|
368 |
} |
|
369 |
|
|
370 |
try { |
|
371 |
bufferedImage = ImageIO.read(file); |
|
372 |
} catch (IOException e) { |
|
373 |
e.printStackTrace(); |
|
374 |
} |
|
375 |
|
|
376 |
if(bufferedImage == null) { |
|
377 |
paintPanel.setOpaque(true); |
|
378 |
paintPanel.repaint(); |
|
379 |
|
|
380 |
System.err.println("打开图片失败,请重新选择!"); |
|
381 |
return ""; |
|
382 |
} else { |
|
383 |
paintPanel.setOpaque(false); |
|
384 |
paintPanel.setImage(bufferedImage); |
|
385 |
paintPanel.repaint(); |
|
386 |
} |
|
387 |
} |
|
388 |
|
|
389 |
try { |
|
390 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
|
391 |
} catch (Exception e) { |
|
392 |
e.printStackTrace(); |
|
393 |
} |
|
394 |
return picPath; |
|
395 |
} |
|
396 |
|
|
397 |
/* |
|
398 |
* 传入图片路径, 打开图片, 并在面板显示 |
|
399 |
*/ |
|
400 |
public static File openPictureFile(String picPath, PaintPanel paintPanel) { |
|
401 |
/* |
|
402 |
* 读取本地图片, 并在面板上显示 |
|
403 |
*/ |
|
404 |
BufferedImage bufferedImage = null; |
|
405 |
if(picPath == null || picPath.equals("")) { |
|
406 |
return null; |
|
407 |
} |
|
408 |
|
|
409 |
File file = new File(picPath); |
|
410 |
if(!file.exists()) { |
|
411 |
return null; |
|
412 |
} |
|
413 |
|
|
414 |
try { |
|
415 |
bufferedImage = ImageIO.read(file); |
|
416 |
} catch (IOException e) { |
|
417 |
e.printStackTrace(); |
|
418 |
} |
|
419 |
|
|
420 |
if(bufferedImage == null) { |
|
421 |
paintPanel.setOpaque(true); |
|
422 |
paintPanel.repaint(); |
|
423 |
|
|
424 |
System.err.println("打开图片失败,请重新选择!"); |
|
425 |
return null; |
|
426 |
} else { |
|
427 |
paintPanel.setOpaque(false); |
|
428 |
paintPanel.setImage(bufferedImage); |
|
429 |
paintPanel.repaint(); |
|
430 |
} |
|
431 |
|
|
432 |
return file; |
|
433 |
} |
|
434 |
|
|
435 |
/** |
|
436 |
* 读取图片 |
|
437 |
* @return 图片缓存 |
|
438 |
* @throws IOException |
|
439 |
*/ |
|
440 |
public static Memory readPictureFile(String picPath) throws IOException { |
|
441 |
int nPicBufLen = 0; |
|
442 |
Memory memory = null; |
|
443 |
|
|
444 |
/* |
|
445 |
* 读取本地图片大小 |
|
446 |
*/ |
|
447 |
nPicBufLen = (int)ToolKits.GetFileSize(picPath); |
|
448 |
|
|
449 |
// 读取文件大小失败 |
|
450 |
if (nPicBufLen <= 0) { |
|
451 |
System.err.println("读取图片大小失败,请重新选择!"); |
|
452 |
return null; |
|
453 |
} |
|
454 |
|
|
455 |
/* |
|
456 |
* 读取图片缓存 |
|
457 |
*/ |
|
458 |
memory = new Memory(nPicBufLen); // 申请缓存 |
|
459 |
memory.clear(); |
|
460 |
|
|
461 |
if (!ToolKits.ReadAllFileToMemory(picPath, memory)) { |
|
462 |
System.err.println("读取图片数据,请重新选择!"); |
|
463 |
return null; |
|
464 |
} |
|
465 |
|
|
466 |
return memory; |
|
467 |
} |
|
468 |
|
|
469 |
/** |
|
470 |
* 登录设备设备错误状态, 用于界面显示 |
|
471 |
*/ |
|
472 |
public static String getErrorCodeShow() { |
|
473 |
return ErrorCode.getErrorCode(LoginModule.netsdk.CLIENT_GetLastError()); |
|
474 |
} |
|
475 |
|
|
476 |
/** |
|
477 |
* 获取接口错误码和错误信息,用于打印 |
|
478 |
* @return |
|
479 |
*/ |
|
480 |
public static String getErrorCodePrint() { |
|
481 |
return "\n{error code: (0x80000000|" + (LoginModule.netsdk.CLIENT_GetLastError() & 0x7fffffff) +").参考 NetSDKLib.java }" |
|
482 |
+ " - {error info:" + ErrorCode.getErrorCode(LoginModule.netsdk.CLIENT_GetLastError()) + "}\n"; |
|
483 |
} |
|
484 |
|
|
485 |
/** |
|
486 |
* 获取单个配置 |
|
487 |
* @param hLoginHandle 登陆句柄 |
|
488 |
* @param nChn 通道号,-1 表示全通道 |
|
489 |
* @param strCmd 配置名称 |
|
490 |
* @param cmdObject 配置对应的结构体对象 |
|
491 |
* @return 成功返回 true |
|
492 |
*/ |
|
493 |
public static boolean GetDevConfig(LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) { |
|
494 |
boolean result = false; |
|
495 |
IntByReference error = new IntByReference(0); |
|
496 |
int nBufferLen = 2*1024*1024; |
|
497 |
byte[] strBuffer = new byte[nBufferLen]; |
|
498 |
|
|
499 |
if(netsdkapi.CLIENT_GetNewDevConfig( hLoginHandle, strCmd , nChn, strBuffer, nBufferLen,error,3000,null)) { |
|
500 |
cmdObject.write(); |
|
501 |
if (configapi.CLIENT_ParseData(strCmd, strBuffer, cmdObject.getPointer(), |
|
502 |
cmdObject.size(), null)) { |
|
503 |
cmdObject.read(); |
|
504 |
result = true; |
|
505 |
} else { |
|
506 |
System.err.println("Parse " + strCmd + " Config Failed!" + ToolKits.getErrorCodePrint()); |
|
507 |
result = false; |
|
508 |
} |
|
509 |
} else { |
|
510 |
System.err.printf("Get %s Config Failed!Last Error = %s\n" , strCmd , getErrorCodePrint()); |
|
511 |
result = false; |
|
512 |
} |
|
513 |
|
|
514 |
return result; |
|
515 |
} |
|
516 |
|
|
517 |
/** |
|
518 |
* 设置单个配置 |
|
519 |
* @param hLoginHandle 登陆句柄 |
|
520 |
* @param nChn 通道号,-1 表示全通道 |
|
521 |
* @param strCmd 配置名称 |
|
522 |
* @param cmdObject 配置对应的结构体对象 |
|
523 |
* @return 成功返回 true |
|
524 |
*/ |
|
525 |
public static boolean SetDevConfig(LLong hLoginHandle, int nChn, String strCmd, Structure cmdObject) { |
|
526 |
boolean result = false; |
|
527 |
int nBufferLen = 2*1024*1024; |
|
528 |
byte szBuffer[] = new byte[nBufferLen]; |
|
529 |
for(int i=0; i<nBufferLen; i++)szBuffer[i]=0; |
|
530 |
IntByReference error = new IntByReference(0); |
|
531 |
IntByReference restart = new IntByReference(0); |
|
532 |
|
|
533 |
cmdObject.write(); |
|
534 |
if (configapi.CLIENT_PacketData(strCmd, cmdObject.getPointer(), cmdObject.size(), |
|
535 |
szBuffer, nBufferLen)) { |
|
536 |
cmdObject.read(); |
|
537 |
if( netsdkapi.CLIENT_SetNewDevConfig(hLoginHandle, strCmd , nChn , szBuffer, nBufferLen, error, restart, 3000)) { |
|
538 |
result = true; |
|
539 |
} else { |
|
540 |
System.err.printf("Set %s Config Failed! Last Error = %s\n" , strCmd , getErrorCodePrint()); |
|
541 |
result = false; |
|
542 |
} |
|
543 |
} else { |
|
544 |
System.err.println("Packet " + strCmd + " Config Failed!" + getErrorCodePrint()); |
|
545 |
result = false; |
|
546 |
} |
|
547 |
|
|
548 |
return result; |
|
549 |
} |
|
550 |
|
|
551 |
// Win下,将GBK String类型的转为Pointer |
|
552 |
public static Pointer GetGBKStringToPointer(String src) { |
|
553 |
Pointer pointer = null; |
|
554 |
try { |
|
555 |
byte[] b = src.getBytes("GBK"); |
|
556 |
pointer = new Memory(b.length+1); |
|
557 |
pointer.clear(b.length+1); |
|
558 |
|
|
559 |
pointer.write(0, b, 0, b.length); |
|
560 |
} catch (UnsupportedEncodingException e) { |
|
561 |
e.printStackTrace(); |
|
562 |
} |
|
563 |
return pointer; |
|
564 |
} |
|
565 |
|
|
566 |
/** |
|
567 |
* 字符串拷贝,用于先获取,再设置(src → dst) |
|
568 |
* @param src |
|
569 |
* @param dst |
|
570 |
*/ |
|
571 |
public static void StringToByteArray(String src, byte[] dst) { |
|
572 |
for(int i = 0; i < dst.length; i++) { |
|
573 |
dst[i] = 0; |
|
574 |
} |
|
575 |
|
|
576 |
System.arraycopy(src.getBytes(), 0, dst, 0, src.getBytes().length); |
|
577 |
} |
|
578 |
|
|
579 |
/** |
|
580 |
* 数组拷贝, 用于先获取,再设置(src → dst) |
|
581 |
* @param b |
|
582 |
* @param dst |
|
583 |
*/ |
|
584 |
public static void ByteArrayToByteArray(byte[] src, byte[] dst) { |
|
585 |
for(int i = 0; i < dst.length; i++) { |
|
586 |
dst[i] = 0; |
|
587 |
} |
|
588 |
|
|
589 |
System.arraycopy(src, 0, dst, 0, src.length); |
|
590 |
} |
|
591 |
} |