@Override public void write(byte[] b, int off, int len) throws IOException { if (closed) { throw new IOException("Stream closed."); } Memory m = new Memory(len); m.write(0, b, off, len); int written = library.ssh_channel_write(channel, m, len); if (written < 1) { throw new IOException("Failed to write."); } }
protected static Memory encodeString(String s) { // create NULL-terminated UTF-8 String byte[] bb = s.getBytes(Charset.forName("UTF-8")); Memory valueBuffer = new Memory(bb.length + 1); valueBuffer.write(0, bb, 0, bb.length); valueBuffer.setByte(valueBuffer.size() - 1, (byte) 0); return valueBuffer; }
public void read() { Memory old = getPointer(); Memory m = autoAllocate(size()); // horribly inefficient, but it'll do m.write(0, old.getByteArray(0, size()), 0, size()); useMemory(m); // Zero out the problematic callbacks for (field : problematic_fields) { m.setPointer(field_offset, null); } super.read(); useMemory(old); }
// prepare the credential blob byte[] credBlob = password.getBytes( "UTF-16LE" ) ; Memory credBlobMem = new Memory( credBlob.length ) ; credBlobMem.write( 0 , credBlob , 0 , credBlob.length ) ; // create the credential CREDENTIAL cred = new CREDENTIAL() ; cred.Type = CRED_TYPE_GENERIC ; cred.TargetName = new WString( target ) ; cred.CredentialBlobSize = (int) credBlobMem.size() ; cred.CredentialBlob = credBlobMem ; cred.Persist = CRED_PERSIST_LOCAL_MACHINE ; cred.UserName = new WString( userName ) ; // save the credential boolean rc = WinCrypt.INSTANCE.CredWrite( cred , 0 ) ; if ( ! rc ) ... ; // handle the error
public void SendReply(String ipAddress) { final IcmpEcho icmpecho = IcmpEcho.INSTANCE; final InetAddr inetAddr = InetAddr.INSTANCE; HANDLE icmpHandle = icmpecho.IcmpCreateFile(); byte[] message = new String("thisIsMyMessage!".toCharArray()).getBytes(); Memory messageData = new Memory(32); //In C/C++ this would be: void *messageData = (void*) malloc(message.length); messageData.write(0, message, 0, message.length); //but ignored the length and set it to 32 bytes instead for now Pointer requestData = messageData; Pointer replyBuffer = new Memory(256); replyBuffer.clear(256); // HERE IS THE NATIVE CALL!! reply = icmpecho.IcmpSendEcho(icmpHandle, inetAddr.inet_addr(ipAddress), requestData, (short) 32, null, replyBuffer, 256, timeout); // NATIVE CALL DONE, CHECK REPLY!! icmpecho.IcmpCloseHandle(icmpHandle); } public boolean IsReachable () { return (reply > 0); }
void mpzImport(mpz_t ptr, int signum, byte[] bytes) { int expectedLength = bytes.length; ensureBufferSize(expectedLength); scratchBuf.write(0, bytes, 0, bytes.length); __gmpz_import(ptr, bytes.length, 1, 1, 1, 0, scratchBuf); if (signum < 0) { __gmpz_neg(ptr, ptr); } }
void mpzImport(mpz_t ptr, int signum, byte[] bytes) { int expectedLength = bytes.length; ensureBufferSize(expectedLength); scratchBuf.write(0, bytes, 0, bytes.length); __gmpz_import(ptr, bytes.length, 1, 1, 1, 0, scratchBuf); if (signum < 0) { __gmpz_neg(ptr, ptr); } }
public void setValue(String value) { if(value == null) { value = ""; } try { byte[] encodedValue = value.getBytes("UTF-16LE"); // 4 bytes for the length prefix, length for the encoded data, // 2 bytes for the two NULL terminators Memory mem = new Memory(4 + encodedValue.length + 2); mem.clear(); mem.setInt(0, encodedValue.length); mem.write(4, encodedValue, 0, encodedValue.length); this.setPointer(mem.share(4)); } catch (UnsupportedEncodingException ex) { throw new RuntimeException("UTF-16LE charset is not supported", ex); } }
/** * String representation of the buffer. * @return * Unicode string. */ public String getString() { byte[] data = Buffer.getByteArray(0, Length); if (data.length < 2 || data[data.length - 1] != 0) { Memory newdata = new Memory(data.length + 2); newdata.write(0, data, 0, data.length); return newdata.getWideString(0); } return Buffer.getWideString(0); } }
/** * String representation of the buffer. * @return * Unicode string. */ public String getString() { byte[] data = Buffer.getByteArray(0, Length); if (data.length < 2 || data[data.length - 1] != 0) { Memory newdata = new Memory(data.length + 2); newdata.write(0, data, 0, data.length); return newdata.getString(0, true); } return Buffer.getString(0, true); } }
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() {
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 final Object marshal(Invocation invocation, IRubyObject parameter) { // Ruby strings are UTF-8, so should be able to just copy directly RubyString s = parameter.asString(); ByteList bl = s.getByteList(); final Memory memory = new Memory(bl.length() + 1); memory.write(0, bl.unsafeBytes(), bl.begin(), bl.length()); memory.setByte(bl.length(), (byte) 0); return memory; } public static final Marshaller INSTANCE = new StringMarshaller();
private zmq_msg_t newZmqMessage(byte[] msg) { zmq_msg_t message = new zmq_msg_t(); if (msg.length == 0) { if (zmq.zmq_msg_init_size(message, new NativeLong(msg.length)) != 0) { raiseZMQException(); } } else { Memory mem = new Memory(msg.length); mem.write(0, msg, 0, msg.length); if (zmq.zmq_msg_init_data(message, mem, new NativeLong(msg.length), messageDataBuffer, mem) == 0) { messageDataBuffer.add(mem); } else { raiseZMQException(); } } return message; }
private zmq_msg_t newZmqMessage(byte[] msg) { zmq_msg_t message = new zmq_msg_t(); if (msg.length == 0) { if (zmq.zmq_msg_init_size(message, new NativeLong(msg.length)) != 0) { raiseZMQException(); } } else { Memory mem = new Memory(msg.length); mem.write(0, msg, 0, msg.length); if (zmq.zmq_msg_init_data(message, mem, new NativeLong(msg.length), messageDataBuffer, mem) == 0) { messageDataBuffer.add(mem); } else { raiseZMQException(); } } return message; }
private zmq_msg_t newZmqMessage(byte[] msg) { zmq_msg_t message = new zmq_msg_t(); if (msg.length == 0) { if (zmq.zmq_msg_init_size(message, new NativeLong(msg.length)) != 0) { raiseZMQException(); } } else { Memory mem = new Memory(msg.length); mem.write(0, msg, 0, msg.length); if (zmq.zmq_msg_init_data(message, mem, new NativeLong(msg.length), messageDataBuffer, mem) == 0) { messageDataBuffer.add(mem); } else { raiseZMQException(); } } return message; }
private zmq_msg_t newZmqMessage(byte[] msg) { zmq_msg_t message = new zmq_msg_t(); if (msg.length == 0) { if (zmq.zmq_msg_init_size(message, new NativeLong(msg.length)) != 0) { raiseZMQException(); } } else { Memory mem = new Memory(msg.length); mem.write(0, msg, 0, msg.length); if (zmq.zmq_msg_init_data(message, mem, new NativeLong(msg.length), messageDataBuffer, mem) == 0) { messageDataBuffer.add(mem); } else { raiseZMQException(); } } return message; }
public final Object marshal(Invocation invocation, IRubyObject parameter) { // Ruby strings are UTF-8, so should be able to just copy directly final ByteList bl = parameter.asString().getByteList(); final int strlen = bl.length(); final Memory memory = new Memory(strlen + 1); memory.write(0, bl.unsafeBytes(), bl.begin(), strlen); memory.setByte(bl.length(), (byte) 0); // // Arrange for the bytes to be copied back after the function is called // invocation.addPostInvoke(new Runnable() { public void run() { memory.read(0, bl.unsafeBytes(), bl.begin(), strlen); } }); return memory; } public static final Marshaller INSTANCE = new RbxStringMarshaller();
public final Object marshal(Invocation invocation, IRubyObject parameter) { if (parameter instanceof JNAMemory) { return (((JNAMemory) parameter).getNativeMemory()); } else if (parameter.isNil()) { return Pointer.NULL; } else if (parameter instanceof RubyString) { // Handle a string being used as a inout buffer final ByteList bl = ((RubyString) parameter).getByteList(); final int len = bl.length(); final Memory memory = new Memory(len); memory.write(0, bl.unsafeBytes(), bl.begin(), len); // // Arrange for the bytes to be copied back after the function is called // invocation.addPostInvoke(new Runnable() { public void run() { memory.read(0, bl.unsafeBytes(), bl.begin(), len); } }); return memory; } return Util.convertParameter(parameter, Pointer.class); } public static final Marshaller INSTANCE = new PointerMarshaller();
try { passwordData = new Memory(utf8Password.length + 1); passwordData.write(0, utf8Password, 0, utf8Password.length); passwordData.setByte(utf8Password.length, (byte) 0); result = gnomeKeyringLibrary.gnome_keyring_set_network_password_sync(null, userName, realm, null, null, null, null, 0, passwordData, itemId);