/** * Return a direct allocated * bytebuffer from the compression codec. * The size of the bytebuffer is calculated to be: * 40: 8 + 32 * two ints representing their enum values * for the compression algorithm and opType * * and 4 longs for the compressed and * original sizes * @return the bytebuffer described above */ public ByteBuffer toByteBuffer() { //2 ints at 4 bytes a piece, this includes the compression algorithm //that we convert to enum int enumSize = 2 * 4; //4 longs at 8 bytes a piece int sizesLength = 4 * 8; ByteBuffer directAlloc = ByteBuffer.allocateDirect(enumSize + sizesLength).order(ByteOrder.nativeOrder()); directAlloc.putInt(compressionType.ordinal()); directAlloc.putInt(CompressionAlgorithm.valueOf(compressionAlgorithm).ordinal()); directAlloc.putLong(originalLength); directAlloc.putLong(compressedLength); directAlloc.putLong(numberOfElements); directAlloc.putLong(originalElementSize); directAlloc.rewind(); return directAlloc; }
/** * Instantiate a compression descriptor from * the given bytebuffer * @param byteBuffer the bytebuffer to instantiate * the descriptor from * @return the instantiated descriptor based on the given * bytebuffer */ public static CompressionDescriptor fromByteBuffer(ByteBuffer byteBuffer) { CompressionDescriptor compressionDescriptor = new CompressionDescriptor(); //compression opType int compressionTypeOrdinal = byteBuffer.getInt(); CompressionType compressionType = CompressionType.values()[compressionTypeOrdinal]; compressionDescriptor.setCompressionType(compressionType); //compression algo int compressionAlgoOrdinal = byteBuffer.getInt(); CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.values()[compressionAlgoOrdinal]; compressionDescriptor.setCompressionAlgorithm(compressionAlgorithm.name()); //from here everything is longs compressionDescriptor.setOriginalLength(byteBuffer.getLong()); compressionDescriptor.setCompressedLength(byteBuffer.getLong()); compressionDescriptor.setNumberOfElements(byteBuffer.getLong()); compressionDescriptor.setOriginalElementSize(byteBuffer.getLong()); return compressionDescriptor; }
/** * Instantiate a compression descriptor from * the given bytebuffer * @param byteBuffer the bytebuffer to instantiate * the descriptor from * @return the instantiated descriptor based on the given * bytebuffer */ public static CompressionDescriptor fromByteBuffer(ByteBuffer byteBuffer) { CompressionDescriptor compressionDescriptor = new CompressionDescriptor(); //compression type int compressionTypeOrdinal = byteBuffer.getInt(); CompressionType compressionType = CompressionType.values()[compressionTypeOrdinal]; compressionDescriptor.setCompressionType(compressionType); //compression algo int compressionAlgoOrdinal = byteBuffer.getInt(); CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.values()[compressionAlgoOrdinal]; compressionDescriptor.setCompressionAlgorithm(compressionAlgorithm.name()); //from here everything is longs compressionDescriptor.setOriginalLength(byteBuffer.getLong()); compressionDescriptor.setCompressedLength(byteBuffer.getLong()); compressionDescriptor.setNumberOfElements(byteBuffer.getLong()); compressionDescriptor.setOriginalElementSize(byteBuffer.getLong()); return compressionDescriptor; }
/** * Return a direct allocated * bytebuffer from the compression codec. * The size of the bytebuffer is calculated to be: * 40: 8 + 32 * two ints representing their enum values * for the compression algorithm and type * * and 4 longs for the compressed and * original sizes * @return the bytebuffer described above */ public ByteBuffer toByteBuffer() { //2 ints at 4 bytes a piece, this includes the compression algorithm //that we convert to enum int enumSize = 2 * 4; //4 longs at 8 bytes a piece int sizesLength = 4 * 8; ByteBuffer directAlloc = ByteBuffer.allocateDirect(enumSize + sizesLength).order(ByteOrder.nativeOrder()); directAlloc.putInt(compressionType.ordinal()); directAlloc.putInt(CompressionAlgorithm.valueOf(compressionAlgorithm).ordinal()); directAlloc.putLong(originalLength); directAlloc.putLong(compressedLength); directAlloc.putLong(numberOfElements); directAlloc.putLong(originalElementSize); directAlloc.rewind(); return directAlloc; }