Exemplo n.º 1
0
 def test_buffers(self):
     from java.nio import ByteBuffer, ByteOrder
     if sys.version_info.major > 2:
         b = ByteBuffer.allocateDirect(16).order(ByteOrder.nativeOrder())
         v = memoryview(b)
         v[0] = 7
         self.assertEqual(v[0], b.get(0))
         s = b.asShortBuffer()
         v = memoryview(s)
         self.assertEqual(v[0], s.get(0))
         i = b.asIntBuffer()
         v = memoryview(i)
         self.assertEqual(v[0], i.get(0))
         l = b.asLongBuffer()
         v = memoryview(l)
         self.assertEqual(v[0], l.get(0))
         f = b.asFloatBuffer()
         v = memoryview(f)
         v[0] = -100
         self.assertEqual(v[0], f.get(0))
         d = b.asDoubleBuffer()
         v = memoryview(d)
         v[0] = -100
         self.assertEqual(v[0], d.get(0))
     try:
         # memoryview only supports native order so numpy is required for the other one.
         from numpy import asarray
         for order in (ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN):
             b = ByteBuffer.allocateDirect(16).order(order)
             v = asarray(b)
             v[0] = 7
             self.assertEqual(v[0], b.get(0))
             s = b.asShortBuffer()
             v = asarray(s)
             self.assertEqual(v[0], s.get(0))
             i = b.asIntBuffer()
             v = asarray(i)
             self.assertEqual(v[0], i.get(0))
             l = b.asLongBuffer()
             v = asarray(l)
             self.assertEqual(v[0], l.get(0))
             f = b.asFloatBuffer()
             v = asarray(f)
             v[0] = -100
             self.assertEqual(v[0], f.get(0))
             d = b.asDoubleBuffer()
             v = asarray(d)
             v[0] = -100
             self.assertEqual(v[0], d.get(0))
     except ImportError:
         pass  # not built with numpy
Exemplo n.º 2
0
    def testDirectNative(self):
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48).order(ByteOrder.nativeOrder())
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
Exemplo n.º 3
0
    def testDirectNative(self):
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48).order(ByteOrder.nativeOrder())
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
Exemplo n.º 4
0
    def testDirect(self):
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
        with self.assertRaises(ValueError):
            self.createNdarrayFromBuffer(buffer.asCharBuffer())
Exemplo n.º 5
0
    def testDirect(self):
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        self.assertIntDirect(buffer)
        self.assertIntDirect(buffer.asShortBuffer())
        self.assertIntDirect(buffer.asIntBuffer())
        self.assertIntDirect(buffer.asLongBuffer())
        self.assertFloatDirect(buffer.asFloatBuffer())
        self.assertFloatDirect(buffer.asDoubleBuffer())
        with self.assertRaises(ValueError):
            self.createNdarrayFromBuffer(buffer.asCharBuffer())
Exemplo n.º 6
0
def _create_byte_buffer(data):
    """
    Converts data into a ByteBuffer. This is useful for interfacing with the Gb
    class.
    """
    buf = ByteBuffer.allocateDirect(len(data))
    if isinstance(data[0], int):
        for byte in data:
            buf.put(byte)
    else:
        for byte in data:
            buf.put(ord(byte))
    return buf
Exemplo n.º 7
0
 def test_buffer_no_array(self):
     """
     Directly tests StreamIO with and without a backing array and an
     InputStream that returns early.
     """
     size = 10000
     without_array = ByteBuffer.allocateDirect(size)
     self.assertFalse(without_array.hasArray())
     with_array = ByteBuffer.allocate(size)
     self.assertTrue(with_array.hasArray())
     bbs = [with_array, without_array]
     for bb in bbs:
         iis = InfiniteInputStream()
         io = StreamIO(iis, True)
         self.assertEqual(io.readinto(bb), size)
Exemplo n.º 8
0
 def test_buffer_no_array(self):
     """
     Directly tests StreamIO with and without a backing array and an
     InputStream that returns early.
     """
     size = 10000
     without_array = ByteBuffer.allocateDirect(size)
     self.assertFalse(without_array.hasArray())
     with_array = ByteBuffer.allocate(size)
     self.assertTrue(with_array.hasArray())
     bbs = [with_array, without_array]
     for bb in bbs:
         iis = InfiniteInputStream()
         io = StreamIO(iis, True)
         self.assertEqual(io.readinto(bb), size)
Exemplo n.º 9
0
    def testPassingDirect(self):
        """
        Test that when a numpy ndarray created from a java direct buffer is
        passed to java that it still uses the exact same direct buffer. The
        simplest way to test that is to copy it back to python and ensure
        modifications are visible in both numpy ndarrays
        """
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer)

        from java.util import ArrayList
        a = ArrayList()
        a.add(ndarray)
        ndarray2 = a.get(0)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])
Exemplo n.º 10
0
    def testPassingDirect(self):
        """
        Test that when a numpy ndarray created from a java direct buffer is
        passed to java that it still uses the exact same direct buffer. The
        simplest way to test that is to copy it back to python and ensure
        modifications are visible in both numpy ndarrays
        """
        from java.nio import ByteBuffer

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer)

        from java.util import ArrayList
        a = ArrayList()
        a.add(ndarray)
        ndarray2 = a.get(0)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])
Exemplo n.º 11
0
    def testDirectArgReturn(self):
        """
        Tests making a python ndarray from a java DirectNDArray.
        It should be possible to pass to java and back and still
        reference the same memory. It should also be possible to
        pass it to java methods that expect an NDArray or a primitive
        array in which case the memory is copied.
        """
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer.asIntBuffer())
        ndarray2 = self.test.testDirectArgAndReturn(ndarray)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])

        ndarray2 = self.test.testArgAndReturn(ndarray)
        self.assertEquals(ndarray[0] + 5, ndarray2[0])

        self.assertTrue(self.test.callIntMethod(ndarray))
Exemplo n.º 12
0
    def testDirectArgReturn(self):
        """
        Tests making a python ndarray from a java DirectNDArray.
        It should be possible to pass to java and back and still
        reference the same memory. It should also be possible to
        pass it to java methods that expect an NDArray or a primitive
        array in which case the memory is copied.
        """
        from java.nio import ByteBuffer, ByteOrder

        buffer = ByteBuffer.allocateDirect(48)
        ndarray = self.createNdarrayFromBuffer(buffer.asIntBuffer())
        ndarray2 = self.test.testDirectArgAndReturn(ndarray)
        ndarray[0] = 1
        self.assertEquals(1, ndarray2[0])
        ndarray2[0] = 2
        self.assertEquals(2, ndarray[0])

        ndarray2 = self.test.testArgAndReturn(ndarray)
        self.assertEquals(ndarray[0] + 5, ndarray2[0])

        self.assertTrue(self.test.callIntMethod(ndarray))