/** Create a native string (NUL-terminated array of <code>char</code>), * using the requested encoding. */ public NativeString(String string, String encoding) { if (string == null) { throw new NullPointerException("String must not be null"); } // Allocate the memory to hold the string. Note, we have to // make this 1 element longer in order to accommodate the terminating // NUL (which is generated in Pointer.setString()). this.encoding = encoding; if (WIDE_STRING.equals(this.encoding)) { int len = (string.length() + 1 ) * Native.WCHAR_SIZE; pointer = new StringMemory(len); pointer.setWideString(0, string); } else { byte[] data = Native.getBytes(string, encoding); pointer = new StringMemory(data.length + 1); pointer.write(0, data, 0, data.length); pointer.setByte(data.length, (byte)0); } }
/** Create a native string (NUL-terminated array of <code>char</code>), * using the requested encoding. */ public NativeString(String string, String encoding) { if (string == null) { throw new NullPointerException("String must not be null"); } // Allocate the memory to hold the string. Note, we have to // make this 1 element longer in order to accommodate the terminating // NUL (which is generated in Pointer.setString()). this.encoding = encoding; if (WIDE_STRING.equals(this.encoding)) { int len = (string.length() + 1 ) * Native.WCHAR_SIZE; pointer = new StringMemory(len); pointer.setWideString(0, string); } else { byte[] data = Native.getBytes(string, encoding); pointer = new StringMemory(data.length + 1); pointer.write(0, data, 0, data.length); pointer.setByte(data.length, (byte)0); } }