From c7b229f03eab7e46a7af7cd21f5f9d9979d81475 Mon Sep 17 00:00:00 2001
From: Oscar Vega-Gisbert <ovega@dsic.upv.es>
Date: Tue, 3 Jun 2014 21:32:27 +0000
Subject: [PATCH] Java - slice methods: set buffer limit to its capacity before
 change its position

This commit was SVN r31943.
---
 ompi/mpi/java/java/MPI.java | 40 ++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/ompi/mpi/java/java/MPI.java b/ompi/mpi/java/java/MPI.java
index 77a5fcce01..16af6a22c8 100644
--- a/ompi/mpi/java/java/MPI.java
+++ b/ompi/mpi/java/java/MPI.java
@@ -57,6 +57,7 @@ public final class MPI
 private static boolean initialized, finalized;
 private static byte[] buffer = null; // Buffer allocation
 private static final int MAX_PROCESSOR_NAME = 256;
+private static final ByteOrder nativeOrder = ByteOrder.nativeOrder();
 
 public static final Intracomm COMM_WORLD, COMM_SELF;
 
@@ -673,7 +674,7 @@ protected static Object attrGet(byte[] value) throws MPIException
 public static ByteBuffer newByteBuffer(int capacity)
 {
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf;
 }
 
@@ -686,7 +687,7 @@ public static CharBuffer newCharBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 2;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 2);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asCharBuffer();
 }
 
@@ -699,7 +700,7 @@ public static ShortBuffer newShortBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 2;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 2);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asShortBuffer();
 }
 
@@ -712,7 +713,7 @@ public static IntBuffer newIntBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 4;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 4);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asIntBuffer();
 }
 
@@ -725,7 +726,7 @@ public static LongBuffer newLongBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 8;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 8);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asLongBuffer();
 }
 
@@ -738,7 +739,7 @@ public static FloatBuffer newFloatBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 4;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 4);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asFloatBuffer();
 }
 
@@ -751,7 +752,7 @@ public static DoubleBuffer newDoubleBuffer(int capacity)
 {
     assert capacity <= Integer.MAX_VALUE / 8;
     ByteBuffer buf = ByteBuffer.allocateDirect(capacity * 8);
-    buf.order(ByteOrder.nativeOrder());
+    buf.order(nativeOrder);
     return buf.asDoubleBuffer();
 }
 
@@ -808,9 +809,8 @@ protected static boolean isHeapBuffer(Object obj)
  */
 public static ByteBuffer slice(ByteBuffer buf, int offset)
 {
-    buf.position(offset);
-    ByteOrder order = buf.order();
-    return buf.slice().order(order);
+    return ((ByteBuffer)buf.clear().position(offset))
+            .slice().order(nativeOrder);
 }
 
 /**
@@ -822,8 +822,7 @@ public static ByteBuffer slice(ByteBuffer buf, int offset)
  */
 public static CharBuffer slice(CharBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((CharBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -835,8 +834,7 @@ public static CharBuffer slice(CharBuffer buf, int offset)
  */
 public static ShortBuffer slice(ShortBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((ShortBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -848,8 +846,7 @@ public static ShortBuffer slice(ShortBuffer buf, int offset)
  */
 public static IntBuffer slice(IntBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((IntBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -861,8 +858,7 @@ public static IntBuffer slice(IntBuffer buf, int offset)
  */
 public static LongBuffer slice(LongBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((LongBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -874,8 +870,7 @@ public static LongBuffer slice(LongBuffer buf, int offset)
  */
 public static FloatBuffer slice(FloatBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((FloatBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -887,8 +882,7 @@ public static FloatBuffer slice(FloatBuffer buf, int offset)
  */
 public static DoubleBuffer slice(DoubleBuffer buf, int offset)
 {
-    buf.position(offset);
-    return buf.slice();
+    return ((DoubleBuffer)buf.clear().position(offset)).slice();
 }
 
 /**
@@ -901,7 +895,7 @@ public static DoubleBuffer slice(DoubleBuffer buf, int offset)
 public static ByteBuffer slice(byte[] buf, int offset)
 {
     return ByteBuffer.wrap(buf, offset, buf.length - offset)
-                     .slice().order(ByteOrder.nativeOrder());
+                     .slice().order(nativeOrder);
 }
 
 /**