Java Code Examples for com.sun.jna.Memory#setByte()

The following examples show how to use com.sun.jna.Memory#setByte() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotesErrorUtils.java    From domino-jna with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a C API error code to an error message
 * 
 * @param err error code
 * @return error message
 */
public static String errToString(short err) {
	if (err==0)
		return "";

	Memory retBuffer = new Memory(256);
	short outStrLength = NotesNativeAPI.get().OSLoadString(0, err, retBuffer, (short) 255);
	if (outStrLength==0) {
		if (err == 577) { // ERR_BAD_PARAM
			return "(invalid usage - see NSF documentation)";
		}
		
		return "";
	}
	Memory newRetBuffer = new Memory(outStrLength);
	for (int i=0; i<outStrLength; i++) {
		newRetBuffer.setByte(i, retBuffer.getByte(i));
	}
	
	String message = NotesStringUtils.fromLMBCS(newRetBuffer, outStrLength);
	return message;
}
 
Example 2
Source File: Poller.java    From jnanomsg with Apache License 2.0 6 votes vote down vote up
public void register(Socket socket, int flags) {
  int pos = this.next;
  this.next += EVENT_SIZE;

  if (pos > this.items.size()) {
    final long newSize = this.items.size() + (EVENT_SIZE * SIZE_INCREMENT);
    final Memory newItems = new Memory(newSize);

    for(int i=0; i<this.items.size(); i++) {
      newItems.setByte(i, this.items.getByte(i));
    }
  }

  final int socketFd = socket.getFd();
  final NNPollEvent.ByReference event = new NNPollEvent.ByReference();
  event.reuse(this.items, pos);
  event.fd = socketFd;
  event.events = (short)flags;
  event.write();

  this.offsetMap.put(socketFd, pos);
}
 
Example 3
Source File: IDUtils.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
/**
 * Internal helper method to fetch the ID from the ID vault.
 * 
 * @param userName Name of user whose ID is being put into vault - either abbreviated or canonical format
 * @param password Password to id file being uploaded to the vault
 * @param idPath if not null, path to where the download ID file should be created or overwritten
 * @param rethKFC64 if not null, returns the hKFC handle to the in-memory id for 64 bit
 * @param rethKFC32 if not null, returns the hKFC handle to the in-memory id for 32 bit
 * @param serverName Name of server to contact
 * @return the vault server name
 * @throws NotesError in case of problems, e.g. ERR 22792 Wrong Password
 */
private static String _getUserIdFromVault(String userName, String password, String idPath, LongByReference rethKFC64, IntByReference rethKFC32, String serverName) {
	String userNameCanonical = NotesNamingUtils.toCanonicalName(userName);
	Memory userNameCanonicalMem = NotesStringUtils.toLMBCS(userNameCanonical, true);
	Memory passwordMem = NotesStringUtils.toLMBCS(password, true);
	Memory idPathMem = NotesStringUtils.toLMBCS(idPath, true);
	Memory serverNameMem = new Memory(NotesConstants.MAXPATH);
	{
		Memory serverNameParamMem = NotesStringUtils.toLMBCS(serverName, true);
		if (serverNameParamMem!=null && (serverNameParamMem.size() > NotesConstants.MAXPATH)) {
			throw new IllegalArgumentException("Servername length cannot exceed MAXPATH ("+NotesConstants.MAXPATH+" characters)");
		}
		if (serverNameParamMem!=null) {
			byte[] serverNameParamArr = serverNameParamMem.getByteArray(0, (int) serverNameParamMem.size());
			serverNameMem.write(0, serverNameParamArr, 0, serverNameParamArr.length);
		}
		else {
			serverNameMem.setByte(0, (byte) 0);
		}
	}
	
	short result;
	if (PlatformUtils.is64Bit()) {
		result = NotesNativeAPI64.get().SECidfGet(userNameCanonicalMem, passwordMem, idPathMem, rethKFC64, serverNameMem, 0, (short) 0, null);
	}
	else {
		result = NotesNativeAPI32.get().SECidfGet(userNameCanonicalMem, passwordMem, idPathMem, rethKFC32, serverNameMem, 0, (short) 0, null);
	}
	NotesErrorUtils.checkResult(result);
	
	int vaultServerNameLength = 0;
	for (int i=0; i<serverNameMem.size(); i++) {
		vaultServerNameLength = i;
		if (serverNameMem.getByte(i) == 0) {
			break;
		}
	}
	
	String vaultServerName = NotesStringUtils.fromLMBCS(serverNameMem, vaultServerNameLength);
	return vaultServerName;
}
 
Example 4
Source File: IDUtils.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
/**
 * Will open the ID file name provided, locate a vault server for user <code>userName</code>,
 * upload the ID file contents to the vault, then return with the vault server name.<br>
 * 
 * @param userName Name of user whose ID is being put into vault - either abbreviated or canonical format
 * @param password Password to id file being uploaded to the vault
 * @param idPath Path to where the download ID file should be created or overwritten or null to use the in-memory id
 * @param phKFC64 handle to the in-memory id or null to use an id file on disk for 64 bit
 * @param phKFC32 handle to the in-memory id or null to use an id file on disk for 32 bit
 * @param serverName Name of server to contact
 * @return the vault server name
 * @throws NotesError in case of problems, e.g. ERR 22792 Wrong Password
 */
private static String _putUserIdIntoVault(String userName, String password, String idPath,
		LongByReference phKFC64, IntByReference phKFC32, String serverName) {
	//opening any database on the server is required before putting the id fault, according to the
	//C API documentation and sample "idvault.c"
	NotesDatabase anyServerDb = new NotesDatabase(serverName, "names.nsf", (String) null);
	try {
		String userNameCanonical = NotesNamingUtils.toCanonicalName(userName);
		Memory userNameCanonicalMem = NotesStringUtils.toLMBCS(userNameCanonical, true);
		Memory passwordMem = NotesStringUtils.toLMBCS(password, true);
		Memory idPathMem = NotesStringUtils.toLMBCS(idPath, true);
		Memory serverNameMem = new Memory(NotesConstants.MAXPATH);
		{
			Memory serverNameParamMem = NotesStringUtils.toLMBCS(serverName, true);
			if (serverNameParamMem!=null && (serverNameParamMem.size() > NotesConstants.MAXPATH)) {
				throw new IllegalArgumentException("Servername length cannot exceed MAXPATH ("+NotesConstants.MAXPATH+" characters)");
			}
			if (serverNameParamMem!=null) {
				byte[] serverNameParamArr = serverNameParamMem.getByteArray(0, (int) serverNameParamMem.size());
				serverNameMem.write(0, serverNameParamArr, 0, serverNameParamArr.length);
			}
			else {
				serverNameMem.setByte(0, (byte) 0);
			}
		}
		
		short result;
		if (PlatformUtils.is64Bit()) {
			result = NotesNativeAPI64.get().SECKFMOpen (phKFC64, idPathMem, passwordMem, NotesConstants.SECKFM_open_All, 0, null);
		}
		else {
			result = NotesNativeAPI32.get().SECKFMOpen (phKFC32, idPathMem, passwordMem, NotesConstants.SECKFM_open_All, 0, null);
		}
		NotesErrorUtils.checkResult(result);
		
		try {
			if (PlatformUtils.is64Bit()) {
				result = NotesNativeAPI64.get().SECidfPut(userNameCanonicalMem, passwordMem, idPathMem, phKFC64, serverNameMem, 0, (short) 0, null);
			}
			else {
				result = NotesNativeAPI32.get().SECidfPut(userNameCanonicalMem, passwordMem, idPathMem, phKFC32, serverNameMem, 0, (short) 0, null);
			}
			NotesErrorUtils.checkResult(result);
		}
		finally {
			if (PlatformUtils.is64Bit()) {
				result = NotesNativeAPI64.get().SECKFMClose(phKFC64, NotesConstants.SECKFM_close_WriteIdFile, 0, null);
				
			}
			else {
				result = NotesNativeAPI32.get().SECKFMClose(phKFC32, NotesConstants.SECKFM_close_WriteIdFile, 0, null);
			}
			NotesErrorUtils.checkResult(result);
		}
				
		int vaultServerNameLength = 0;
		for (int i=0; i<serverNameMem.size(); i++) {
			if (serverNameMem.getByte(i) == 0) {
				break;
			}
			else {
				vaultServerNameLength = i;
			}
		}
		
		String vaultServerName = NotesStringUtils.fromLMBCS(serverNameMem, vaultServerNameLength);
		return vaultServerName;

	}
	finally {
		anyServerDb.recycle();
	}
}
 
Example 5
Source File: IDUtils.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
/**
 * Will open the ID file name provided, locate a vault server, synch the ID file contents to the vault,
 * then return the synched content. If successful the vault server name is returned.

 * @param userName Name of user whose ID is being put into vault - either abbreviated or canonical format
 * @param password Password to id file being uploaded to the vault
 * @param idPath Path to where the download ID file should be created or overwritten
 * @param serverName Name of server to contact
 * @return sync result
 */
public static SyncResult syncUserIdWithVault(String userName, String password, String idPath, String serverName) {
	String userNameCanonical = NotesNamingUtils.toCanonicalName(userName);
	Memory userNameCanonicalMem = NotesStringUtils.toLMBCS(userNameCanonical, true);
	Memory passwordMem = NotesStringUtils.toLMBCS(password, true);
	Memory idPathMem = NotesStringUtils.toLMBCS(idPath, true);
	Memory serverNameMem = new Memory(NotesConstants.MAXPATH);
	{
		Memory serverNameParamMem = NotesStringUtils.toLMBCS(serverName, true);
		if (serverNameParamMem!=null && (serverNameParamMem.size() > NotesConstants.MAXPATH)) {
			throw new IllegalArgumentException("Servername length cannot exceed MAXPATH ("+NotesConstants.MAXPATH+" characters)");
		}
		if (serverNameParamMem!=null) {
			byte[] serverNameParamArr = serverNameParamMem.getByteArray(0, (int) serverNameParamMem.size());
			serverNameMem.write(0, serverNameParamArr, 0, serverNameParamArr.length);
		}
		else {
			serverNameMem.setByte(0, (byte) 0);
		}
	}
	
	LongByReference phKFC64 = new LongByReference();
	IntByReference phKFC32 = new IntByReference();
	IntByReference retdwFlags = new IntByReference();
	
	short result;
	if (PlatformUtils.is64Bit()) {
		result = NotesNativeAPI64.get().SECKFMOpen (phKFC64, idPathMem, passwordMem, NotesConstants.SECKFM_open_All, 0, null);
	}
	else {
		result = NotesNativeAPI32.get().SECKFMOpen (phKFC32, idPathMem, passwordMem, NotesConstants.SECKFM_open_All, 0, null);
	}
	NotesErrorUtils.checkResult(result);
	
	try {
		if (PlatformUtils.is64Bit()) {
			result = NotesNativeAPI64.get().SECidfSync(userNameCanonicalMem, passwordMem, idPathMem, phKFC64, serverNameMem, 0, (short) 0, null, retdwFlags);
		}
		else {
			result = NotesNativeAPI32.get().SECidfSync(userNameCanonicalMem, passwordMem, idPathMem, phKFC32, serverNameMem, 0, (short) 0, null, retdwFlags);
		}
		NotesErrorUtils.checkResult(result);
	}
	finally {
		if (PlatformUtils.is64Bit()) {
			result = NotesNativeAPI64.get().SECKFMClose(phKFC64, NotesConstants.SECKFM_close_WriteIdFile, 0, null);
		}
		else {
			result = NotesNativeAPI32.get().SECKFMClose(phKFC32, NotesConstants.SECKFM_close_WriteIdFile, 0, null);
		}
		NotesErrorUtils.checkResult(result);
	}

	NotesErrorUtils.checkResult(result);
	
	int vaultServerNameLength = 0;
	for (int i=0; i<serverNameMem.size(); i++) {
		vaultServerNameLength = i;
		if (serverNameMem.getByte(i) == 0) {
			break;
		}
	}
	
	String vaultServerName = NotesStringUtils.fromLMBCS(serverNameMem, vaultServerNameLength);
	
	SyncResult syncResult = new SyncResult(vaultServerName, retdwFlags.getValue());
	return syncResult;
}
 
Example 6
Source File: CompoundTextWriter.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
@Override
public void addText(String txt, TextStyle textStyle, FontStyle fontStyle, boolean createParagraphOnLinebreak) {
	checkHandle();
	if (isClosed())
		throw new NotesError(0, "CompoundText already closed");

	Memory txtMem = NotesStringUtils.toLMBCS(txt, false);
	Memory lineDelimMem = new Memory(3);
	lineDelimMem.setByte(0, (byte) '\r'); 
	lineDelimMem.setByte(1, (byte) '\n'); 
	lineDelimMem.setByte(2, (byte) 0);

	int fontId;
	if (fontStyle==null) {
		fontId = getDefaultFontId();
	}
	else {
		FontId fontIdObj = fontStyle.getAdapter(FontId.class);
		if (fontIdObj==null)
			throw new NotesError(0, "Unable to get FontId from FontStyle");
		fontId = fontIdObj.getFontId();
	}
	
	int dwStyleID = textStyle==null ? NotesConstants.STYLE_ID_SAMEASPREV : getStyleId(textStyle);

	Pointer nlsInfoPtr = NotesNativeAPI.get().OSGetLMBCSCLS();
	short result;
	int dwFlags = NotesConstants.COMP_PRESERVE_LINES | NotesConstants.COMP_PARA_BLANK_LINE;
	if (createParagraphOnLinebreak) {
		dwFlags = dwFlags | NotesConstants.COMP_PARA_LINE;
	}
	if (PlatformUtils.is64Bit()) {
		result = NotesNativeAPI64.get().CompoundTextAddTextExt(m_handle64, dwStyleID, fontId, txtMem, txtMem==null ? 0 : (int) txtMem.size(),
				lineDelimMem, dwFlags, nlsInfoPtr);
	}
	else {
		result = NotesNativeAPI32.get().CompoundTextAddTextExt(m_handle32, dwStyleID, fontId, txtMem, txtMem==null ? 0 : (int) txtMem.size(),
				lineDelimMem, dwFlags, nlsInfoPtr);
	}
	NotesErrorUtils.checkResult(result);
	m_hasData=true;
}