protected final static Pointer stringPointer(String str) { Memory mem = new Memory(str.length() + 1); mem.setString(0, str); return mem; }
public void testGetSetStringWithDefaultEncoding() throws Exception { final String ENCODING = Native.DEFAULT_ENCODING; String VALUE = getName(); int size = VALUE.getBytes(ENCODING).length+1; Memory m = new Memory(size); m.setString(0, VALUE); assertEquals("Wrong decoded value", VALUE, m.getString(0)); }
public NativeArgs(String progname, String[] args) { // // Allocate some native memory to pass the args down to the native layer // argsCopy = new Memory[args.length + 2]; argvMemory = new Memory(argsCopy.length * Native.POINTER_SIZE); // // Insert the program name as argv[0] // Memory arg = new Memory(progname.getBytes().length + 4); arg.setString(0, progname); argsCopy[0] = arg; for (int i = 0; i < args.length; i++) { arg = new Memory(args[i].getBytes().length + 1); arg.setString(0, args[i]); argsCopy[i + 1] = arg; } argvMemory.write(0, argsCopy, 0, argsCopy.length); argvRef = new PointerByReference(argvMemory); argcRef = new IntByReference(args.length + 1); } String[] toStringArray() {
public NativeArgs(String progname, String[] args) { // // Allocate some native memory to pass the args down to the native layer // argsCopy = new Memory[args.length + 2]; argvMemory = new Memory(argsCopy.length * Pointer.SIZE); // // Insert the program name as argv[0] // Memory arg = new Memory(progname.getBytes().length + 4); arg.setString(0, progname, false); argsCopy[0] = arg; for (int i = 0; i < args.length; i++) { arg = new Memory(args[i].getBytes().length + 1); arg.setString(0, args[i], false); argsCopy[i + 1] = arg; } argvMemory.write(0, argsCopy, 0, argsCopy.length); argvRef = new PointerByReference(argvMemory); argcRef = new IntByReference(args.length + 1); } String[] toStringArray() {
class BSTRUtils { private BSTR() { } public static Memory toNative(String value) { Memory m = new Memory(value.length()*2+6); m.setInt(0, value.length()*2); m.setString(4, value, true); return m; } public static String toString(PointerByReference pbr) { return toString(pbr.getValue()); } public static String toString(Pointer p) { int length = p.getInt(0); char[] data = p.getCharArray(4, length/2); return new String(data); } }
data.setWideString(offset, s); } else { data.setString(offset, s);
/** * Set an expandable string value in registry. * * @param hKey * Parent key. * @param name * Value name. * @param value * Value to write to registry. */ public static void registrySetExpandableStringValue(HKEY hKey, String name, String value) { Memory data; if( W32APITypeMapper.DEFAULT == W32APITypeMapper.UNICODE ) { data = new Memory((value.length() + 1) * Native.WCHAR_SIZE); data.setWideString(0, value); } else { data = new Memory((value.length() + 1)); data.setString(0, value); } int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_EXPAND_SZ, data, (int) data.size()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
Memory data = new Memory(size); for (String s : arr) { data.setString(offset, s, true); offset += s.length() * Native.WCHAR_SIZE; offset += Native.WCHAR_SIZE;
/** * Set a string value in registry. * * @param hKey * Parent key. * @param name * Value name. * @param value * Value to write to registry. */ public static void registrySetStringValue(HKEY hKey, String name, String value) { if(value == null) { value = ""; } Memory data; if( W32APITypeMapper.DEFAULT == W32APITypeMapper.UNICODE ) { data = new Memory((value.length() + 1) * Native.WCHAR_SIZE); data.setWideString(0, value); } else { data = new Memory((value.length() + 1)); data.setString(0, value); } int rc = Advapi32.INSTANCE.RegSetValueEx(hKey, name, 0, WinNT.REG_SZ, data, (int) data.size()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } }
mTest.setString(0, testStr); String testReturn = mTest.getString(0); // you can see that String got properly stored in Memory object System.out.println("String in Memory:"+testReturn);
protected static void write(int level, String message, UnixSyslogConfig config) throws SyslogRuntimeException { synchronized (libraryInstance) { if (currentFacility != config.getFacility()) { if (openlogCalled) { libraryInstance.closelog(); openlogCalled = false; } currentFacility = config.getFacility(); } if (!openlogCalled) { String ident = config.getIdent(); if (ident != null && "".equals(ident.trim())) { ident = null; } if (ident != null) { identBuffer = new Memory(128); identBuffer.setString(0, ident, false); } libraryInstance.openlog(identBuffer, config.getOption(), currentFacility); openlogCalled = true; } int priority = currentFacility | level; libraryInstance.syslog(priority, "%s", message); } }
protected static void write(SyslogLevel level, String message, UnixSyslogConfig config) throws SyslogRuntimeException { synchronized(libraryInstance) { if (currentFacility != config.getFacility()) { if (openlogCalled) { libraryInstance.closelog(); openlogCalled = false; } currentFacility = config.getFacility(); } if (!openlogCalled) { String ident = config.getIdent(); if (!StringUtils.isBlank(ident)) { ident = null; } Memory identBuffer = ident == null ? null : (Memory) identMap.get(ident); if (ident != null && identBuffer == null) { identBuffer = new Memory(128); identBuffer.setString(0, ident, false); identMap.put(ident, identBuffer); } libraryInstance.openlog(identBuffer,config.getOption(),currentFacility.getValue()); openlogCalled = true; } int priority = (currentFacility == null ? 0 : currentFacility.getValue()) | (level == null ? 0 : level.getValue()); libraryInstance.syslog(priority,"%s",message); } }
Type = type.ordinal(); Memory mem = new Memory(((String) value).length() + 1); mem.setString(0, (String) value); holder = mem; Count = 0;