1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org
.libreoffice
.kit
;
8 import java
.nio
.ByteBuffer
;
11 * This is the common code for allocation and freeing of memory. For this direct ByteBuffer is used but
12 * in the past it was possible to use a JNI version of allocation because of a bug in old Android version.
14 public final class DirectBufferAllocator
{
16 private static final String LOGTAG
= DirectBufferAllocator
.class.getSimpleName();
18 private DirectBufferAllocator() {
21 public static ByteBuffer
allocate(int size
) {
22 ByteBuffer directBuffer
= ByteBuffer
.allocateDirect(size
);
23 if (directBuffer
== null) {
25 throw new IllegalArgumentException("Invalid allocation size: " + size
);
27 throw new OutOfMemoryError("allocateDirectBuffer() returned null");
29 } else if (!directBuffer
.isDirect()) {
30 throw new AssertionError("allocateDirectBuffer() did not return a direct buffer");
36 public static ByteBuffer
free(ByteBuffer buffer
) {
41 if (!buffer
.isDirect()) {
42 throw new IllegalArgumentException("ByteBuffer must be direct");
44 // can't free buffer - leave this to the VM
48 public static ByteBuffer
guardedAllocate(int size
) {
49 ByteBuffer buffer
= null;
51 buffer
= allocate(size
);
52 } catch (OutOfMemoryError oomException
) {