本文整理汇总了Java中org.xerial.snappy.Snappy类的典型用法代码示例。如果您正苦于以下问题:Java Snappy类的具体用法?Java Snappy怎么用?Java Snappy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Snappy类属于org.xerial.snappy包,在下文中一共展示了Snappy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCompressionSnappyRaw
import org.xerial.snappy.Snappy; //导入依赖的package包/类
/**
* Test compression snappy raw.
*
* @throws IOException Signals that an I/O exception has occurred.
*/
public void testCompressionSnappyRaw() throws IOException
{
ByteBuffer src = ByteBuffer.allocateDirect(1024);
ByteBuffer dst = ByteBuffer.allocateDirect(1024);
String value = "0000000000000000000000000000000000000000000000000000000000000000000000000000000"+
"0000000000000000000000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000000000000000000000000000000000000000000000000000000000000000000";
src.put(value.getBytes());
src.flip();
int len = Snappy.compress(src, dst);
LOG.info("Compressed size ="+len+" dst pos ="+dst.position()+" dst limit="+dst.limit());
len = Snappy.uncompress(dst, src);
LOG.info("Uncompressed size="+len);
LOG.info("Original size="+value.length());
}
示例2: compressBufferForMessaging
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public static ByteBuffer compressBufferForMessaging(ByteBuffer buffer) throws IOException {
assert(buffer.isDirect());
int lim = buffer.limit();
int pos = buffer.position();
int rem = buffer.remaining();
IOBuffers buffers = getBuffersForCompression(buffer.remaining(), true);
ByteBuffer output = buffers.output;
final int compressedSize = Snappy.compress(buffer, output);
ByteBuffer result = ByteBuffer.wrap(new byte[compressedSize + 4]);
result.putInt(compressedSize);
result.put(output);
result.flip();
return result;
}
示例3: decompressBuffer
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public static byte[] decompressBuffer(final ByteBuffer compressed) throws IOException {
assert(compressed.isDirect());
IOBuffers buffers = m_buffers.get();
ByteBuffer input = buffers.input;
ByteBuffer output = buffers.output;
final int uncompressedLength = Snappy.uncompressedLength(input);
if (output.capacity() < uncompressedLength) {
output = ByteBuffer.allocateDirect(Math.max(output.capacity() * 2, uncompressedLength));
buffers = new IOBuffers(input, output);
m_buffers.set(buffers);
}
output.clear();
final int actualUncompressedLength = Snappy.uncompress(input, output);
assert(uncompressedLength == actualUncompressedLength);
byte result[] = new byte[actualUncompressedLength];
output.get(result);
return result;
}
示例4: compress
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public static byte[] compress(byte[] uncompressData) {
try {
switch (CMP) {
case SNAPPY:
return Snappy.compress(uncompressData);
case GZIP:
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
GZIPOutputStream gzout = new GZIPOutputStream(out);
gzout.write(uncompressData, 0, uncompressData.length);
gzout.close();
return out.toByteArray();
default:
throw new RuntimeException("Should not happen!");
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
示例5: compress
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public Frame compress(Frame frame) throws IOException
{
byte[] input = CBUtil.readRawBytes(frame.body);
ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.maxCompressedLength(input.length));
try
{
int written = Snappy.compress(input, 0, input.length, output.array(), output.arrayOffset());
output.writerIndex(written);
}
catch (final Throwable e)
{
output.release();
throw e;
}
finally
{
//release the old frame
frame.release();
}
return frame.with(output);
}
示例6: decompressBuffer
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public static byte[] decompressBuffer(final ByteBuffer compressed) throws IOException {
assert(compressed.isDirect());
IOBuffers buffers = m_buffers.get();
BBContainer output = buffers.output;
final int uncompressedLength = Snappy.uncompressedLength(compressed);
final int outputCapacity = buffers.output.b().capacity();
if (outputCapacity < uncompressedLength) {
buffers.output.discard();
output = DBBPool.allocateDirect(Math.max(outputCapacity * 2, uncompressedLength));
buffers = new IOBuffers(buffers.input, output);
m_buffers.set(buffers);
}
output.b().clear();
final int actualUncompressedLength = Snappy.uncompress(compressed, output.b());
assert(uncompressedLength == actualUncompressedLength);
byte result[] = new byte[actualUncompressedLength];
output.b().get(result);
return result;
}
示例7: insert
import org.xerial.snappy.Snappy; //导入依赖的package包/类
/**
* Insert a tuple into the page, appending its serialized length to the file beforehand
*/
public void insert(MapTuple tuple){
try {
// Generate serial data and length
byte[] serialData = Snappy.compress(Utils.serialize(tuple));
byte[] serialLen = ByteBuffer.allocate(4).putInt(serialData.length).array();
// Append compressed objects to the file
FileOutputStream fos = new FileOutputStream(_tupleFileName, true);
fos.write(serialLen);
fos.write(serialData);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例8: snappyCompress
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public void snappyCompress() {
byte[] raw = gen_.generate(4 * 1024);
byte[] compressedOutput = new byte[Snappy
.maxCompressedLength(raw.length)];
long produced = 0;
// attempt to compress the block
while (bytes_ < 1024 * 1048576) { // Compress 1G
try {
int compressedSize = Snappy.compress(raw, 0, raw.length,
compressedOutput, 0);
bytes_ += raw.length;
produced += compressedSize;
} catch (IOException ignored) {
throw Throwables.propagate(ignored);
}
finishedSingleOp();
}
message_ = String.format("(output: %.1f%%)", (produced * 100.0)
/ bytes_);
}
示例9: snappyUncompressArray
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public void snappyUncompressArray() {
int inputSize = 4 * 1024;
byte[] compressedOutput = new byte[Snappy
.maxCompressedLength(inputSize)];
byte raw[] = gen_.generate(inputSize);
int compressedLength;
try {
compressedLength = Snappy.compress(raw, 0, raw.length,
compressedOutput, 0);
} catch (IOException e) {
throw Throwables.propagate(e);
}
// attempt to uncompress the block
while (bytes_ < 5L * 1024 * 1048576) { // Compress 1G
try {
Snappy.uncompress(compressedOutput, 0, compressedLength, raw, 0);
bytes_ += inputSize;
} catch (IOException ignored) {
throw Throwables.propagate(ignored);
}
finishedSingleOp();
}
}
示例10: put
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public boolean put(byte[] key, byte[] value, long timeToLive, long createdTime, boolean isDelete) throws IOException {
ensureNotClosed();
Preconditions.checkArgument(key != null && key.length > 0, "Key is empty");
Preconditions.checkArgument(value != null && value.length > 0, "value is empty");
IMapEntry mapEntry = null;
if (isDelete) {
// make a tombstone
mapEntry = this.appendTombstone(key);
} else {
mapEntry = this.compressionEnabled ?
this.appendNewCompressed(key, Snappy.compress(value), timeToLive, createdTime) : this.appendNew(key, value, timeToLive, createdTime);
}
if (mapEntry == null) { // no space
return false;
}
return true;
}
示例11: put
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public boolean put(byte[] key, byte[] value, long timeToLive, long createdTime, boolean isDelete) throws IOException {
Preconditions.checkArgument(key != null && key.length > 0, "Key is empty");
Preconditions.checkArgument(value != null && value.length > 0, "value is empty");
IMapEntry mapEntry = null;
if (isDelete) {
// make a tombstone
mapEntry = this.appendTombstone(key);
} else {
mapEntry = this.compressionEnabled ?
this.appendNewCompressed(key, Snappy.compress(value), timeToLive, createdTime) : this.appendNew(key, value, timeToLive, createdTime);
}
if (mapEntry == null) { // no space
return false;
}
return true;
}
示例12: writeCompressed
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public static int writeCompressed(DataOutput out, byte[] bytes, int length) throws IOException {
if (length > COMPRESSION_THRESHOLD && ourCanUseSnappy) {
SoftReference<byte[]> reference = spareBufferLocal.get();
byte[] compressedOutputBuffer = reference != null ? reference.get():null;
int maxCompressedSize = 32 + length + length / 6; // snappy.cc#MaxCompressedLength
if (compressedOutputBuffer == null || compressedOutputBuffer.length < maxCompressedSize) {
compressedOutputBuffer = new byte[maxCompressedSize];
spareBufferLocal.set(new SoftReference<byte[]>(compressedOutputBuffer));
}
int compressedSize = Snappy.rawCompress(bytes, 0, length, compressedOutputBuffer, 0);
DataInputOutputUtil.writeINT(out, -compressedSize);
out.write(compressedOutputBuffer, 0, compressedSize);
return compressedSize;
} else {
DataInputOutputUtil.writeINT(out, length);
out.write(bytes, 0, length);
return length;
}
}
示例13: writeListOfValues
import org.xerial.snappy.Snappy; //导入依赖的package包/类
private void writeListOfValues(List<Object> currentBatchValues) throws IOException {
ByteArrayOutputStream bos;
DataOutputStream dos;
//write values
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
for (Object value : currentBatchValues) {
byte[] objectAsBytes = SerializationUtils.objectToBytesCheckForNull(value, dataInterface.getObjectClass());
if (SerializationUtils.getWidth(dataInterface.getObjectClass()) == -1) {
dos.writeInt(objectAsBytes.length);
}
dos.write(objectAsBytes);
}
dos.close();
byte[] origValues = bos.toByteArray();
byte[] compressedValues = Snappy.compress(origValues);
// Log.i("Compressed values from " + origValues.length + " to " + compressedValues.length);
connection.writeByteArray(compressedValues);
}
示例14: compress
import org.xerial.snappy.Snappy; //导入依赖的package包/类
@Override
public int compress(byte[] src, int srcPos, int length, byte[] dest,
int destPos) throws IOException {
System.arraycopy(header, 0, dest, destPos, headerLength);
// Compressed size cannot be greater than what we have available
maxCompressedSize = dest.length - destPos - headerLength - 4;
if (Snappy.maxCompressedLength(length) > maxCompressedSize) {
return -1;
}
compressedLength = Snappy.compress(src, srcPos, length, dest, destPos
+ headerLength + 4);
writeInt(compressedLength, dest, destPos + headerLength);
return headerLength + 4 + compressedLength;
}
示例15: objectToBytes
import org.xerial.snappy.Snappy; //导入依赖的package包/类
public byte[] objectToBytes(Object o) throws IOException {
try {
byte[] bytes = MarshallerUtil.obj2bytes(marshaller, o);
if( DEBUG )
log.debug( StackTraceUtil.getStackTrace(Thread.currentThread().getStackTrace()) );
if( USE_SNAPPY_COMPRESSION ) {
byte[] compressBuf = Snappy.compress(bytes);
if( DEBUG ) {
log.debug("KhanGridMarshaller/SIZE=" + bytes.length + "/COMPRESS=" + compressBuf.length);
}
return compressBuf;
} else {
if( DEBUG ) {
log.debug("KhanGridMarshaller/SIZE=" + bytes.length );
}
return bytes;
}
} catch (Exception e) {
throw new IOException("Exception");
}
}