本文整理了Java中com.sun.jna.Native.getBytes()
方法的一些代码示例,展示了Native.getBytes()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Native.getBytes()
方法的具体详情如下:
包路径:com.sun.jna.Native
类名称:Native
方法名:getBytes
[英]Return a byte array corresponding to the given String. If the system property jna.encoding
is set, its value will override the default platform encoding (if supported).
[中]返回与给定字符串对应的字节数组。如果设置了系统属性jna.encoding
,其值将覆盖默认的平台编码(如果支持)。
代码示例来源:origin: net.java.dev.jna/jna
/**
* @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;
}
代码示例来源:origin: net.java.dev.jna/jna
/**
* @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());
}
代码示例来源:origin: net.java.dev.jna/jna
/**
* @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));
}
代码示例来源:origin: net.java.dev.jna/jna
@Override
public void setString(long offset, String value, String encoding) {
boundsCheck(offset, Native.getBytes(value, encoding).length + 1L);
super.setString(offset, value, encoding);
}
代码示例来源:origin: net.java.dev.jna/jna
/**
* 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);
}
代码示例来源:origin: net.java.dev.jna/jna
/** 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);
}
}
代码示例来源:origin: org.elasticsearch/jna
/**
* @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;
}
代码示例来源:origin: org.elasticsearch/jna
/**
* @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());
}
代码示例来源:origin: org.elasticsearch/jna
@Override
public void setString(long offset, String value, String encoding) {
boundsCheck(offset, Native.getBytes(value, encoding).length + 1L);
super.setString(offset, value, encoding);
}
代码示例来源:origin: org.elasticsearch/jna
/**
* 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);
}
代码示例来源:origin: org.elasticsearch/jna
/** 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);
}
}
代码示例来源:origin: com.sun.jna/jna
/** 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);
}
}
代码示例来源:origin: com.sun.jna/jna
byte[] data = Native.getBytes(value);
write(offset, data, 0, data.length);
setByte(offset + data.length, (byte)0);
内容来源于网络,如有侵权,请联系作者删除!