use of com.sun.jna.Pointer in project jna by java-native-access.
the class Advapi32Test method testWriteEncryptedFileRaw.
public void testWriteEncryptedFileRaw() throws Exception {
// create an encrypted file
File file = createTempFile();
String lpFileName = file.getAbsolutePath();
assertTrue(Advapi32.INSTANCE.EncryptFile(lpFileName));
// open file for export
ULONG ulFlags = new ULONG(0);
PointerByReference pvContext = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lpFileName, ulFlags, pvContext));
// read encrypted file
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
FE_EXPORT_FUNC pfExportCallback = new FE_EXPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONG ulLength) {
if (pbData == null) {
throw new NullPointerException("Callback data unexpectedly null");
}
byte[] arr = pbData.getByteArray(0, ulLength.intValue());
try {
outputStream.write(arr);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.ReadEncryptedFileRaw(pfExportCallback, null, pvContext.getValue()));
outputStream.close();
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext.getValue());
// open file for import
String lbFileName2 = System.getProperty("java.io.tmpdir") + File.separator + "backup-" + file.getName();
ULONG ulFlags2 = new ULONG(CREATE_FOR_IMPORT);
PointerByReference pvContext2 = new PointerByReference();
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.OpenEncryptedFileRaw(lbFileName2, ulFlags2, pvContext2));
// write encrypted file
final IntByReference elementsReadWrapper = new IntByReference(0);
FE_IMPORT_FUNC pfImportCallback = new FE_IMPORT_FUNC() {
@Override
public DWORD callback(Pointer pbData, Pointer pvCallbackContext, ULONGByReference ulLength) {
int elementsRead = elementsReadWrapper.getValue();
int remainingElements = outputStream.size() - elementsRead;
int length = Math.min(remainingElements, ulLength.getValue().intValue());
pbData.write(0, outputStream.toByteArray(), elementsRead, length);
elementsReadWrapper.setValue(elementsRead + length);
ulLength.setValue(new ULONG(length));
return new DWORD(W32Errors.ERROR_SUCCESS);
}
};
assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.WriteEncryptedFileRaw(pfImportCallback, null, pvContext2.getValue()));
Advapi32.INSTANCE.CloseEncryptedFileRaw(pvContext2.getValue());
file.delete();
new File(lbFileName2.toString()).delete();
}
use of com.sun.jna.Pointer in project jnanomsg by niwinz.
the class AbstractSocket method recvBytes.
public synchronized byte[] recvBytes(final EnumSet<SocketFlag> flagSet) throws IOException {
final PointerByReference ptrBuff = new PointerByReference();
int flags = 0;
if (flagSet.contains(SocketFlag.NN_DONTWAIT)) {
flags |= SocketFlag.NN_DONTWAIT.value();
}
final int rc = nn_recv(this.fd, ptrBuff, NN_MSG, flags);
if (rc < 0) {
Nanomsg.handleError(rc);
}
final Pointer result = ptrBuff.getValue();
final byte[] bytesResult = result.getByteArray(0, rc);
return bytesResult;
}
use of com.sun.jna.Pointer in project jnanomsg by niwinz.
the class AbstractSocket method recv.
public ByteBuffer recv(final EnumSet<SocketFlag> flagSet) throws IOException {
final PointerByReference ptrBuff = new PointerByReference();
int flags = 0;
if (flagSet.contains(SocketFlag.NN_DONTWAIT)) {
flags |= SocketFlag.NN_DONTWAIT.value();
}
final int rc = nn_recv(this.fd, ptrBuff, NN_MSG, flags);
if (rc < 0) {
Nanomsg.handleError(rc);
}
final Pointer result = ptrBuff.getValue();
final ByteBuffer buffer = result.getByteBuffer(0, rc);
return buffer;
}
use of com.sun.jna.Pointer in project voldemort by voldemort.
the class mman method mmap.
/* Changes are private. */
// http://linux.die.net/man/2/mmap
// http://www.opengroup.org/sud/sud1/xsh/mmap.htm
// http://linux.die.net/include/sys/mman.h
// http://linux.die.net/include/bits/mman.h
// off_t = 8
// size_t = 8
/**
* Map the given region of the given file descriptor into memory.
* Returns a Pointer to the newly mapped memory throws an
* IOException on error.
*/
public static Pointer mmap(long len, int prot, int flags, int fildes, long off) throws IOException {
// we don't really have a need to change the recommended pointer.
Pointer addr = new Pointer(0);
Pointer result = Delegate.mmap(addr, new NativeLong(len), prot, flags, fildes, new NativeLong(off));
if (Pointer.nativeValue(result) == -1) {
if (logger.isDebugEnabled())
logger.debug(errno.strerror());
throw new IOException("mmap failed: " + errno.strerror());
}
return result;
}
use of com.sun.jna.Pointer in project voldemort by voldemort.
the class mman method main.
public static void main(String[] args) throws Exception {
String path = args[0];
File file = new File(path);
FileInputStream in = new FileInputStream(file);
int fd = voldemort.store.readonly.io.Native.getFd(in.getFD());
if (logger.isDebugEnabled())
logger.debug("File descriptor is: " + fd);
// mmap a large file...
Pointer addr = mmap(file.length(), PROT_READ, mman.MAP_SHARED, fd, 0L);
if (logger.isDebugEnabled())
logger.debug("mmap address is: " + Pointer.nativeValue(addr));
// try to mlock it directly
mlock(addr, file.length());
munlock(addr, file.length());
munmap(addr, file.length());
}
Aggregations