Use ByteBuf.*LE methods for write and read LE
Motivation: We recently added methods to ByteBuf to directly write and read LE values. We should use these in the Snappy implementation and so reduce duplication. Modifications: Replace manually swapping of values with LE write and read methods of ByteBuf. Result: Cleaner code with less duplication.
This commit is contained in:
parent
7abacd3edd
commit
7c29c83e10
@ -16,7 +16,6 @@
|
|||||||
package io.netty.handler.codec.compression;
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uncompresses an input {@link ByteBuf} encoded with Snappy compression into an
|
* Uncompresses an input {@link ByteBuf} encoded with Snappy compression into an
|
||||||
@ -410,19 +409,19 @@ class Snappy {
|
|||||||
if (in.readableBytes() < 2) {
|
if (in.readableBytes() < 2) {
|
||||||
return NOT_ENOUGH_INPUT;
|
return NOT_ENOUGH_INPUT;
|
||||||
}
|
}
|
||||||
length = ByteBufUtil.swapShort(in.readShort());
|
length = in.readShortLE();
|
||||||
break;
|
break;
|
||||||
case 62:
|
case 62:
|
||||||
if (in.readableBytes() < 3) {
|
if (in.readableBytes() < 3) {
|
||||||
return NOT_ENOUGH_INPUT;
|
return NOT_ENOUGH_INPUT;
|
||||||
}
|
}
|
||||||
length = ByteBufUtil.swapMedium(in.readUnsignedMedium());
|
length = in.readUnsignedMediumLE();
|
||||||
break;
|
break;
|
||||||
case 64:
|
case 64:
|
||||||
if (in.readableBytes() < 4) {
|
if (in.readableBytes() < 4) {
|
||||||
return NOT_ENOUGH_INPUT;
|
return NOT_ENOUGH_INPUT;
|
||||||
}
|
}
|
||||||
length = ByteBufUtil.swapInt(in.readInt());
|
length = in.readIntLE();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
length = tag >> 2 & 0x3F;
|
length = tag >> 2 & 0x3F;
|
||||||
@ -502,7 +501,7 @@ class Snappy {
|
|||||||
|
|
||||||
int initialIndex = out.writerIndex();
|
int initialIndex = out.writerIndex();
|
||||||
int length = 1 + (tag >> 2 & 0x03f);
|
int length = 1 + (tag >> 2 & 0x03f);
|
||||||
int offset = ByteBufUtil.swapShort(in.readShort());
|
int offset = in.readShortLE();
|
||||||
|
|
||||||
validateOffset(offset, writtenSoFar);
|
validateOffset(offset, writtenSoFar);
|
||||||
|
|
||||||
@ -546,7 +545,7 @@ class Snappy {
|
|||||||
|
|
||||||
int initialIndex = out.writerIndex();
|
int initialIndex = out.writerIndex();
|
||||||
int length = 1 + (tag >> 2 & 0x03F);
|
int length = 1 + (tag >> 2 & 0x03F);
|
||||||
int offset = ByteBufUtil.swapInt(in.readInt());
|
int offset = in.readIntLE();
|
||||||
|
|
||||||
validateOffset(offset, writtenSoFar);
|
validateOffset(offset, writtenSoFar);
|
||||||
|
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
package io.netty.handler.codec.compression;
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufUtil;
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||||
|
|
||||||
@ -95,7 +94,7 @@ public class SnappyFrameDecoder extends ByteToMessageDecoder {
|
|||||||
|
|
||||||
final int chunkTypeVal = in.getUnsignedByte(idx);
|
final int chunkTypeVal = in.getUnsignedByte(idx);
|
||||||
final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
|
final ChunkType chunkType = mapChunkType((byte) chunkTypeVal);
|
||||||
final int chunkLength = ByteBufUtil.swapMedium(in.getUnsignedMedium(idx + 1));
|
final int chunkLength = in.getUnsignedMediumLE(idx + 1);
|
||||||
|
|
||||||
switch (chunkType) {
|
switch (chunkType) {
|
||||||
case STREAM_IDENTIFIER:
|
case STREAM_IDENTIFIER:
|
||||||
@ -149,7 +148,7 @@ public class SnappyFrameDecoder extends ByteToMessageDecoder {
|
|||||||
|
|
||||||
in.skipBytes(4);
|
in.skipBytes(4);
|
||||||
if (validateChecksums) {
|
if (validateChecksums) {
|
||||||
int checksum = ByteBufUtil.swapInt(in.readInt());
|
int checksum = in.readIntLE();
|
||||||
validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4);
|
validateChecksum(checksum, in, in.readerIndex(), chunkLength - 4);
|
||||||
} else {
|
} else {
|
||||||
in.skipBytes(4);
|
in.skipBytes(4);
|
||||||
@ -166,7 +165,7 @@ public class SnappyFrameDecoder extends ByteToMessageDecoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
in.skipBytes(4);
|
in.skipBytes(4);
|
||||||
int checksum = ByteBufUtil.swapInt(in.readInt());
|
int checksum = in.readIntLE();
|
||||||
ByteBuf uncompressed = ctx.alloc().buffer(0);
|
ByteBuf uncompressed = ctx.alloc().buffer(0);
|
||||||
if (validateChecksums) {
|
if (validateChecksums) {
|
||||||
int oldWriterIndex = in.writerIndex();
|
int oldWriterIndex = in.writerIndex();
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
package io.netty.handler.codec.compression;
|
package io.netty.handler.codec.compression;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.buffer.ByteBufUtil;
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.handler.codec.MessageToByteEncoder;
|
import io.netty.handler.codec.MessageToByteEncoder;
|
||||||
|
|
||||||
@ -99,7 +98,7 @@ public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {
|
|||||||
if (chunkLength >>> 24 != 0) {
|
if (chunkLength >>> 24 != 0) {
|
||||||
throw new CompressionException("compressed data too large: " + chunkLength);
|
throw new CompressionException("compressed data too large: " + chunkLength);
|
||||||
}
|
}
|
||||||
out.setMedium(lengthIdx, ByteBufUtil.swapMedium(chunkLength));
|
out.setMediumLE(lengthIdx, chunkLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,7 +108,7 @@ public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {
|
|||||||
* @param chunkLength The length to write
|
* @param chunkLength The length to write
|
||||||
*/
|
*/
|
||||||
private static void writeChunkLength(ByteBuf out, int chunkLength) {
|
private static void writeChunkLength(ByteBuf out, int chunkLength) {
|
||||||
out.writeMedium(ByteBufUtil.swapMedium(chunkLength));
|
out.writeMediumLE(chunkLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,6 +118,6 @@ public class SnappyFrameEncoder extends MessageToByteEncoder<ByteBuf> {
|
|||||||
* @param out The output buffer to write the checksum to
|
* @param out The output buffer to write the checksum to
|
||||||
*/
|
*/
|
||||||
private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) {
|
private static void calculateAndWriteChecksum(ByteBuf slice, ByteBuf out) {
|
||||||
out.writeInt(ByteBufUtil.swapInt(calculateChecksum(slice)));
|
out.writeIntLE(calculateChecksum(slice));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user