This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch 2.2.X
in repository https://gitbox.apache.org/repos/asf/mina.git


The following commit(s) were added to refs/heads/2.2.X by this push:
     new f383da4d8 Applied patch for PR #64 (Flaky AbstractFileRegionTest 
failures due to byte alignment)
f383da4d8 is described below

commit f383da4d890a5a350feb3e85592f9f7db924a5fc
Author: emmanuel lecharny <[email protected]>
AuthorDate: Tue Sep 22 06:16:25 2026 +0200

    Applied patch for PR #64 (Flaky AbstractFileRegionTest failures due to byte 
alignment)
---
 .../mina/transport/AbstractFileRegionTest.java     | 40 ++++++++++++++++++++--
 1 file changed, 38 insertions(+), 2 deletions(-)

diff --git 
a/mina-core/src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java 
b/mina-core/src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java
index 7c3d82565..965822160 100644
--- 
a/mina-core/src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java
+++ 
b/mina-core/src/test/java/org/apache/mina/transport/AbstractFileRegionTest.java
@@ -69,6 +69,7 @@ public abstract class AbstractFileRegionTest {
         try {
             acceptor.setHandler(new IoHandlerAdapter() {
                 private int index = 0;
+                private ByteBuffer localBuffer = ByteBuffer.allocate(0);
 
                 @Override
                 public void exceptionCaught(IoSession session, Throwable 
cause) throws Exception {
@@ -79,8 +80,15 @@ public abstract class AbstractFileRegionTest {
                 @Override
                 public void messageReceived(IoSession session, Object message) 
throws Exception {
                     IoBuffer buffer = (IoBuffer) message;
-                    while (buffer.hasRemaining()) {
-                        int x = buffer.getInt();
+
+                    // copy message to the local buffer (expand if necessary)
+                    localBuffer.compact();
+                    localBuffer = copy(buffer.buf(), localBuffer);
+                    localBuffer.flip();
+
+                    while (localBuffer.remaining() >= 4) {
+                        int x = localBuffer.getInt();
+
                         if (x != index) {
                             throw new Exception(String.format("Integer at %d 
was %d but should have been %d", index, x,
                                     index));
@@ -138,6 +146,34 @@ public abstract class AbstractFileRegionTest {
         }
     }
 
+    /**
+     * Copies the remaining bytes of {@code src} into {@code dst}, growing the 
destination buffer first if it doesn't
+     * have enough remaining capacity to hold them.
+     * <p>
+     * The {@code src} buffer's position, limit, and mark are not modified. 
The {@code dst} buffer's position is
+     * advanced by the number of bytes copied; if the buffer was grown, a new 
{@link ByteBuffer} instance is returned
+     * (the original {@code dst} reference becomes stale and must not be used 
further).
+     */
+    public static ByteBuffer copy(ByteBuffer src, ByteBuffer dst) {
+        if (dst.remaining() < src.remaining()) {
+            int newCapacity = dst.position() + src.remaining();
+            ByteBuffer newDst = dst.isDirect() ? 
ByteBuffer.allocateDirect(newCapacity) : ByteBuffer.allocate(newCapacity);
+            newDst.order(dst.order());
+
+            for (int i = 0; i < dst.position(); i++) {
+                newDst.put(dst.get(i));
+            }
+
+            dst = newDst;
+        }
+
+        for (int i = src.position(); i < src.limit(); i++) {
+            dst.put(src.get(i));
+        }
+
+        return dst;
+    }
+
     private File createLargeFile() throws IOException {
         File largeFile = File.createTempFile("mina-test", "largefile");
         largeFile.deleteOnExit();

Reply via email to