Skip to content

Commit d2564df

Browse files
authored
Merge pull request TooTallNate#16 from dota17/cleanup-code
Using Maven Checkstyle Plugin and Add Github Actions to check code style
2 parents a797dfb + b8e95f9 commit d2564df

File tree

11 files changed

+186
-145
lines changed

11 files changed

+186
-145
lines changed

.github/workflows/checkstyle.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This workflow will build a Java project with Maven
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Java Code Style Check with Maven
5+
6+
on: [push, pull_request]
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Set up JDK 1.8
16+
uses: actions/setup-java@v1
17+
with:
18+
java-version: 1.8
19+
- name: Code Style Check
20+
run: mvn -B checkstyle:check --file pom.xml

checkstyle-suppressions.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
3+
<!DOCTYPE suppressions PUBLIC
4+
"-//Checkstyle//DTD SuppressionFilter Configuration 1.0//EN"
5+
"https://checkstyle.org/dtds/suppressions_1_0.dtd">
6+
7+
<suppressions>
8+
<suppress checks="Javadoc" files="."/>
9+
<suppress checks="MagicNumberCheck" files="."/>
10+
<suppress checks="PackageName" files="."/>
11+
<suppress checks="AbbreviationAsWordInName" files="."/>
12+
<suppress checks="LineLength" files="."/>
13+
<suppress checks="VariableDeclarationUsageDistance" files="."/>
14+
</suppressions>

pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<!-- Maven plugin versions -->
2121
<bnd.maven.plugin.version>4.3.1</bnd.maven.plugin.version>
22+
<maven.checkstyle.plugin.version>3.1.1</maven.checkstyle.plugin.version>
2223
<maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
2324
<maven.gpg.plugin.version>1.6</maven.gpg.plugin.version>
2425
<maven.jar.plugin.version>3.0.2</maven.jar.plugin.version>
@@ -174,6 +175,27 @@
174175
<groupId>org.apache.maven.plugins</groupId>
175176
<artifactId>maven-compiler-plugin</artifactId>
176177
</plugin>
178+
<plugin>
179+
<groupId>org.apache.maven.plugins</groupId>
180+
<artifactId>maven-checkstyle-plugin</artifactId>
181+
<version>${maven.checkstyle.plugin.version}</version>
182+
<configuration>
183+
<configLocation>google_checks.xml</configLocation>
184+
<violationSeverity>warning</violationSeverity>
185+
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
186+
<suppressionsFileExpression>checkstyle.suppressions.file</suppressionsFileExpression>
187+
<consoleOutput>true</consoleOutput>
188+
<failsOnError>false</failsOnError>
189+
</configuration>
190+
<executions>
191+
<execution>
192+
<id>check</id>
193+
<goals>
194+
<goal>check</goal>
195+
</goals>
196+
</execution>
197+
</executions>
198+
</plugin>
177199
</plugins>
178200
</build>
179201
<profiles>

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2323
* OTHER DEALINGS IN THE SOFTWARE.
2424
*/
25+
2526
package org.java_websocket;
2627

2728
import java.io.EOFException;
@@ -275,10 +276,10 @@ public int write(ByteBuffer src) throws IOException {
275276
processHandshake();
276277
return 0;
277278
}
278-
// assert ( bufferallocations > 1 ); //see #190
279-
//if( bufferallocations <= 1 ) {
280-
// createBuffers( sslEngine.getSession() );
281-
//}
279+
// assert(bufferallocations > 1); // see #190
280+
// if(bufferallocations <= 1) {
281+
// createBuffers(sslEngine.getSession());
282+
// }
282283
int num = socketChannel.write(wrap(src));
283284
if (writeEngineResult.getStatus() == SSLEngineResult.Status.CLOSED) {
284285
throw new EOFException("Connection is closed");
@@ -311,10 +312,11 @@ public int read(ByteBuffer dst) throws IOException {
311312
}
312313
}
313314
}
314-
// assert ( bufferallocations > 1 ); //see #190
315-
//if( bufferallocations <= 1 ) {
316-
// createBuffers( sslEngine.getSession() );
317-
//}
315+
// assert(bufferallocations > 1); // see #190
316+
// if (bufferallocations <= 1) {
317+
// createBuffers(sslEngine.getSession());
318+
// }
319+
318320
/* 1. When "dst" is smaller than "inData" readRemaining will fill "dst" with data decoded in a previous read call.
319321
* 2. When "inCrypt" contains more data than "inData" has remaining space, unwrap has to be called on more time(readRemaining)
320322
*/

src/main/java/org/java_websocket/SocketChannelIOHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static boolean batch(WebSocketImpl ws, ByteChannel sockchannel) throws IO
107107
}
108108

109109
if (ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft() != null
110-
&& ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER) {//
110+
&& ws.getDraft().getRole() != null && ws.getDraft().getRole() == Role.SERVER) {
111111
ws.closeConnection();
112112
}
113113
return c == null || !((WrappedByteChannel) sockchannel).isNeedWrite();

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ public void run() {
498498
ostream = socket.getOutputStream();
499499

500500
sendHandshake();
501-
} catch ( /*IOException | SecurityException | UnresolvedAddressException | InvalidHandshakeException | ClosedByInterruptException | SocketTimeoutException */Exception e) {
501+
} catch (/*IOException | SecurityException | UnresolvedAddressException | InvalidHandshakeException | ClosedByInterruptException | SocketTimeoutException */Exception e) {
502502
onWebsocketError(engine, e);
503503
engine.closeConnection(CloseFrame.NEVER_CONNECTED, e.getMessage());
504504
return;

src/main/java/org/java_websocket/drafts/Draft_6455.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,12 @@ private Framedata translateSingleFrame(ByteBuffer buffer)
522522
int maxpacketsize = buffer.remaining();
523523
int realpacketsize = 2;
524524
translateSingleFrameCheckPacketSize(maxpacketsize, realpacketsize);
525-
byte b1 = buffer.get( /*0*/);
525+
byte b1 = buffer.get(/*0*/);
526526
boolean fin = b1 >> 8 != 0;
527527
boolean rsv1 = (b1 & 0x40) != 0;
528528
boolean rsv2 = (b1 & 0x20) != 0;
529529
boolean rsv3 = (b1 & 0x10) != 0;
530-
byte b2 = buffer.get( /*1*/);
530+
byte b2 = buffer.get(/*1*/);
531531
boolean mask = (b2 & -128) != 0;
532532
int payloadlength = (byte) (b2 & ~(byte) 128);
533533
Opcode optcode = toOpcode((byte) (b1 & 15));
@@ -548,7 +548,7 @@ private Framedata translateSingleFrame(ByteBuffer buffer)
548548
byte[] maskskey = new byte[4];
549549
buffer.get(maskskey);
550550
for (int i = 0; i < payloadlength; i++) {
551-
payload.put((byte) (buffer.get( /*payloadstart + i*/) ^ maskskey[i % 4]));
551+
payload.put((byte) (buffer.get(/*payloadstart + i*/) ^ maskskey[i % 4]));
552552
}
553553
} else {
554554
payload.put(buffer.array(), buffer.position(), payload.limit());
@@ -599,15 +599,15 @@ private TranslatedPayloadMetaData translateSingleFramePayloadLength(ByteBuffer b
599599
realpacketsize += 2; // additional length bytes
600600
translateSingleFrameCheckPacketSize(maxpacketsize, realpacketsize);
601601
byte[] sizebytes = new byte[3];
602-
sizebytes[1] = buffer.get( /*1 + 1*/);
603-
sizebytes[2] = buffer.get( /*1 + 2*/);
602+
sizebytes[1] = buffer.get(/*1 + 1*/);
603+
sizebytes[2] = buffer.get(/*1 + 2*/);
604604
payloadlength = new BigInteger(sizebytes).intValue();
605605
} else {
606606
realpacketsize += 8; // additional length bytes
607607
translateSingleFrameCheckPacketSize(maxpacketsize, realpacketsize);
608608
byte[] bytes = new byte[8];
609609
for (int i = 0; i < 8; i++) {
610-
bytes[i] = buffer.get( /*1 + i*/);
610+
bytes[i] = buffer.get(/*1 + i*/);
611611
}
612612
long length = new BigInteger(bytes).longValue();
613613
translateSingleFrameCheckLengthLimit(length);
@@ -735,7 +735,8 @@ public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataExcep
735735
}
736736
}
737737

738-
while (buffer.hasRemaining()) {// Read as much as possible full frames
738+
// Read as much as possible full frames
739+
while (buffer.hasRemaining()) {
739740
buffer.mark();
740741
try {
741742
cur = translateSingleFrame(buffer);

src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ public void decodeFrame(Framedata inputFrame) throws InvalidDataException {
123123
try {
124124
decompress(inputFrame.getPayloadData().array(), output);
125125

126-
/*
127-
If a message is "first fragmented and then compressed", as this project does, then the inflater
128-
can not inflate fragments except the first one.
129-
This behavior occurs most likely because those fragments end with "final deflate blocks".
130-
We can check the getRemaining() method to see whether the data we supplied has been decompressed or not.
131-
And if not, we just reset the inflater and decompress again.
132-
Note that this behavior doesn't occur if the message is "first compressed and then fragmented".
133-
*/
126+
/*
127+
If a message is "first fragmented and then compressed", as this project does, then the inflater
128+
can not inflate fragments except the first one.
129+
This behavior occurs most likely because those fragments end with "final deflate blocks".
130+
We can check the getRemaining() method to see whether the data we supplied has been decompressed or not.
131+
And if not, we just reset the inflater and decompress again.
132+
Note that this behavior doesn't occur if the message is "first compressed and then fragmented".
133+
*/
134134
if (inflater.getRemaining() > 0) {
135135
inflater = new Inflater(true);
136136
decompress(inputFrame.getPayloadData().array(), output);
@@ -196,15 +196,15 @@ public void encodeFrame(Framedata inputFrame) {
196196
output.write(buffer, 0, bytesCompressed);
197197
}
198198

199-
byte outputBytes[] = output.toByteArray();
199+
byte[] outputBytes = output.toByteArray();
200200
int outputLength = outputBytes.length;
201201

202-
/*
203-
https://tools.ietf.org/html/rfc7692#section-7.2.1 states that if the final fragment's compressed
204-
payload ends with 0x00 0x00 0xff 0xff, they should be removed.
205-
To simulate removal, we just pass 4 bytes less to the new payload
206-
if the frame is final and outputBytes ends with 0x00 0x00 0xff 0xff.
207-
*/
202+
/*
203+
https://tools.ietf.org/html/rfc7692#section-7.2.1 states that if the final fragment's compressed
204+
payload ends with 0x00 0x00 0xff 0xff, they should be removed.
205+
To simulate removal, we just pass 4 bytes less to the new payload
206+
if the frame is final and outputBytes ends with 0x00 0x00 0xff 0xff.
207+
*/
208208
if (inputFrame.isFin()) {
209209
if (endsWithTail(outputBytes)) {
210210
outputLength -= TAIL_BYTES.length;

0 commit comments

Comments
 (0)