提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.lib; |
H |
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.nio.CharBuffer; |
|
5 |
|
|
6 |
import com.sun.jna.Memory; |
|
7 |
import com.sun.jna.Native; |
|
8 |
import com.sun.jna.Pointer; |
|
9 |
|
|
10 |
|
|
11 |
/** Provides a temporary allocation of an immutable C string |
|
12 |
* (<code>const char*</code> or <code>const wchar_t*</code>) for use when |
|
13 |
* converting a Java String into a native memory function argument. |
|
14 |
* |
|
15 |
* @author Todd Fast, todd.fast@sun.com |
|
16 |
* @author twall@users.sf.net |
|
17 |
*/ |
|
18 |
public class NativeString implements CharSequence, Comparable<Object> { |
|
19 |
|
|
20 |
private Pointer pointer; |
|
21 |
private boolean wide; |
|
22 |
|
|
23 |
/** Create a native string (NUL-terminated array of <code>char</code>).<p> |
|
24 |
* If the system property <code>jna.encoding</code> is set, its value will |
|
25 |
* be used to encode the native string. If not set or if the encoding |
|
26 |
* is unavailable, the default platform encoding will be used. |
|
27 |
*/ |
|
28 |
public NativeString(String string) { |
|
29 |
this(string, false); |
|
30 |
} |
|
31 |
|
|
32 |
/** Create a native string as a NUL-terminated array of <code>wchar_t</code> |
|
33 |
* (if <code>wide</code> is true) or <code>char</code>.<p> |
|
34 |
* If the system property <code>jna.encoding</code> is set, its value will |
|
35 |
* be used to encode the native <code>char</code>string. |
|
36 |
* If not set or if the encoding is unavailable, the default platform |
|
37 |
* encoding will be used. |
|
38 |
* |
|
39 |
* @param string value to write to native memory |
|
40 |
* @param wide whether to store the String as <code>wchar_t</code> |
|
41 |
*/ |
|
42 |
public NativeString(String string, boolean wide) { |
|
43 |
if (string == null) { |
|
44 |
throw new NullPointerException("String must not be null"); |
|
45 |
} |
|
46 |
// Allocate the memory to hold the string. Note, we have to |
|
47 |
// make this 1 element longer in order to accommodate the terminating |
|
48 |
// NUL (which is generated in Pointer.setString()). |
|
49 |
this.wide = wide; |
|
50 |
if (wide) { |
|
51 |
int len = (string.length() + 1 ) * Native.WCHAR_SIZE; |
|
52 |
pointer = new Memory(len); |
|
53 |
pointer.setString(0, string); |
|
54 |
} |
|
55 |
else { |
|
56 |
byte[] data = getBytes(string); |
|
57 |
pointer = new Memory(data.length + 1); |
|
58 |
pointer.write(0, data, 0, data.length); |
|
59 |
pointer.setByte(data.length, (byte)0); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
static byte[] getBytes(String s) { |
|
64 |
try { |
|
65 |
return getBytes(s, System.getProperty("jna.encoding")); |
|
66 |
} |
|
67 |
catch (UnsupportedEncodingException e) { |
|
68 |
return s.getBytes(); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
/** Return a byte array corresponding to the given String, using the given |
|
73 |
encoding. |
|
74 |
*/ |
|
75 |
static byte[] getBytes(String s, String encoding) throws UnsupportedEncodingException { |
|
76 |
if (encoding != null) { |
|
77 |
return s.getBytes(encoding); |
|
78 |
} |
|
79 |
return s.getBytes(); |
|
80 |
} |
|
81 |
|
|
82 |
public int hashCode() { |
|
83 |
return toString().hashCode(); |
|
84 |
} |
|
85 |
|
|
86 |
public boolean equals(Object other) { |
|
87 |
|
|
88 |
if (other instanceof CharSequence) { |
|
89 |
return compareTo(other) == 0; |
|
90 |
} |
|
91 |
return false; |
|
92 |
} |
|
93 |
|
|
94 |
public String toString() { |
|
95 |
String s = wide ? "const wchar_t*" : "const char*"; |
|
96 |
s += "(" + pointer.getString(0) + ")"; |
|
97 |
return s; |
|
98 |
} |
|
99 |
|
|
100 |
public Pointer getPointer() { |
|
101 |
return pointer; |
|
102 |
} |
|
103 |
|
|
104 |
public char charAt(int index) { |
|
105 |
return toString().charAt(index); |
|
106 |
} |
|
107 |
|
|
108 |
public int length() { |
|
109 |
return toString().length(); |
|
110 |
} |
|
111 |
|
|
112 |
public CharSequence subSequence(int start, int end) { |
|
113 |
return CharBuffer.wrap(toString()).subSequence(start, end); |
|
114 |
} |
|
115 |
|
|
116 |
public int compareTo(Object other) { |
|
117 |
|
|
118 |
if (other == null) |
|
119 |
return 1; |
|
120 |
|
|
121 |
return toString().compareTo(other.toString()); |
|
122 |
} |
|
123 |
} |
|
124 |
|