directbuffer.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdlib.h>
  2. #include "directbuffer.h"
  3. #define BUFFER_SIZE 1024
  4. static void *address;
  5. JNIEXPORT jobject JNICALL
  6. Java_directbuffer_createDirectByteBuffer (JNIEnv *env, jclass k)
  7. {
  8. address = malloc (BUFFER_SIZE);
  9. return (*env)->NewDirectByteBuffer (env, address, 1024);
  10. }
  11. static void
  12. test_buffer (JNIEnv *env, jobject buffer, const char *name, int len)
  13. {
  14. void *tmp = (*env)->GetDirectBufferAddress (env, buffer);
  15. if (address == tmp)
  16. printf ("PASS: address of %s\n", name);
  17. else
  18. printf ("FAIL: address of %s\n", name);
  19. int tmplen = (*env)->GetDirectBufferCapacity (env, buffer);
  20. if (len == tmplen)
  21. printf ("PASS: length of %s\n", name);
  22. else
  23. printf ("FAIL: length of %s\n", name);
  24. }
  25. JNIEXPORT void JNICALL
  26. Java_directbuffer_testDirectByteBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  27. {
  28. test_buffer (env, buffer, "direct java.nio.ByteBuffer", len);
  29. }
  30. JNIEXPORT void JNICALL
  31. Java_directbuffer_testCharBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  32. {
  33. test_buffer (env, buffer, "java.nio.CharBuffer view", len);
  34. }
  35. JNIEXPORT void JNICALL
  36. Java_directbuffer_testDoubleBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  37. {
  38. test_buffer (env, buffer, "java.nio.DoubleBuffer view", len);
  39. }
  40. JNIEXPORT void JNICALL
  41. Java_directbuffer_testFloatBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  42. {
  43. test_buffer (env, buffer, "java.nio.FloatBuffer view", len);
  44. }
  45. JNIEXPORT void JNICALL
  46. Java_directbuffer_testIntBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  47. {
  48. test_buffer (env, buffer, "java.nio.IntBuffer view", len);
  49. }
  50. JNIEXPORT void JNICALL
  51. Java_directbuffer_testLongBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  52. {
  53. test_buffer (env, buffer, "java.nio.LongBuffer view", len);
  54. }
  55. JNIEXPORT void JNICALL
  56. Java_directbuffer_testShortBuffer (JNIEnv *env, jclass k, jobject buffer, jint len)
  57. {
  58. test_buffer (env, buffer, "java.nio.ShortBuffer view", len);
  59. }