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

com.sun.xml.ws.transport.tcp.util.ByteBufferFactory Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.xml.ws.transport.tcp.util;


import java.nio.ByteBuffer;


/**
 * Class was copied from GlassFish Grizzly sources to be available
 * also for client side and don't require GlassFish to be installed
 *
 * Factory class used to create views of a ByteBuffer. 
 * The ByteBuffer can by direct or not.
 *
 * @author Jean-Francois Arcand
 */
public final class ByteBufferFactory{

    
    /**
     * The default capacity of the default view of a ByteBuffer
     */ 
    public static int defaultCapacity = 9000;
    
    
    /**
     * The default capacity of the ByteBuffer from which views
     * will be created.
     */
    public static int capacity = 4000000; 
    
    
    /**
     * The ByteBuffer used to create views.
     */
    private static ByteBuffer byteBuffer;
            
    
    /**
     * Private constructor.
     */
    private ByteBufferFactory(){
    }
    
    
    /**
     * Return a direct ByteBuffer view
     * @param size the Size of the ByteBuffer
     */ 
    public synchronized static ByteBuffer allocateView(final int size, final boolean direct){
        if (byteBuffer == null || 
               (byteBuffer.capacity() - byteBuffer.limit() < size)){
            if ( direct )
                byteBuffer = ByteBuffer.allocateDirect(capacity); 
            else
                byteBuffer = ByteBuffer.allocate(capacity);              
        }

        byteBuffer.limit(byteBuffer.position() + size);
        final ByteBuffer view = byteBuffer.slice();
        byteBuffer.position(byteBuffer.limit());  
        
        return view;
    }

    
    /**
     * Return a direct ByteBuffer view using the default size.
     */ 
    public synchronized static ByteBuffer allocateView(final boolean direct){
        return allocateView(defaultCapacity, direct);
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy