Search in sources :

Example 81 with Pointer

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();
}
Also used : ULONG(com.sun.jna.platform.win32.WinDef.ULONG) IntByReference(com.sun.jna.ptr.IntByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) Pointer(com.sun.jna.Pointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ULONGByReference(com.sun.jna.platform.win32.WinDef.ULONGByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) FE_EXPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC) File(java.io.File) FE_IMPORT_FUNC(com.sun.jna.platform.win32.WinBase.FE_IMPORT_FUNC)

Example 82 with Pointer

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;
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer)

Example 83 with Pointer

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;
}
Also used : PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer) ByteBuffer(java.nio.ByteBuffer)

Example 84 with Pointer

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;
}
Also used : NativeLong(com.sun.jna.NativeLong) Pointer(com.sun.jna.Pointer) IOException(java.io.IOException)

Example 85 with Pointer

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());
}
Also used : Pointer(com.sun.jna.Pointer) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Pointer (com.sun.jna.Pointer)93 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Memory (com.sun.jna.Memory)15 IntByReference (com.sun.jna.ptr.IntByReference)15 Test (org.junit.Test)12 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)7 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)7 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)7 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 File (java.io.File)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HWND (com.sun.jna.platform.win32.WinDef.HWND)6 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)6 IOException (java.io.IOException)6 BufferedImage (java.awt.image.BufferedImage)5 Function (com.sun.jna.Function)4 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)4