/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2017 The eXist-db Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
package org.exist.util;

/**
 * A pool for byte arrays.
 * <p>
 * This pool is primarily used while parsing documents: serializing the
 * DOM nodes generates a lot of small byte chunks. Only byte arrays
 * with length &lt; MAX are kept in the pool. Large arrays are rarely
 * reused.
 */
public class ByteArrayPool {

    private static final int POOL_SIZE = 32;
    private static final int MAX = 128;
    private static final ThreadLocal<byte[][]> pools_ = new PoolThreadLocal();
    private static int slot_ = 0;

    private ByteArrayPool() {
    }

    public static byte[] getByteArray(final int size) {
        final byte[][] pool = pools_.get();
        if (size < MAX) {
            for (int i = pool.length; i-- > 0; ) {
                if (pool[i] != null && pool[i].length == size) {
                    //System.out.println("found byte[" + size + "]");
                    final byte[] b = pool[i];
                    pool[i] = null;
                    return b;
                }
            }
        }
        return new byte[size];
    }

    public static void releaseByteArray(final byte[] b) {
        if (b == null || b.length > MAX) {
            return;
        }
        //System.out.println("releasing byte[" + b.length + "]");
        final byte[][] pool = pools_.get();
        for (int i = pool.length; i-- > 0; ) {
            if (pool[i] == null) {
                pool[i] = b;
                return;
            }
        }

        // TODO(AR) I note that this ++ operation is not thread-safe, slot requires some form of synchronization
        int s = slot_++;
        if (s < 0) {
            s = -s;
        }
        pool[s % pool.length] = b;
    }

    private static final class PoolThreadLocal extends ThreadLocal<byte[][]> {

        @Override
        protected byte[][] initialValue() {
            return new byte[POOL_SIZE][];
        }

    }
}

@@ -881,16 +881,108 @@
 .util;%0A%0A
+import net.jcip.annotations.ThreadSafe;%0A%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0A
 /**%0A * A
@@ -1236,16 +1236,28 @@
 ed.%0A */%0A
+@ThreadSafe%0A
 public c
@@ -1465,21 +1465,49 @@
 tic 
-int slot_ = 0
+AtomicInteger slot_ = new AtomicInteger()
 ;%0A%0A 
@@ -2459,136 +2459,39 @@
     
-// TODO(AR) I note that this ++ operation is not thread-safe, slot requires some form of synchronization%0A        int s = slot_++
+int s = slot_.incrementAndGet()
 ;%0A  

Fix concurrency issue in ByteArrayPool