Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.util;

Base class for specialized primitive array builders.
  
  public abstract class PrimitiveArrayBuilder<T>
  {
    
Let's start with small chunks; typical usage is for small arrays anyway.
 
     final static int INITIAL_CHUNK_SIZE = 12;

    
Also: let's expand by doubling up until 64k chunks (which is 16k entries for 32-bit machines)
 
     final static int SMALL_CHUNK_SIZE = (1 << 14);

    
Let's limit maximum size of chunks we use; helps avoid excessive allocation overhead for huge data sets. For now, let's limit to quarter million entries, 1 meg chunks for 32-bit machines.
 
     final static int MAX_CHUNK_SIZE = (1 << 18);
 
     // // // Data storage
 
     protected T _freeBuffer;
 
     protected Node<T> _bufferHead;
 
     protected Node<T> _bufferTail;

    
Number of total buffered entries in this buffer, counting all instances within linked list formed by following _bufferHead.
 
     protected int _bufferedEntryCount;
 
     // // // Recycled instances of sub-classes
 
     // // // Life-cycle
 
     protected PrimitiveArrayBuilder() { }
 
     /*
     /**********************************************************
     /* Public API
     /**********************************************************
      */
 
     public T resetAndStart()
     {
         _reset();
         return ( == null) ?
             _constructArray() : ;
     }

    

Returns:
Length of the next chunk to allocate
 
     public final T appendCompletedChunk(T fullChunkint fullChunkLength)
     {
         Node<T> next = new Node<T>(fullChunkfullChunkLength);
         if ( == null) { // first chunk
              =  = next;
         } else { // have something already
             .linkNext(next);
              = next;
         }
          += fullChunkLength;
         int nextLen = fullChunkLength// start with last chunk size
         // double the size for small chunks
         if (nextLen < ) {
             nextLen += nextLen;
         } else { // but by +25% for larger (to limit overhead)
             nextLen += (nextLen >> 2);
         }
         return _constructArray(nextLen);
     }
 
     public T completeAndClearBuffer(T lastChunkint lastChunkEntries)
     {
         int totalSize = lastChunkEntries + ;
         T resultArray = _constructArray(totalSize);
 
         int ptr = 0;
 
         for (Node<T> n = n != nulln = n.next()) {
             ptr = n.copyData(resultArrayptr);
         }
         System.arraycopy(lastChunk, 0, resultArrayptrlastChunkEntries);
         ptr += lastChunkEntries;
 
         // sanity check (could have failed earlier due to out-of-bounds, too)
         if (ptr != totalSize) {
             throw new IllegalStateException("Should have gotten "+totalSize+" entries, got "+ptr);
         }
        return resultArray;
    }
    /*
    /**********************************************************
    /* Abstract methods for sub-classes to implement
    /**********************************************************
     */
    protected abstract T _constructArray(int len);
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    protected void _reset()
    {
        // can we reuse the last (and thereby biggest) array for next time?
        if ( != null) {
             = .getData();
        }
        // either way, must discard current contents
         =  = null;
         = 0;
    }
    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */

    
For actual buffering beyond the current buffer, we can actually use shared class which only deals with opaque "untyped" chunks. This works because java.lang.System.arraycopy(java.lang.Object,int,java.lang.Object,int,int) does not take type; hence we can implement some aspects of primitive data handling in generic fashion.
    final static class Node<T>
    {
        
Data stored in this node.
        final T _data;

        
Number entries in the (untyped) array. Offset is assumed to be 0.
        final int _dataLength;
        Node<T> _next;
        public Node(T dataint dataLen)
        {
             = data;
             = dataLen;
        }
        public T getData() { return ; }
        public int copyData(T dstint ptr)
        {
            System.arraycopy(, 0, dstptr);
            ptr += ;
            return ptr;
        }
        public Node<T> next() { return ; }
        public void linkNext(Node<T> next)
        {
            if ( != null) { // sanity check
                throw new IllegalStateException();
            }
             = next;
        }
    }
New to GrepCode? Check out our FAQ X