Examples of LZ4Factory

Only the {@link #safeInstance() safe instance} is guaranteed to work on yourJVM, as a consequence it is advised to use the {@link #fastestInstance()} or{@link #fastestJavaInstance()} to pull a {@link LZ4Factory} instance.

All methods from this class are very costly, so you should get an instance once, and then reuse it whenever possible. This is typically done by storing a {@link LZ4Factory} instance in a static field.


Examples of net.jpountz.lz4.LZ4Factory

  public static void main(String[] args) throws Exception {
    example();
  }

  private static void example() throws UnsupportedEncodingException {
    LZ4Factory factory = LZ4Factory.fastestInstance();

    byte[] data = "12345345234572".getBytes("UTF-8");
    final int decompressedLength = data.length;

    // compress data
    LZ4Compressor compressor = factory.fastCompressor();
    int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
    byte[] compressed = new byte[maxCompressedLength];
    int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);

    // decompress data
    // - method 1: when the decompressed length is known
    LZ4FastDecompressor decompressor = factory.fastDecompressor();
    byte[] restored = new byte[decompressedLength];
    int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
    // compressedLength == compressedLength2

    // - method 2: when the compressed length is known (a little slower)
    // the destination buffer needs to be over-sized
    LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
    int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
    // decompressedLength == decompressedLength2
  }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

        private static final int INTEGER_BYTES = 4;
        private final net.jpountz.lz4.LZ4Compressor compressor;
        private final net.jpountz.lz4.LZ4FastDecompressor decompressor;

        private LZ4Compressor() {
            final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
            compressor = lz4Factory.fastCompressor();
            decompressor = lz4Factory.fastDecompressor();
        }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

        private final net.jpountz.lz4.LZ4Compressor compressor;
        private final net.jpountz.lz4.LZ4Decompressor decompressor;

        private LZ4Compressor()
        {
            final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
            compressor = lz4Factory.fastCompressor();
            decompressor = lz4Factory.decompressor();
        }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

        private final net.jpountz.lz4.LZ4Compressor compressor;
        private final net.jpountz.lz4.LZ4Decompressor decompressor;

        private LZ4Compressor()
        {
            final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
            compressor = lz4Factory.fastCompressor();
            decompressor = lz4Factory.decompressor();
        }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

      }
   };

   @Override
   public byte[] getBytes(Object obj) {
      final LZ4Factory factory = lz4Factory.get();
      byte[] data = codecUncompressed.getBytes(obj);
      final int decompressedLength = data.length;
      LZ4Compressor compressor = factory.fastCompressor();
      int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
      byte[] compressed = new byte[4 + maxCompressedLength];
      compressor.compress(data, 0, decompressedLength, compressed, 4, maxCompressedLength);
      return compressed;
   }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

   public Object createObjectFromBytes(byte[] bytes) {
      // decompress
     
      // received format = | decompressed len (4B) | compressed bytes (bytes.length - 4) |
     
      final LZ4Factory factory = lz4Factory.get();
      int compressedLength = bytes.length - 4;
      ByteBuffer compbb = ByteBuffer.wrap(bytes);
      int decompressedLength = compbb.getInt();

     
      LZ4FastDecompressor decompressor = factory.fastDecompressor();
      byte[] decompressedBytes = new byte[decompressedLength];
      int compressedLength2 = decompressor.decompress(bytes, 4, decompressedBytes, 0, decompressedLength);
     
      assert(compressedLength == compressedLength2);
     
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

      return codecUncompressed.createObjectFromBytes(decompressedBytes);
   }

   @Override
   public ByteBuffer getByteBufferWithLengthHeader(Object obj) {
      final LZ4Factory factory = lz4Factory.get();

      byte[] data = codecUncompressed.getBytes(obj);
      final int decompressedLength = data.length;

      // compress data
      LZ4Compressor compressor = factory.fastCompressor();
      int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
      byte[] compressed = new byte[8 + maxCompressedLength];
      int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 8, maxCompressedLength);

      // tcpreceiver decoder expects | x = message length (4B) | message    (x bytes)                           |
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

      // decompress
      //                                                [-------------------- LENGTH IN HEADER -----------------------]
      //                                                                        [-----------COMPRESSED LENGTH---------]
      // received format = | compressed length + 4 (4B) | decompressed len (4B) | compressed bytes (bytes.length - 4) |
     
      final LZ4Factory factory = lz4Factory.get();
     
      int compressedLength      = buffer.getInt() - 4;
      int decompressedLength    = buffer.getInt();
      int compressedBytesOffset = buffer.arrayOffset() + buffer.position();
      byte[] compressedBytes    = buffer.array();
     
      LZ4FastDecompressor decompressor = factory.fastDecompressor();
      byte[] decompressedBytes = new byte[decompressedLength];
      int compressedLength2 = decompressor.decompress(compressedBytes, compressedBytesOffset, decompressedBytes, 0, decompressedLength);
     
      assert(compressedLength == compressedLength2);
     
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

        private final net.jpountz.lz4.LZ4Compressor compressor;
        private final net.jpountz.lz4.LZ4Decompressor decompressor;

        private LZ4Compressor()
        {
            final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
            compressor = lz4Factory.fastCompressor();
            decompressor = lz4Factory.decompressor();
        }
View Full Code Here

Examples of net.jpountz.lz4.LZ4Factory

    private final net.jpountz.lz4.LZ4Compressor compressor;
    private final net.jpountz.lz4.LZ4Decompressor decompressor;

    private LZ4Compressor()
    {
        final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
        compressor = lz4Factory.fastCompressor();
        decompressor = lz4Factory.decompressor();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.