Java Code Examples for org.xerial.snappy.Snappy#uncompressedLength()

The following examples show how to use org.xerial.snappy.Snappy#uncompressedLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnappyCompressor.java    From besu with Apache License 2.0 5 votes vote down vote up
public int uncompressedLength(final byte[] compressed) {
  checkNotNull(compressed, "input data must not be null");
  try {
    return Snappy.uncompressedLength(compressed);
  } catch (final IOException e) {
    throw new FramingException("Snappy uncompressedLength failed", e);
  }
}
 
Example 2
Source File: SnappyDecompressor.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
/**
  * Fills specified buffer with uncompressed data. Returns actual number
  * of bytes of uncompressed data. A return value of 0 indicates that
  * {@link #needsInput()} should be called in order to determine if more
  * input data is required.
  *
  * @param buffer   Buffer for the compressed data
  * @param off Start offset of the data
  * @param len Size of the buffer
  * @return The actual number of bytes of uncompressed data.
  * @throws IOException if reading or decompression fails
  */
 @Override
 public synchronized int decompress(byte[] buffer, int off, int len) throws IOException {
   SnappyUtil.validateBuffer(buffer, off, len);
if (inputBuffer.position() == 0 && !outputBuffer.hasRemaining()) {
     return 0;
   }
   
   if (!outputBuffer.hasRemaining()) {
     inputBuffer.rewind();
     Preconditions.checkArgument(inputBuffer.position() == 0, "Invalid position of 0.");
     Preconditions.checkArgument(outputBuffer.position() == 0, "Invalid position of 0.");
     // There is compressed input, decompress it now.
     int decompressedSize = Snappy.uncompressedLength(inputBuffer);
     if (decompressedSize > outputBuffer.capacity()) {
       ByteBuffer oldBuffer = outputBuffer;
       outputBuffer = ByteBuffer.allocateDirect(decompressedSize);
       CleanUtil.cleanDirectBuffer(oldBuffer);
     }

     // Reset the previous outputBuffer (i.e. set position to 0)
     outputBuffer.clear();
     int size = Snappy.uncompress(inputBuffer, outputBuffer);
     outputBuffer.limit(size);
     // We've decompressed the entire input, reset the input now
     inputBuffer.clear();
     inputBuffer.limit(0);
     finished = true;
   }

   // Return compressed output up to 'len'
   int numBytes = Math.min(len, outputBuffer.remaining());
   outputBuffer.get(buffer, off, numBytes);
   return numBytes;	    
 }
 
Example 3
Source File: HashCacheFactory.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Closeable newCache(ImmutableBytesWritable cachePtr, MemoryChunk chunk) throws SQLException {
    try {
        int size = Snappy.uncompressedLength(cachePtr.get());
        byte[] uncompressed = new byte[size];
        Snappy.uncompress(cachePtr.get(), 0, cachePtr.getLength(), uncompressed, 0);
        return new HashCacheImpl(uncompressed, chunk);
    } catch (IOException e) {
        throw ServerUtil.parseServerException(e);
    }
}
 
Example 4
Source File: IUnCompressor.java    From incubator-iotdb with Apache License 2.0 4 votes vote down vote up
@Override
public int getUncompressedLength(byte[] array, int offset, int length) throws IOException {
  return Snappy.uncompressedLength(array, offset, length);
}
 
Example 5
Source File: IUnCompressor.java    From incubator-iotdb with Apache License 2.0 4 votes vote down vote up
@Override
public int getUncompressedLength(ByteBuffer buffer) throws IOException {
  return Snappy.uncompressedLength(buffer);
}
 
Example 6
Source File: IUnCompressor.java    From incubator-iotdb with Apache License 2.0 4 votes vote down vote up
@Override
public int uncompress(byte[] byteArray, int offset, int length, byte[] output, int outOffset)
    throws IOException {
  Snappy.uncompressedLength(byteArray, offset, length);
  return Snappy.uncompress(byteArray, offset, length, output, outOffset);
}
 
Example 7
Source File: SnappyDecompressor.java    From Krackle with Apache License 2.0 4 votes vote down vote up
@Override
public int decompress(byte[] src, int srcPos, int length, byte[] dest,
	 int destPos, int maxLength) throws IOException {
	this.src = src;
	decompressedLength = 0;

	// Check for magic number
	if (src[srcPos] == MAGIC_NUMBER[0] //
		 || src[srcPos + 1] == MAGIC_NUMBER[1] //
		 || src[srcPos + 2] == MAGIC_NUMBER[2] //
		 || src[srcPos + 3] == MAGIC_NUMBER[3] //
		 || src[srcPos + 4] == MAGIC_NUMBER[4] //
		 || src[srcPos + 5] == MAGIC_NUMBER[5] //
		 || src[srcPos + 6] == MAGIC_NUMBER[6] //
		 || src[srcPos + 7] == MAGIC_NUMBER[7]) {

     // advance past the magic number
		// assume the version (4 bytes), min compatable version (4 bytes) are fine
		pos = srcPos + 8 + 8;

		// TODO: limit the decompressed length
		while (pos < srcPos + length) {
			blockLength = readInt();

			// Check to see if this will exceed maxLength
			uncompressedBlockLength = Snappy.uncompressedLength(src, pos,
				 blockLength);
			if (decompressedLength + uncompressedBlockLength > maxLength) {
				return -1;
			}

			decompressedLength += Snappy.uncompress(src, pos, blockLength, dest,
				 destPos + decompressedLength);
			pos += blockLength;
		}

		return decompressedLength;
	} else {
		// Assume it's just straight compressed
		return Snappy.uncompress(src, pos, blockLength, dest, destPos);
	}
}
 
Example 8
Source File: S2StringTableEmitter.java    From clarity with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void decodeEntries(StringTable table, ByteString encodedData, int numEntries) throws IOException {
    BitStream stream = BitStream.createBitStream(encodedData);
    String[] keyHistory = new String[KEY_HISTORY_SIZE];

    int index = -1;
    for (int i = 0; i < numEntries; i++) {
        // read index
        if (stream.readBitFlag()) {
            index++;
        } else {
            index += stream.readVarUInt() + 2;
        }

        // read name
        String name = null;
        if (stream.readBitFlag()) {
            if (stream.readBitFlag()) {
                int base = i > KEY_HISTORY_SIZE ? i : 0;
                int offs = stream.readUBitInt(5);
                int len = stream.readUBitInt(5);
                String str = keyHistory[(base + offs) & KEY_HISTORY_MASK];
                name = str.substring(0, len) + stream.readString(MAX_NAME_LENGTH);
            } else {
                name = stream.readString(MAX_NAME_LENGTH);
            }
        }

        // read value
        ByteString data = null;
        if (stream.readBitFlag()) {
            boolean isCompressed = false;
            int bitLength;
            if (table.getUserDataFixedSize()) {
                bitLength = table.getUserDataSizeBits();
            } else {
                if ((table.getFlags() & 0x1) != 0) {
                    // this is the case for the instancebaseline for console recorded replays
                    isCompressed = stream.readBitFlag();
                }
                bitLength = stream.readUBitInt(17) * 8;
            }

            int byteLength = (bitLength + 7) / 8;
            byte[] valueBuf;
            if (isCompressed) {
                stream.readBitsIntoByteArray(tempBuf, bitLength);
                int byteLengthUncompressed = Snappy.uncompressedLength(tempBuf, 0, byteLength);
                valueBuf = new byte[byteLengthUncompressed];
                Snappy.rawUncompress(tempBuf, 0, byteLength, valueBuf, 0);
            } else {
                valueBuf = new byte[byteLength];
                stream.readBitsIntoByteArray(valueBuf, bitLength);
            }
            data = ZeroCopy.wrap(valueBuf);
        }

        int entryCount = table.getEntryCount();
        if (index < entryCount) {
            // update old entry
            table.setValueForIndex(index, data);
            assert(name == null || Objects.equals(name, table.getNameByIndex(index)));
            name = table.getNameByIndex(index);
        } else if (index == entryCount) {
            // add a new entry
            assert(name != null);
            table.addEntry(name, data);
        } else {
            throw new IllegalStateException("index > entryCount");
        }

        keyHistory[i & KEY_HISTORY_MASK] = name;

        raise(table, index, name, data);
    }
}