/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.libreoffice.kit;

//
// We must manually allocate direct buffers in JNI to work around a bug where Honeycomb's
// ByteBuffer.allocateDirect() grossly overallocates the direct buffer size.
// https://code.google.com/p/android/issues/detail?id=16941
//

import android.util.Log;

import java.nio.ByteBuffer;

public class DirectBufferAllocator {

    private static final String LOGTAG = DirectBufferAllocator.class.getSimpleName();

    private DirectBufferAllocator() {
    }

    private static native ByteBuffer allocateDirectBufferNative(int size);

    private static native void freeDirectBufferNative(ByteBuffer aBuffer);

    public static ByteBuffer allocate(int size) {
        Log.i(LOGTAG, "Buffer size: " + size);
        return allocateVM(size);
    }

    public static ByteBuffer free(ByteBuffer buffer) {
        return freeVM(buffer);
    }

    private static ByteBuffer allocateJNI(int size) {
        ByteBuffer directBuffer = allocateDirectBufferNative(size);

        if (directBuffer == null) {
            if (size <= 0) {
                throw new IllegalArgumentException("Invalid allocation size: " + size);
            } else {
                throw new OutOfMemoryError("allocateDirectBuffer() returned null");
            }
        } else if (!directBuffer.isDirect()) {
            throw new AssertionError("allocateDirectBuffer() did not return a direct buffer");
        }

        return directBuffer;
    }

    private static ByteBuffer freeJNI(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        if (!buffer.isDirect()) {
            throw new IllegalArgumentException("buffer must be direct");
        }

        freeDirectBufferNative(buffer);
        return null;
    }

    private static ByteBuffer allocateVM(int size) {
        ByteBuffer directBuffer = ByteBuffer.allocateDirect(size);
        if (directBuffer == null) {
            if (size <= 0) {
                throw new IllegalArgumentException("Invalid allocation size: " + size);
            } else {
                throw new OutOfMemoryError("allocateDirectBuffer() returned null");
            }
        } else if (!directBuffer.isDirect()) {
            throw new AssertionError("allocateDirectBuffer() did not return a direct buffer");
        }

        return directBuffer;
    }

    private static ByteBuffer freeVM(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        if (!buffer.isDirect()) {
            throw new IllegalArgumentException("buffer must be direct");
        }

        return null;
    }
}

@@ -605,16 +605,22 @@
 %0Apublic 
+final 
 class Di
@@ -999,22 +999,26 @@
 OGTAG, %22
-Buffer
+Allocating
  size: %22
@@ -1278,25 +1278,24 @@
 tive(size);%0A
-%0A
         if (
@@ -1698,33 +1698,32 @@
 er%22);%0A        %7D%0A
-%0A
         return d
@@ -1823,32 +1823,81 @@
 ffer == null) %7B%0A
+            Log.i(LOGTAG, %22ByteBuffer is null%22);%0A
             retu
@@ -1990,33 +1990,37 @@
 umentException(%22
-b
+ByteB
 uffer must be di
@@ -2767,32 +2767,81 @@
 ffer == null) %7B%0A
+            Log.i(LOGTAG, %22ByteBuffer is null%22);%0A
             retu
@@ -2942,17 +2942,21 @@
 eption(%22
-b
+ByteB
 uffer mu
@@ -2973,32 +2973,83 @@
 ct%22);%0A        %7D%0A
+        // can't free buffer - leave this to the VM
 %0A        return 

improve error messages in DirectBufferAllocator