Skip to content

Commit

Permalink
Remove redundant casts (#13509)
Browse files Browse the repository at this point in the history
Motivation:
We should remove unnecessary redundant type casing for primitive types
of byte, int and long.

Modification:
Removed unnecessary redundant type casing

Result:
No more redundant casting
  • Loading branch information
hyperxpro committed Jul 24, 2023
1 parent 8b803ab commit c07dec1
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 55 deletions.
8 changes: 4 additions & 4 deletions buffer/src/main/java/io/netty/buffer/AbstractByteBuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -676,14 +676,14 @@ public ByteBuf setZero(int index, int length) {
// Not need to update the index as we not will use it after this.
} else if (nBytes < 4) {
for (int i = nBytes; i > 0; i --) {
_setByte(index, (byte) 0);
_setByte(index, 0);
index ++;
}
} else {
_setInt(index, 0);
index += 4;
for (int i = nBytes - 4; i > 0; i --) {
_setByte(index, (byte) 0);
_setByte(index, 0);
index ++;
}
}
Expand Down Expand Up @@ -1167,14 +1167,14 @@ public ByteBuf writeZero(int length) {
wIndex += 4;
} else if (nBytes < 4) {
for (int i = nBytes; i > 0; i --) {
_setByte(wIndex, (byte) 0);
_setByte(wIndex, 0);
wIndex++;
}
} else {
_setInt(wIndex, 0);
wIndex += 4;
for (int i = nBytes - 4; i > 0; i --) {
_setByte(wIndex, (byte) 0);
_setByte(wIndex, 0);
wIndex++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
@Sharable
public class WebSocket00FrameEncoder extends MessageToMessageEncoder<WebSocketFrame> implements WebSocketFrameEncoder {
private static final ByteBuf _0X00 = Unpooled.unreleasableBuffer(
Unpooled.directBuffer(1, 1).writeByte((byte) 0x00)).asReadOnly();
Unpooled.directBuffer(1, 1).writeByte(0x00)).asReadOnly();
private static final ByteBuf _0XFF = Unpooled.unreleasableBuffer(
Unpooled.directBuffer(1, 1).writeByte((byte) 0xFF)).asReadOnly();
private static final ByteBuf _0XFF_0X00 = Unpooled.unreleasableBuffer(
Unpooled.directBuffer(2, 2).writeByte((byte) 0xFF).writeByte((byte) 0x00)).asReadOnly();
Unpooled.directBuffer(2, 2).writeByte((byte) 0xFF).writeByte(0x00)).asReadOnly();

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ private void unmask(ByteBuf frame) {

int intMask = mask;
// Avoid sign extension on widening primitive conversion
long longMask = (long) intMask & 0xFFFFFFFFL;
long longMask = intMask & 0xFFFFFFFFL;
longMask |= longMask << 32;

for (int lim = end - 7; i < lim; i += 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object
if (srcOrder == dstOrder) {
// Use the optimized path only when byte orders match.
// Avoid sign extension on widening primitive conversion
long longMask = (long) mask & 0xFFFFFFFFL;
long longMask = mask & 0xFFFFFFFFL;
longMask |= longMask << 32;

// If the byte order of our buffers it little endian we have to bring our mask
Expand Down
44 changes: 22 additions & 22 deletions common/src/main/java/io/netty/util/internal/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ public final class StringUtil {
// if a char type is used as an index.
HEX2B = new byte[Character.MAX_VALUE + 1];
Arrays.fill(HEX2B, (byte) -1);
HEX2B['0'] = (byte) 0;
HEX2B['1'] = (byte) 1;
HEX2B['2'] = (byte) 2;
HEX2B['3'] = (byte) 3;
HEX2B['4'] = (byte) 4;
HEX2B['5'] = (byte) 5;
HEX2B['6'] = (byte) 6;
HEX2B['7'] = (byte) 7;
HEX2B['8'] = (byte) 8;
HEX2B['9'] = (byte) 9;
HEX2B['A'] = (byte) 10;
HEX2B['B'] = (byte) 11;
HEX2B['C'] = (byte) 12;
HEX2B['D'] = (byte) 13;
HEX2B['E'] = (byte) 14;
HEX2B['F'] = (byte) 15;
HEX2B['a'] = (byte) 10;
HEX2B['b'] = (byte) 11;
HEX2B['c'] = (byte) 12;
HEX2B['d'] = (byte) 13;
HEX2B['e'] = (byte) 14;
HEX2B['f'] = (byte) 15;
HEX2B['0'] = 0;
HEX2B['1'] = 1;
HEX2B['2'] = 2;
HEX2B['3'] = 3;
HEX2B['4'] = 4;
HEX2B['5'] = 5;
HEX2B['6'] = 6;
HEX2B['7'] = 7;
HEX2B['8'] = 8;
HEX2B['9'] = 9;
HEX2B['A'] = 10;
HEX2B['B'] = 11;
HEX2B['C'] = 12;
HEX2B['D'] = 13;
HEX2B['E'] = 14;
HEX2B['F'] = 15;
HEX2B['a'] = 10;
HEX2B['b'] = 11;
HEX2B['c'] = 12;
HEX2B['d'] = 13;
HEX2B['e'] = 14;
HEX2B['f'] = 15;
}

private StringUtil() {
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/java/io/netty/example/ocsp/OcspUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static URI ocspUri(X509Certificate certificate) throws IOException {
}

byte[] encoded = taggedObject.getEncoded();
int length = (int) encoded[1] & 0xFF;
int length = encoded[1] & 0xFF;
String uri = new String(encoded, 2, length, CharsetUtil.UTF_8);
return URI.create(uri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,32 +140,32 @@ private static int decodeHexNibble(final char c) {
static {
HEX2B = new byte['f' + 1];
Arrays.fill(HEX2B, (byte) -1);
HEX2B['0'] = (byte) 0;
HEX2B['1'] = (byte) 1;
HEX2B['2'] = (byte) 2;
HEX2B['3'] = (byte) 3;
HEX2B['4'] = (byte) 4;
HEX2B['5'] = (byte) 5;
HEX2B['6'] = (byte) 6;
HEX2B['7'] = (byte) 7;
HEX2B['8'] = (byte) 8;
HEX2B['9'] = (byte) 9;
HEX2B['A'] = (byte) 10;
HEX2B['B'] = (byte) 11;
HEX2B['C'] = (byte) 12;
HEX2B['D'] = (byte) 13;
HEX2B['E'] = (byte) 14;
HEX2B['F'] = (byte) 15;
HEX2B['a'] = (byte) 10;
HEX2B['b'] = (byte) 11;
HEX2B['c'] = (byte) 12;
HEX2B['d'] = (byte) 13;
HEX2B['e'] = (byte) 14;
HEX2B['f'] = (byte) 15;
HEX2B['0'] = 0;
HEX2B['1'] = 1;
HEX2B['2'] = 2;
HEX2B['3'] = 3;
HEX2B['4'] = 4;
HEX2B['5'] = 5;
HEX2B['6'] = 6;
HEX2B['7'] = 7;
HEX2B['8'] = 8;
HEX2B['9'] = 9;
HEX2B['A'] = 10;
HEX2B['B'] = 11;
HEX2B['C'] = 12;
HEX2B['D'] = 13;
HEX2B['E'] = 14;
HEX2B['F'] = 15;
HEX2B['a'] = 10;
HEX2B['b'] = 11;
HEX2B['c'] = 12;
HEX2B['d'] = 13;
HEX2B['e'] = 14;
HEX2B['f'] = 15;
}

private static int decodeHexNibbleWithCheck(final char c) {
if ((int) c >= HEX2B.length) {
if (c >= HEX2B.length) {
return -1;
}
if (PlatformDependent.hasUnsafe()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int fd(int index) {

private int getInt(int index, int offset) {
if (PlatformDependent.hasUnsafe()) {
long n = (long) index * (long) EPOLL_EVENT_SIZE;
long n = (long) index * EPOLL_EVENT_SIZE;
return PlatformDependent.getInt(memoryAddress + n + offset);
}
return memory.getInt(index * EPOLL_EVENT_SIZE + offset);
Expand Down

0 comments on commit c07dec1

Please sign in to comment.