Skip to content

Commit

Permalink
Address some SonarCloud code smells in new network classes
Browse files Browse the repository at this point in the history
  • Loading branch information
willkroboth committed Jul 21, 2023
1 parent 021755d commit a264fec
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class FriendlyByteBuffer {
private int readIndex = 0;
private int writeIndex = 0;
private final List<Byte> bytes = new ArrayList<>();
private final List<Byte> byteBuffer = new ArrayList<>();

/**
* Creates a new {@link FriendlyByteBuffer} with no bytes, ready to be written to.
Expand Down Expand Up @@ -157,17 +157,17 @@ public boolean isWriteIndexOutOfBounds(int writeIndex) {
* {@link IndexOutOfBoundsException} when accessing bytes at the write index.
*/
private void padBytesWithZerosUntilWriteIndex() {
if(this.writeIndex > this.bytes.size()) {
List<Byte> paddingZeros = Collections.nCopies(this.writeIndex - this.bytes.size(), (byte) 0);
this.bytes.addAll(paddingZeros);
if(this.writeIndex > this.byteBuffer.size()) {
List<Byte> paddingZeros = Collections.nCopies(this.writeIndex - this.byteBuffer.size(), (byte) 0);
this.byteBuffer.addAll(paddingZeros);
}
}

private IllegalStateException unexpectedByteJustRead(String message) {
return new IllegalStateException(
message + "\n" +
"At position " + (this.readIndex - 1) + "\n" +
this.bytes.subList(0, this.readIndex) + " <-- HERE " + this.bytes.subList(this.readIndex, this.writeIndex)
this.byteBuffer.subList(0, this.readIndex) + " <-- HERE " + this.byteBuffer.subList(this.readIndex, this.writeIndex)
);
}

Expand Down Expand Up @@ -239,7 +239,7 @@ public void resetIndices() {
public byte[] toByteArray() throws IllegalStateException {
this.checkWriteIndexIsInBounds();

return Bytes.toArray(this.bytes.subList(0, this.writeIndex));
return Bytes.toArray(this.byteBuffer.subList(0, this.writeIndex));
}

/**
Expand All @@ -252,7 +252,7 @@ public byte[] getRemainingBytes() throws IllegalStateException {
this.checkWriteIndexIsInBounds();
this.checkReadIndexIsInBounds();

return Bytes.toArray(this.bytes.subList(this.readIndex, this.writeIndex));
return Bytes.toArray(this.byteBuffer.subList(this.readIndex, this.writeIndex));
}

/**
Expand Down Expand Up @@ -282,10 +282,10 @@ public int countReadableBytes() {
public void writeByte(byte b) throws IllegalStateException {
this.checkWriteIndexIsInBounds();

if(this.writeIndex == this.bytes.size()) {
this.bytes.add(b);
if(this.writeIndex == this.byteBuffer.size()) {
this.byteBuffer.add(b);
} else {
this.bytes.set(this.writeIndex, b);
this.byteBuffer.set(this.writeIndex, b);
}

this.writeIndex++;
Expand All @@ -309,7 +309,7 @@ public void writeByte(int i) throws IllegalStateException {
*/
public byte readByte() throws IllegalStateException{
this.checkReadIndexIsInBounds();
byte b = this.bytes.get(this.readIndex);
byte b = this.byteBuffer.get(this.readIndex);

this.readIndex++;
return b;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ClientToServerUpdateRequirementsPacketTests extends NetworkTestBase {
class ClientToServerUpdateRequirementsPacketTests extends NetworkTestBase {

/*********
* Setup *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class CommandAPIMessengerTests extends NetworkTestBase {
class CommandAPIMessengerTests extends NetworkTestBase {

/*********
* Setup *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class FriendlyByteBufferTests extends TestBase {
class FriendlyByteBufferTests extends TestBase {

/*********
* Setup *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import static org.junit.jupiter.api.Assertions.*;

public class ProtocolVersionTooOldPacketTests extends NetworkTestBase {
class ProtocolVersionTooOldPacketTests extends NetworkTestBase {

/*********
* Setup *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
public class SetVersionPacketTests extends NetworkTestBase {
class SetVersionPacketTests extends NetworkTestBase {

/*********
* Setup *
Expand Down

0 comments on commit a264fec

Please sign in to comment.