All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testifyproject.netty.channel.ThreadLocalPooledDirectByteBuf Maven / Gradle / Ivy

/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org.testifyproject/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
/*
 * Written by Josh Bloch of Google Inc. and released to the public domain,
 * as explained at http://creativecommons.org.testifyproject/publicdomain/zero/1.0/.
 */
package org.testifyproject.testifyproject.netty.channel;

import org.testifyproject.testifyproject.netty.buffer.ByteBuf;
import org.testifyproject.testifyproject.netty.buffer.UnpooledByteBufAllocator;
import org.testifyproject.testifyproject.netty.buffer.UnpooledDirectByteBuf;
import org.testifyproject.testifyproject.netty.buffer.UnpooledUnsafeDirectByteBuf;
import org.testifyproject.testifyproject.netty.util.Recycler;
import org.testifyproject.testifyproject.netty.util.internal.PlatformDependent;
import org.testifyproject.testifyproject.netty.util.internal.SystemPropertyUtil;
import org.testifyproject.testifyproject.netty.util.internal.logging.InternalLogger;
import org.testifyproject.testifyproject.netty.util.internal.logging.InternalLoggerFactory;

final class ThreadLocalPooledDirectByteBuf {
    private static final InternalLogger logger =
            InternalLoggerFactory.getInstance(ThreadLocalPooledDirectByteBuf.class);
    public static final int threadLocalDirectBufferSize;

    static {
        threadLocalDirectBufferSize = SystemPropertyUtil.getInt("org.testifyproject.testifyproject.netty.threadLocalDirectBufferSize", 64 * 1024);
        logger.org.testifyproject.testifyprojectbug("-Dio.netty.threadLocalDirectBufferSize: {}", threadLocalDirectBufferSize);
    }

    public static ByteBuf newInstance() {
        if (PlatformDependent.hasUnsafe()) {
            return ThreadLocalUnsafeDirectByteBuf.newInstance();
        } else {
            return ThreadLocalDirectByteBuf.newInstance();
        }
    }

    private ThreadLocalPooledDirectByteBuf() {
        // utility
    }

    static final class ThreadLocalUnsafeDirectByteBuf extends UnpooledUnsafeDirectByteBuf {

        private static final Recycler RECYCLER =
                new Recycler() {
            @Override
            protected ThreadLocalUnsafeDirectByteBuf newObject(Handle handle) {
                return new ThreadLocalUnsafeDirectByteBuf(handle);
            }
        };

        static ThreadLocalUnsafeDirectByteBuf newInstance() {
            ThreadLocalUnsafeDirectByteBuf buf = RECYCLER.get();
            buf.setRefCnt(1);
            return buf;
        }

        private final Recycler.Handle handle;

        private ThreadLocalUnsafeDirectByteBuf(Recycler.Handle handle) {
            super(UnpooledByteBufAllocator.DEFAULT, 256, Integer.MAX_VALUE);
            this.handle = handle;
        }

        @Override
        protected void org.testifyproject.testifyprojectallocate() {
            if (capacity() > threadLocalDirectBufferSize) {
                super.org.testifyproject.testifyprojectallocate();
            } else {
                clear();
                RECYCLER.recycle(this, handle);
            }
        }
    }

    static final class ThreadLocalDirectByteBuf extends UnpooledDirectByteBuf {

        private static final Recycler RECYCLER = new Recycler() {
            @Override
            protected ThreadLocalDirectByteBuf newObject(Handle handle) {
                return new ThreadLocalDirectByteBuf(handle);
            }
        };

        static ThreadLocalDirectByteBuf newInstance() {
            ThreadLocalDirectByteBuf buf = RECYCLER.get();
            buf.setRefCnt(1);
            return buf;
        }

        private final Recycler.Handle handle;

        private ThreadLocalDirectByteBuf(Recycler.Handle handle) {
            super(UnpooledByteBufAllocator.DEFAULT, 256, Integer.MAX_VALUE);
            this.handle = handle;
        }

        @Override
        protected void org.testifyproject.testifyprojectallocate() {
            if (capacity() > threadLocalDirectBufferSize) {
                super.org.testifyproject.testifyprojectallocate();
            } else {
                clear();
                RECYCLER.recycle(this, handle);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy