/** * @param s The string. Must not be {@code null}. * @param charset The charset used to encode {@code s}. Must not be {@code null}. * @return A NUL-terminated byte buffer equivalent to the given String, * using the given charset. * @see #getBytes(String, String) */ public static byte[] toByteArray(String s, Charset charset) { byte[] bytes = Native.getBytes(s, charset); byte[] buf = new byte[bytes.length+1]; System.arraycopy(bytes, 0, buf, 0, bytes.length); return buf; }
/** * @param s The input string * @return A byte array corresponding to the given String. The encoding * used is obtained from {@link #getDefaultStringEncoding()}. */ static byte[] getBytes(String s) { return getBytes(s, getDefaultStringEncoding()); }
/** * @param s The string. Must not be {@code null}. * @param encoding The encoding - if {@code null} then the default platform * encoding is used * @return A byte array corresponding to the given String, using the given * encoding. If the encoding is not found default to the platform native * encoding. */ static byte[] getBytes(String s, String encoding) { return Native.getBytes(s, Native.getCharset(encoding)); }
@Override public void setString(long offset, String value, String encoding) { boundsCheck(offset, Native.getBytes(value, encoding).length + 1L); super.setString(offset, value, encoding); }
/** * Copy string <code>value</code> to the location being pointed to, using * the requested encoding. * * @param offset byte offset from pointer at which characters in * <code>value</code> must be set * @param value <code>java.lang.String</code> value to set * @param encoding desired encoding */ public void setString(long offset, String value, String encoding) { byte[] data = Native.getBytes(value, encoding); write(offset, data, 0, data.length); setByte(offset + 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); } }
/** * @param s The string * @return A NUL-terminated byte buffer equivalent to the given String, * using the given encoding. * @see #getBytes(String, String) */ public static byte[] toByteArray(String s, String encoding) { byte[] bytes = getBytes(s, encoding); byte[] buf = new byte[bytes.length+1]; System.arraycopy(bytes, 0, buf, 0, bytes.length); return buf; }
/** * @param s The input string * @return A byte array corresponding to the given String. The encoding * used is obtained from {@link #getDefaultStringEncoding()}. */ static byte[] getBytes(String s) { return getBytes(s, getDefaultStringEncoding()); }
@Override public void setString(long offset, String value, String encoding) { boundsCheck(offset, Native.getBytes(value, encoding).length + 1L); super.setString(offset, value, encoding); }
/** * Copy string <code>value</code> to the location being pointed to, using * the requested encoding. * * @param offset byte offset from pointer at which characters in * <code>value</code> must be set * @param value <code>java.lang.String</code> value to set * @param encoding desired encoding */ public void setString(long offset, String value, String encoding) { byte[] data = Native.getBytes(value, encoding); write(offset, data, 0, data.length); setByte(offset + 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); } }
/** Create a native string as a NUL-terminated array of <code>wchar_t</code> * (if <code>wide</code> is true) or <code>char</code>.<p> * If the system property <code>jna.encoding</code> is set, its value will * be used to encode the native <code>char</code>string. * If not set or if the encoding is unavailable, the default platform * encoding will be used. * * @param string value to write to native memory * @param wide whether to store the String as <code>wchar_t</code> */ public NativeString(String string, boolean wide) { this.value = string; 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()). if (wide) { int len = (string.length() + 1 ) * Native.WCHAR_SIZE; pointer = new Memory(len); pointer.setString(0, string, true); } else { byte[] data = Native.getBytes(string); pointer = new Memory(data.length + 1); pointer.setString(0, string); } }
byte[] data = Native.getBytes(value); write(offset, data, 0, data.length); setByte(offset + data.length, (byte)0);