Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor code simplifications #185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/com/neovisionaries/ws/client/DeflateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.neovisionaries.ws.client;


import java.util.Arrays;

/**
* Utility methods for DEFLATE (<a href="http://tools.ietf.org/html/rfc1951">RFC 1951</a>).
*/
Expand Down Expand Up @@ -83,7 +85,7 @@ public static void readDynamicTables(


private static void readCodeLengths(
ByteArray input, int bitIndex[], int[] codeLengths,
ByteArray input, int[] bitIndex, int[] codeLengths,
Huffman codeLengthHuffman) throws FormatException
{
// 3.2.7 Compression with dynamic Huffman codes (BTYPE=10)
Expand Down Expand Up @@ -130,8 +132,8 @@ private static void readCodeLengths(
default:
// Bad code length.
String message = String.format(
"[%s] Bad code length '%d' at the bit index '%d'.",
DeflateUtil.class.getSimpleName(), codeLength, bitIndex);
"[%s] Bad code length '%d' at the bit index '%s'.",
DeflateUtil.class.getSimpleName(), codeLength, Arrays.toString(bitIndex));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception at runtime possible otherwise.


throw new FormatException(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ private void validateAccept(StatusLine statusLine, Map<String, List<String>> hea
return;
}

if (expected.equals(actual) == false)
if (expected != null && !expected.equals(actual))
{
// The value of 'Sec-WebSocket-Accept' header is different from the expected one.
throw new OpeningHandshakeException(
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/neovisionaries/ws/client/Huffman.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Huffman(int[] codeLensFromSym)

// Create a table to convert code values int symbols.
int[] codeValsFromCodeLen = (int[])out[0];
int maxCodeVal = ((Integer)out[1]).intValue();
int maxCodeVal = (Integer) out[1];
mSymsFromCodeVal = createSymsFromCodeVal(codeLensFromSym, codeValsFromCodeLen, maxCodeVal);
}

Expand All @@ -70,9 +70,7 @@ private static int[] createCountsFromCodeLen(int[] codeLensFromSym, int maxCodeL

// Count the number of entries for each code length.
// This corresponds to the step 1 in 3.2.2. of RFC 1951.
for (int symbol = 0; symbol < codeLensFromSym.length; ++symbol)
{
int codeLength = codeLensFromSym[symbol];
for (int codeLength : codeLensFromSym) {
++countsFromCodeLen[codeLength];
}

Expand Down Expand Up @@ -106,7 +104,7 @@ private static int[] createMaxCodeValsFromCodeLen(int[] countsFromCodeLen, int m
}

out[0] = codeValsFromCodeLen;
out[1] = Integer.valueOf(maxCodeVal);
out[1] = maxCodeVal;

return maxCodeValsFromCodeLen;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ private List<WebSocketListener> getSynchronizedListeners()
List<WebSocketListener> copiedListeners =
new ArrayList<WebSocketListener>(mListeners.size());

for (WebSocketListener listener : mListeners)
{
copiedListeners.add(listener);
}
copiedListeners.addAll(mListeners);

// Synchronize.
mCopiedListeners = copiedListeners;
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/com/neovisionaries/ws/client/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ public static int min(int[] values)
{
int min = Integer.MAX_VALUE;

for (int i = 0; i < values.length; ++i)
{
if (values[i] < min)
{
min = values[i];
for (int value : values) {
if (value < min) {
min = value;
}
}

Expand All @@ -270,11 +268,9 @@ public static int max(int[] values)
{
int max = Integer.MIN_VALUE;

for (int i = 0; i < values.length; ++i)
{
if (max < values[i])
{
max = values[i];
for (int value : values) {
if (max < value) {
max = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ final class OkHostnameVerifier implements HostnameVerifier {
private OkHostnameVerifier() {
}

@Override
Copy link
Author

@ovalseven8 ovalseven8 Jun 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure compatibility with Java 1.5 (@Override is not allowed for implementations of interface methods).

public boolean verify(String host, SSLSession session) {
try {
Certificate[] certificates = session.getPeerCertificates();
Expand All @@ -83,8 +82,8 @@ static boolean verifyAsIpAddress(String host) {
*/
private boolean verifyIpAddress(String ipAddress, X509Certificate certificate) {
List<String> altNames = getSubjectAltNames(certificate, ALT_IPA_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
if (ipAddress.equalsIgnoreCase(altNames.get(i))) {
for (String altName : altNames) {
if (ipAddress.equalsIgnoreCase(altName)) {
return true;
}
}
Expand All @@ -98,9 +97,9 @@ private boolean verifyHostName(String hostName, X509Certificate certificate) {
hostName = hostName.toLowerCase(Locale.US);
boolean hasDns = false;
List<String> altNames = getSubjectAltNames(certificate, ALT_DNS_NAME);
for (int i = 0, size = altNames.size(); i < size; i++) {
for (String altName : altNames) {
hasDns = true;
if (verifyHostName(hostName, altNames.get(i))) {
if (verifyHostName(hostName, altName)) {
return true;
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/neovisionaries/ws/client/WebSocketFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -745,9 +745,8 @@ private void appendPayloadBinary(StringBuilder builder)
return;
}

for (int i = 0; i < mPayload.length; ++i)
{
builder.append(String.format("%02X ", (0xFF & mPayload[i])));
for (byte b : mPayload) {
builder.append(String.format("%02X ", (0xFF & b)));
}

if (mPayload.length != 0)
Expand Down Expand Up @@ -1199,15 +1198,17 @@ private static List<WebSocketFrame> split(WebSocketFrame frame, int maxPayloadSi

// Generate the first frame using the existing WebSocketFrame instance.
// Note that the reserved bit 1 and the opcode are untouched.
byte[] payload = Arrays.copyOf(originalPayload, maxPayloadSize);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays.copyOf() is not available in Java 1.5 that is targeted by this library.

byte[] payload = new byte[maxPayloadSize];
System.arraycopy(originalPayload, 0, payload, 0, Math.min(originalPayload.length, maxPayloadSize));
frame.setFin(false).setPayload(payload);
frames.add(frame);

for (int from = maxPayloadSize; from < originalPayload.length; from += maxPayloadSize)
{
// Prepare the payload of the next continuation frame.
int to = Math.min(from + maxPayloadSize, originalPayload.length);
payload = Arrays.copyOfRange(originalPayload, from, to);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrays.copyOfRange() is not available in Java 1.5 that is targeted by this library.

assert (to - from) >= 0;
System.arraycopy(originalPayload, from, payload, 0, Math.min(originalPayload.length - from, to - from));

// Create a continuation frame.
WebSocketFrame cont = WebSocketFrame.createContinuationFrame(payload);
Expand Down