public synchronized byte [ ] allocate ( int minSize ) { if ( minSize <= MAX_SIZE ) { for ( int i = 0 ; i < SIZES . length ; i ++ ) { if ( minSize < SIZES [ i ] ) { if ( ! fastBuffers . get ( SIZES [ i ] ) . isEmpty ( ) ) { Iterator < byte [ ] > interator = fastBuffers . get ( SIZES [ i ] ) . iterator ( ) ; byte [ ] res = interator . next ( ) ; interator . remove ( ) ; mainFilter . remove ( res ) ; if ( TRACK_ALLOCATIONS ) { references . put ( res , Thread . currentThread ( ) . getStackTrace ( ) ) ; } return res ; } return new byte [ SIZES [ i ] ] ; } } } else { byte [ ] res = null ; for ( byte [ ] cached : byteBuffer ) { if ( cached . length < minSize ) { continue ; } if ( res == null ) { res = cached ; } else if ( res . length > cached . length ) { res = cached ; } } if ( res != null ) { byteBuffer . remove ( res ) ; mainFilter . remove ( res ) ; if ( TRACK_ALLOCATIONS ) { references . put ( res , Thread . currentThread ( ) . getStackTrace ( ) ) ; } return res ; } } return new byte [ minSize ] ; }
Allocating new byte array with minimal size