Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.ning.compress.lzf.impl;
  
  
  import sun.misc.Unsafe;
  
com.ning.compress.lzf.ChunkEncoder implementation that handles actual encoding of individual chunks, using Sun's sun.misc.Unsafe functionality, which gives nice extra boost for speed.

Author(s):
Tatu Saloranta (tatu.saloranta@iki.fi)
 
 @SuppressWarnings("restriction")
 public abstract class UnsafeChunkEncoder
     extends ChunkEncoder
 {
     // // Our Nitro Booster, mr. Unsafe!
 
     protected static final Unsafe unsafe;
     static {
         try {
             Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
             theUnsafe.setAccessible(true);
              = (UnsafetheUnsafe.get(null);
         }
         catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
     protected static final long BYTE_ARRAY_OFFSET = .arrayBaseOffset(byte[].class);
 
     protected static final long BYTE_ARRAY_OFFSET_PLUS2 =  + 2;
     
     public UnsafeChunkEncoder(int totalLength) {
         super(totalLength);
     }
 
     public UnsafeChunkEncoder(int totalLengthboolean bogus) {
         super(totalLengthbogus);
     }
 
     /*
     ///////////////////////////////////////////////////////////////////////
     // Shared helper methods
     ///////////////////////////////////////////////////////////////////////
      */
 
     protected final static int _copyPartialLiterals(byte[] inint inPosbyte[] outint outPos,
             int literals)
     {
         out[outPos++] = (byte) (literals-1);
 
         // Here use of Unsafe is clear win:
 //        System.arraycopy(in, inPos-literals, out, outPos, literals);
 
         long rawInPtr =  + inPos - literals;
         long rawOutPtr + outPos;
 
         switch (literals >> 3) {
         case 3:
             .putLong(outrawOutPtr.getLong(inrawInPtr));
             rawInPtr += 8;
             rawOutPtr += 8;
         case 2:
             .putLong(outrawOutPtr.getLong(inrawInPtr));
             rawInPtr += 8;
             rawOutPtr += 8;
         case 1:
             .putLong(outrawOutPtr.getLong(inrawInPtr));
             rawInPtr += 8;
             rawOutPtr += 8;
         }
         int left = (literals & 7);
         if (left > 4) {
             .putLong(outrawOutPtr.getLong(inrawInPtr));
         } else {
             .putInt(outrawOutPtr.getInt(inrawInPtr));
         }
 
         return outPos+literals;
     }
 
     protected final static int _copyLongLiterals(byte[] inint inPosbyte[] outint outPos,
             int literals)
     {
         inPos -= literals;
 
         long rawInPtr =  + inPos;
         long rawOutPtr =  + outPos;
         
         while (literals >= .) {
             out[outPos++] = (byte) 31;
             ++rawOutPtr;
 
            .putLong(outrawOutPtr.getLong(inrawInPtr));
            rawInPtr += 8;
            rawOutPtr += 8;
            .putLong(outrawOutPtr.getLong(inrawInPtr));
            rawInPtr += 8;
            rawOutPtr += 8;
            .putLong(outrawOutPtr.getLong(inrawInPtr));
            rawInPtr += 8;
            rawOutPtr += 8;
            .putLong(outrawOutPtr.getLong(inrawInPtr));
            rawInPtr += 8;
            rawOutPtr += 8;
            
            inPos += .;
            outPos += .;
            literals -= .;
        }
        if (literals > 0) {
            return _copyPartialLiterals(ininPos+literalsoutoutPosliterals);
        }
        return outPos;
    }
    
    protected final static int _copyFullLiterals(byte[] inint inPosbyte[] outint outPos)
    {
        // literals == 32
        out[outPos++] = (byte) 31;
        long rawInPtr =  + inPos - 32;
        long rawOutPtr =  + outPos;
    
        .putLong(outrawOutPtr.getLong(inrawInPtr));
        rawInPtr += 8;
        rawOutPtr += 8;
        .putLong(outrawOutPtr.getLong(inrawInPtr));
        rawInPtr += 8;
        rawOutPtr += 8;
        .putLong(outrawOutPtr.getLong(inrawInPtr));
        rawInPtr += 8;
        rawOutPtr += 8;
        .putLong(outrawOutPtr.getLong(inrawInPtr));
        return (outPos + 32);
    }
    protected final static int _handleTail(byte[] inint inPosint inEndbyte[] outint outPos,
            int literals)
    {
        while (inPos < inEnd) {
            ++inPos;
            ++literals;
            if (literals == .) {
                out[outPos++] = (byte) (literals-1); // <= out[outPos - literals - 1] = MAX_LITERAL_MINUS_1;
                System.arraycopy(ininPos-literalsoutoutPosliterals);
                outPos += literals;
                literals = 0;
            }
        }
        if (literals > 0) {
            out[outPos++] = (byte) (literals - 1);
            System.arraycopy(ininPos-literalsoutoutPosliterals);
            outPos += literals;
        }
        return outPos;
    }
    protected final static int _findTailMatchLength(final byte[] inint ptr1int ptr2final int maxPtr1)
    {
        final int start1 = ptr1;
        while (ptr1 < maxPtr1 && in[ptr1] == in[ptr2]) {
            ++ptr1;
            ++ptr2;
        }
        return ptr1 - start1 + 1; // i.e. 
    }
New to GrepCode? Check out our FAQ X