Class BytePointer

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    @Properties(inherit=javacpp.class)
    public class BytePointer
    extends Pointer
    The peer class to native pointers and arrays of signed char, including strings. All operations take into account the position and limit, when appropriate.
    • Field Detail

      • logger

        private static final Logger logger
    • Constructor Detail

      • BytePointer

        public BytePointer​(java.lang.String s,
                           java.lang.String charsetName)
                    throws java.io.UnsupportedEncodingException
        Allocates enough memory for the encoded string and actually encodes it in the named charset before copying it.
        Parameters:
        s - the String to encode and copy
        charsetName - the charset in which the bytes are encoded
        Throws:
        java.io.UnsupportedEncodingException
        See Also:
        putString(String, String)
      • BytePointer

        public BytePointer​(java.lang.String s,
                           java.nio.charset.Charset charset)
        Allocates enough memory for the encoded string and actually encodes it in the given charset before copying it.
        Parameters:
        s - the String to encode and copy
        charset - the charset in which the bytes are encoded
        See Also:
        putString(String, Charset)
      • BytePointer

        public BytePointer​(java.lang.String s)
        Allocates enough memory for the encoded string and actually encodes it in the platform's default charset before copying it.
        Parameters:
        s - the String to encode and copy
        See Also:
        putString(String)
      • BytePointer

        public BytePointer​(byte... array)
        Allocates enough memory for the array and copies it.
        Parameters:
        array - the array to copy
        See Also:
        put(byte[])
      • BytePointer

        public BytePointer​(java.nio.ByteBuffer buffer)
        For direct buffers, calls Pointer(Buffer), while for buffers backed with an array, allocates enough memory for the array and copies it.
        Parameters:
        buffer - the Buffer to reference or copy
        See Also:
        put(byte[])
      • BytePointer

        public BytePointer​(long size)
        Allocates a native signed char array of the given size.
        Parameters:
        size - the number of signed char elements to allocate
      • BytePointer

        public BytePointer()
        See Also:
        Pointer()
    • Method Detail

      • allocateArray

        private void allocateArray​(long size)
      • position

        public BytePointer position​(long position)
        Description copied from class: Pointer
        Sets the position and returns this. That makes the array.position(i) statement sort of equivalent to the array[i] statement in C++.
        Overrides:
        position in class Pointer
        Parameters:
        position - the new position
        Returns:
        this
        See Also:
        Pointer.position(long)
      • limit

        public BytePointer limit​(long limit)
        Description copied from class: Pointer
        Sets the limit and returns this. Used to limit the size of an operation on this object.
        Overrides:
        limit in class Pointer
        Parameters:
        limit - the new limit
        Returns:
        this
        See Also:
        Pointer.limit(long)
      • capacity

        public BytePointer capacity​(long capacity)
        Description copied from class: Pointer
        Sets the capacity and returns this. Should not be called more than once after allocation.
        Overrides:
        capacity in class Pointer
        Parameters:
        capacity - the new capacity
        Returns:
        this
        See Also:
        Pointer.capacity(long)
      • sizeof

        public int sizeof()
        Description copied from class: Pointer
        Returns 1 for Pointer or BytePointer else Loader.sizeof(getClass()) or -1 on error.
        Overrides:
        sizeof in class Pointer
      • getStringBytes

        public byte[] getStringBytes()
        Returns the bytes, assuming a null-terminated string if limit <= position.
      • getString

        public java.lang.String getString​(java.lang.String charsetName)
                                   throws java.io.UnsupportedEncodingException
        Decodes the native bytes assuming they are encoded in the named charset. Assumes a null-terminated string if limit <= position.
        Parameters:
        charsetName - the charset in which the bytes are encoded
        Returns:
        a String from the null-terminated string
        Throws:
        java.io.UnsupportedEncodingException
      • getString

        public java.lang.String getString​(java.nio.charset.Charset charset)
        Decodes the native bytes assuming they are encoded in the given charset. Assumes a null-terminated string if limit <= position.
        Parameters:
        charset - the charset in which the bytes are encoded
        Returns:
        a String from the null-terminated string
      • getString

        public java.lang.String getString()
        Decodes the native bytes assuming they are encoded in the platform's default charset. Assumes a null-terminated string if limit <= position.
        Returns:
        a String from the null-terminated string
      • putString

        public BytePointer putString​(java.lang.String s,
                                     java.lang.String charsetName)
                              throws java.io.UnsupportedEncodingException
        Encodes the String into the named charset and copies it in native memory, including a terminating null byte. Sets the limit to just before the terminating null byte.
        Parameters:
        s - the String to encode and copy
        charsetName - the charset in which to encode the bytes
        Returns:
        this
        Throws:
        java.io.UnsupportedEncodingException
        See Also:
        String.getBytes(String), put(byte[])
      • putString

        public BytePointer putString​(java.lang.String s,
                                     java.nio.charset.Charset charset)
        Encodes the String into the given charset and copies it in native memory, including a terminating null byte. Sets the limit to just before the terminating null byte.
        Parameters:
        s - the String to encode and copy
        charset - the charset in which to encode the bytes
        Returns:
        this
        See Also:
        String.getBytes(Charset), put(byte[])
      • putString

        public BytePointer putString​(java.lang.String s)
        Encodes the String into the platform's default charset and copies it in native memory, including a terminating null byte. Sets the limit to just before the terminating null byte.
        Parameters:
        s - the String to encode and copy
        Returns:
        this
        See Also:
        String.getBytes(), put(byte[])
      • get

        public byte get()
        Returns:
        get(0)
      • get

        public byte get​(long i)
        Returns:
        the i-th byte value of a native array
      • put

        public BytePointer put​(byte b)
        Returns:
        put(0, b)
      • put

        public BytePointer put​(long i,
                               byte b)
        Copies the byte value to the i-th element of a native array.
        Parameters:
        i - the index into the array
        b - the byte value to copy
        Returns:
        this
      • get

        public BytePointer get​(byte[] array)
        Returns:
        get(array, 0, array.length)
      • put

        public BytePointer put​(byte... array)
        Returns:
        put(array, 0, array.length)
      • get

        public BytePointer get​(byte[] array,
                               int offset,
                               int length)
        Reads a portion of the native array into a Java array.
        Parameters:
        array - the array to write to
        offset - the offset into the array where to start writing
        length - the length of data to read and write
        Returns:
        this
      • put

        public BytePointer put​(byte[] array,
                               int offset,
                               int length)
        Writes a portion of a Java array into the native array.
        Parameters:
        array - the array to read from
        offset - the offset into the array where to start reading
        length - the length of data to read and write
        Returns:
        this
      • getUnsigned

        public int getUnsigned()
        Returns getUnsigned(0).
      • getUnsigned

        public int getUnsigned​(long i)
        Returns the byte value at the i-th byte in the native array, treated as unsigned.
      • putUnsigned

        public BytePointer putUnsigned​(int b)
        Returns putUnsigned(0, b).
      • putUnsigned

        public BytePointer putUnsigned​(long i,
                                       int b)
        Sets the byte value at the i-th byte in the native array, treated as unsigned.
      • getShort

        public short getShort()
        Returns getShort(0).
      • getShort

        @ValueGetter
        @Cast("short")
        public short getShort​(long i)
        Returns the short value at the i-th byte in the native array.
      • putShort

        public BytePointer putShort​(short s)
        Returns putShort(0, s).
      • putShort

        @ValueSetter
        @Cast("short")
        public BytePointer putShort​(long i,
                                    short s)
        Sets the short value at the i-th byte in the native array.
      • getInt

        public int getInt()
        Returns getInt(0).
      • getInt

        @ValueGetter
        @Cast("int")
        public int getInt​(long i)
        Returns the int value at the i-th byte in the native array.
      • putInt

        public BytePointer putInt​(int j)
        Returns putInt(0, s).
      • getLong

        public long getLong()
        Returns getLong(0).
      • getLong

        @ValueGetter
        @Cast("long long")
        public long getLong​(long i)
        Returns the long value at the i-th byte in the native array.
      • putLong

        public BytePointer putLong​(long j)
        Returns putLong(0, s).
      • putLong

        @ValueSetter
        @Cast("long long")
        public BytePointer putLong​(long i,
                                   long j)
        Sets the long value at the i-th byte in the native array.
      • getFloat

        public float getFloat()
        Returns getFloat(0).
      • getFloat

        @ValueGetter
        @Cast("float")
        public float getFloat​(long i)
        Returns the float value at the i-th byte in the native array.
      • putFloat

        public BytePointer putFloat​(float f)
        Returns putFloat(0, s).
      • putFloat

        @ValueSetter
        @Cast("float")
        public BytePointer putFloat​(long i,
                                    float f)
        Sets the float value at the i-th byte in the native array.
      • getDouble

        public double getDouble()
        Returns getDouble(0).
      • getDouble

        @ValueGetter
        @Cast("double")
        public double getDouble​(long i)
        Returns the double value at the i-th byte in the native array.
      • putDouble

        public BytePointer putDouble​(double d)
        Returns putDouble(0, s).
      • putDouble

        @ValueSetter
        @Cast("double")
        public BytePointer putDouble​(long i,
                                     double d)
        Sets the double value at the i-th byte in the native array.
      • getBool

        public boolean getBool()
        Returns getBool(0).
      • getBool

        @ValueGetter
        @Cast("bool")
        public boolean getBool​(long i)
        Returns the bool value at the i-th byte in the native array.
      • putBool

        public BytePointer putBool​(boolean b)
        Returns putBool(0, s).
      • putBool

        @ValueSetter
        @Cast("bool")
        public BytePointer putBool​(long i,
                                   boolean b)
        Sets the bool value at the i-th byte in the native array.
      • getChar

        public char getChar()
        Returns getChar(0).
      • getChar

        @ValueGetter
        @Cast("short")
        public char getChar​(long i)
        Returns the char value at the i-th byte in the native array.
      • putChar

        public BytePointer putChar​(char c)
        Returns putChar(0, s).
      • putChar

        @ValueSetter
        @Cast("short")
        public BytePointer putChar​(long i,
                                   char c)
        Sets the char value at the i-th byte in the native array.
      • getPointerValue

        public Pointer getPointerValue()
        Returns getPointerValue(0).
      • getPointerValue

        @ValueGetter
        @Cast("void*")
        public Pointer getPointerValue​(long i)
        Returns the Pointer value at the i-th byte in the native array.
      • putPointerValue

        public BytePointer putPointerValue​(Pointer p)
        Returns putPointerValue(0, s).