|
24 | 24 | */
|
25 | 25 | package com.sun.jna.platform.win32;
|
26 | 26 |
|
| 27 | +import java.io.UnsupportedEncodingException; |
| 28 | + |
27 | 29 | import com.sun.jna.Memory;
|
28 | 30 | import com.sun.jna.Native;
|
29 | 31 | import com.sun.jna.Pointer;
|
30 | 32 | import com.sun.jna.PointerType;
|
31 | 33 | import com.sun.jna.Structure;
|
32 | 34 | import com.sun.jna.platform.win32.WinDef.USHORT;
|
33 | 35 | import com.sun.jna.ptr.ByReference;
|
34 |
| -import java.io.UnsupportedEncodingException; |
35 | 36 |
|
36 | 37 | /**
|
37 | 38 | * Constant defined in WTypes.h
|
@@ -103,15 +104,33 @@ public BSTR() {
|
103 | 104 | super(Pointer.NULL);
|
104 | 105 | }
|
105 | 106 |
|
| 107 | + /** |
| 108 | + * Instantiate a BSTR from a pointer. The user is responsible for allocating and |
| 109 | + * releasing memory for the {@link BSTR}, most commonly using |
| 110 | + * {@link OleAuto#SysAllocString(String)} and |
| 111 | + * {@link OleAuto#SysFreeString(BSTR)} |
| 112 | + * |
| 113 | + * @param pointer |
| 114 | + * A pointer to the string |
| 115 | + */ |
106 | 116 | public BSTR(Pointer pointer) {
|
107 | 117 | super(pointer);
|
108 | 118 | }
|
109 | 119 |
|
| 120 | + /** |
| 121 | + * @deprecated Use {@link OleAuto#SysAllocString(String)} and |
| 122 | + * {@link OleAuto#SysFreeString(BSTR)} |
| 123 | + */ |
| 124 | + @Deprecated |
110 | 125 | public BSTR(String value) {
|
111 | 126 | super();
|
112 | 127 | this.setValue(value);
|
113 | 128 | }
|
114 | 129 |
|
| 130 | + /** |
| 131 | + * @deprecated Users should not change the value of an allocated {@link BSTR}. |
| 132 | + */ |
| 133 | + @Deprecated |
115 | 134 | public void setValue(String value) {
|
116 | 135 | if(value == null) {
|
117 | 136 | value = "";
|
@@ -154,21 +173,56 @@ public BSTRByReference() {
|
154 | 173 | super(Native.POINTER_SIZE);
|
155 | 174 | }
|
156 | 175 |
|
| 176 | + /** |
| 177 | + * Store a reference to the specified {@link BSTR}. This method does not |
| 178 | + * maintain a reference to the object passed as an argument. The user is |
| 179 | + * responsible for allocating and freeing the memory associated with this |
| 180 | + * {@link BSTR}. |
| 181 | + * |
| 182 | + * @param value |
| 183 | + * The BSTR to be referenced. Only the pointer is stored as a |
| 184 | + * reference. |
| 185 | + */ |
157 | 186 | public BSTRByReference(BSTR value) {
|
158 | 187 | this();
|
159 | 188 | setValue(value);
|
160 | 189 | }
|
161 | 190 |
|
| 191 | + /** |
| 192 | + * Store a reference to the specified {@link BSTR}. This method does not |
| 193 | + * maintain a reference to the object passed as an argument. The user is |
| 194 | + * responsible for allocating and freeing the memory associated with this |
| 195 | + * {@link BSTR}. |
| 196 | + * |
| 197 | + * @param value |
| 198 | + * The BSTR to be referenced. Only the pointer is stored as a |
| 199 | + * reference. |
| 200 | + */ |
162 | 201 | public void setValue(BSTR value) {
|
163 | 202 | this.getPointer().setPointer(0, value.getPointer());
|
164 | 203 | }
|
165 | 204 |
|
| 205 | + /** |
| 206 | + * Returns a copy of the {@link BSTR} referenced by this object. The memory |
| 207 | + * associated with the {@link BSTR} may be referenced by other objects/threads |
| 208 | + * which may also free the underlying native memory. |
| 209 | + * |
| 210 | + * @return A new {@link BSTR} object corresponding to the memory referenced by |
| 211 | + * this object. |
| 212 | + */ |
166 | 213 | public BSTR getValue() {
|
167 | 214 | return new BSTR(getPointer().getPointer(0));
|
168 | 215 | }
|
169 | 216 |
|
| 217 | + /** |
| 218 | + * Returns the String represented by the referenced {@link BSTR}. |
| 219 | + * |
| 220 | + * @return the referenced String, if the reference is not {@code null}, |
| 221 | + * {@code null} otherwise. |
| 222 | + */ |
170 | 223 | public String getString() {
|
171 |
| - return this.getValue().getValue(); |
| 224 | + BSTR b = this.getValue(); |
| 225 | + return b == null ? null : b.getValue(); |
172 | 226 | }
|
173 | 227 | }
|
174 | 228 |
|
|
0 commit comments