Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Jungmann <lukas.jungmann@oracle.com>
  • Loading branch information
lukasj committed May 5, 2023
1 parent d656ae8 commit 4d06743
Show file tree
Hide file tree
Showing 120 changed files with 340 additions and 503 deletions.
37 changes: 8 additions & 29 deletions core/src/main/java/org/eclipse/angus/mail/auth/Ntlm.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -103,9 +104,7 @@ private void init0() {
fac = SecretKeyFactory.getInstance("DES");
cipher = Cipher.getInstance("DES/ECB/NoPadding");
md4 = new MD4();
} catch (NoSuchPaddingException e) {
assert false;
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
assert false;
}
}
Expand Down Expand Up @@ -191,11 +190,7 @@ public String generateType1Msg(int flags, boolean v2) {
logger.fine("type 1 message: " + toHex(msg));

String result = null;
try {
result = new String(Base64.getEncoder().encode(msg), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
assert false;
}
result = new String(Base64.getEncoder().encode(msg), StandardCharsets.ISO_8859_1);
return result;
}

Expand Down Expand Up @@ -232,13 +227,11 @@ private byte[] hmacMD5(byte[] key, byte[] text) {
}
try {
byte[] nk = new byte[16];
System.arraycopy(key, 0, nk, 0, key.length > 16 ? 16 : key.length);
System.arraycopy(key, 0, nk, 0, Math.min(key.length, 16));
SecretKeySpec skey = new SecretKeySpec(nk, "HmacMD5");
hmac.init(skey);
return hmac.doFinal(text);
} catch (InvalidKeyException ex) {
assert false;
} catch (RuntimeException e) {
} catch (InvalidKeyException | RuntimeException ex) {
assert false;
}
return null;
Expand All @@ -247,12 +240,7 @@ private byte[] hmacMD5(byte[] key, byte[] text) {
private byte[] calcLMHash() throws GeneralSecurityException {
byte[] magic = {0x4b, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25};
byte[] pwb = null;
try {
pwb = password.toUpperCase(Locale.ENGLISH).getBytes("iso-8859-1");
} catch (UnsupportedEncodingException ex) {
// should never happen
assert false;
}
pwb = password.toUpperCase(Locale.ENGLISH).getBytes(StandardCharsets.ISO_8859_1);
byte[] pwb1 = new byte[14];
int len = password.length();
if (len > 14)
Expand Down Expand Up @@ -345,12 +333,7 @@ public String generateType3Msg(String type2msg) {
/* First decode the type2 message to get the server challenge */
/* challenge is located at type2[24] for 8 bytes */
byte[] type2 = null;
try {
type2 = Base64.getDecoder().decode(type2msg.getBytes("us-ascii"));
} catch (UnsupportedEncodingException ex) {
// should never happen
assert false;
}
type2 = Base64.getDecoder().decode(type2msg.getBytes(StandardCharsets.US_ASCII));
if (logger.isLoggable(Level.FINE))
logger.fine("type 2 message: " + toHex(type2));

Expand Down Expand Up @@ -447,11 +430,7 @@ public String generateType3Msg(String type2msg) {
logger.fine("type 3 message: " + toHex(msg));

String result = null;
try {
result = new String(Base64.getEncoder().encode(msg), "iso-8859-1");
} catch (UnsupportedEncodingException e) {
assert false;
}
result = new String(Base64.getEncoder().encode(msg), StandardCharsets.ISO_8859_1);
return result;

} catch (GeneralSecurityException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.eclipse.angus.mail.auth;

import org.eclipse.angus.mail.util.ASCIIUtility;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
Expand All @@ -26,7 +24,7 @@
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
Expand Down Expand Up @@ -83,12 +81,7 @@ public byte[] evaluateChallenge(byte[] challenge) throws SaslException {
pcb.clearPassword();
String resp = "user=" + user + "\001auth=Bearer " + token + "\001\001";
byte[] response;
try {
response = resp.getBytes("utf-8");
} catch (UnsupportedEncodingException ex) {
// fall back to ASCII
response = ASCIIUtility.getBytes(resp);
}
response = resp.getBytes(StandardCharsets.UTF_8);
complete = true;
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,14 @@ public Object getContent(DataSource ds) throws IOException {
InputStream is = ds.getInputStream();
int pos = 0;
int count;
byte buf[] = new byte[1024];
byte[] buf = new byte[1024];

while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
pos += count;
if (pos >= buf.length) {
int size = buf.length;
if (size < 256 * 1024)
size += size;
else
size += 256 * 1024;
byte tbuf[] = new byte[size];
size += Math.min(size, 256 * 1024);
byte[] tbuf = new byte[size];
System.arraycopy(buf, 0, tbuf, 0, pos);
buf = tbuf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,14 @@ public Object getContent(DataSource ds) throws IOException {
try {
int pos = 0;
int count;
char buf[] = new char[1024];
char[] buf = new char[1024];

while ((count = is.read(buf, pos, buf.length - pos)) != -1) {
pos += count;
if (pos >= buf.length) {
int size = buf.length;
if (size < 256 * 1024)
size += size;
else
size += 256 * 1024;
char tbuf[] = new char[size];
size += Math.min(size, 256 * 1024);
char[] tbuf = new char[size];
System.arraycopy(buf, 0, tbuf, 0, pos);
buf = tbuf;
}
Expand Down
12 changes: 2 additions & 10 deletions core/src/main/java/org/eclipse/angus/mail/handlers/text_xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ public void writeTo(Object obj, String mimeType, OutputStream os)
} else {
transformer.transform((Source) obj, result);
}
} catch (TransformerException ex) {
IOException ioex = new IOException(
"Unable to run the JAXP transformer on a stream "
+ ex.getMessage());
ioex.initCause(ex);
throw ioex;
} catch (RuntimeException ex) {
} catch (TransformerException | RuntimeException ex) {
IOException ioex = new IOException(
"Unable to run the JAXP transformer on a stream "
+ ex.getMessage());
Expand All @@ -119,9 +113,7 @@ private boolean isXmlType(String type) {
return ct.getSubType().equals("xml") &&
(ct.getPrimaryType().equals("text") ||
ct.getPrimaryType().equals("application"));
} catch (ParseException ex) {
return false;
} catch (RuntimeException ex) {
} catch (ParseException | RuntimeException ex) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public int available() throws IOException {
* This character array provides the character to value map
* based on RFC1521.
*/
private final static char pem_array[] = {
private final static char[] pem_array = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
Expand All @@ -199,7 +199,7 @@ public int available() throws IOException {
'4', '5', '6', '7', '8', '9', '+', '/' // 7
};

private final static byte pem_convert_array[] = new byte[256];
private final static byte[] pem_convert_array = new byte[256];

static {
for (int i = 0; i < 255; i++)
Expand Down Expand Up @@ -369,7 +369,7 @@ private String recentChars() {
// reach into the input buffer and extract up to 10
// recent characters, to help in debugging.
String errstr = "";
int nc = input_pos > 10 ? 10 : input_pos;
int nc = Math.min(input_pos, 10);
if (nc > 0) {
errstr += ", the " + nc +
" most recent characters were: \"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public synchronized void close() throws IOException {
/**
* This array maps the characters to their 6 bit values
*/
private final static char pem_array[] = {
private final static char[] pem_array = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public void write(int b) throws IOException {
}

@Override
public void write(byte b[]) throws IOException {
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}

@Override
public void write(byte b[], int off, int len) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
int start = off;

len += off;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public void write(int b) throws IOException {
}

@Override
public void write(byte b[]) throws IOException {
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}

@Override
public void write(byte b[], int off, int len) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
int start = off;

if (!logger.isLoggable(level))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void log(Level level, String msg) {
*/
public void log(Level level, String msg, Object param1) {
if (debug) {
msg = MessageFormat.format(msg, new Object[]{param1});
msg = MessageFormat.format(msg, param1);
debugOut(msg);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ private String packageOf(Class<?> clazz) {
*/
private StackTraceElement inferCaller() {
// Get the stack trace.
StackTraceElement stack[] = (new Throwable()).getStackTrace();
StackTraceElement[] stack = (new Throwable()).getStackTrace();
// First, search back to a method in the Logger class.
int ix = 0;
while (ix < stack.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void outputCRLF() throws IOException {
}

// The encoding table
private final static char hex[] = {
private final static char[] hex = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ private static SocketFactory getSocketFactory(String sfClass)
if (clsSockFact == null)
clsSockFact = Class.forName(sfClass);
// get & invoke the getDefault() method
Method mthGetDefault = clsSockFact.getMethod("getDefault",
new Class<?>[]{});
Method mthGetDefault = clsSockFact.getMethod("getDefault"
);
SocketFactory sf = (SocketFactory)
mthGetDefault.invoke(new Object(), new Object[]{});
return sf;
Expand Down Expand Up @@ -608,7 +608,7 @@ private static void configureSSLSocket(Socket socket, String host,
eprots.add(prots[i]);
}
sslsocket.setEnabledProtocols(
eprots.toArray(new String[eprots.size()]));
eprots.toArray(new String[0]));
}
String ciphers = props.getProperty(prefix + ".ssl.ciphersuites", null);
if (ciphers != null)
Expand Down Expand Up @@ -723,17 +723,17 @@ private static boolean matchCert(String server, X509Certificate cert) {
// HostnameChecker.TYPE_LDAP == 2
// LDAP requires the same regex handling as we need
Method getInstance = hnc.getMethod("getInstance",
new Class<?>[]{byte.class});
byte.class);
Object hostnameChecker = getInstance.invoke(new Object(),
new Object[]{Byte.valueOf((byte) 2)});
Byte.valueOf((byte) 2));

// invoke hostnameChecker.match( server, cert)
if (logger.isLoggable(Level.FINER))
logger.finer("using sun.security.util.HostnameChecker");
Method match = hnc.getMethod("match",
new Class<?>[]{String.class, X509Certificate.class});
String.class, X509Certificate.class);
try {
match.invoke(hostnameChecker, new Object[]{server, cert});
match.invoke(hostnameChecker, server, cert);
return true;
} catch (InvocationTargetException cex) {
logger.log(Level.FINER, "HostnameChecker FAIL", cex);
Expand Down Expand Up @@ -908,7 +908,7 @@ private static String[] stringArray(String s) {
List<String> tokens = new ArrayList<>();
while (st.hasMoreTokens())
tokens.add(st.nextToken());
return tokens.toArray(new String[tokens.size()]);
return tokens.toArray(new String[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public int read() throws IOException {
* trace mode is <code>true</code>
*/
@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException {
int count = in.read(b, off, len);
if (trace && count != -1) {
if (quote) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void write(int b) throws IOException {
* @exception IOException for I/O errors
*/
@Override
public void write(byte b[], int off, int len) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
if (trace) {
if (quote) {
for (int i = 0; i < len; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,6 @@ private String handleTimeoutTaskResult(ScheduledFuture<String> sf) {
return sf.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
exceptionMessage = String.format("%s %s", e, ses.toString());
} catch (CancellationException e) {
exceptionMessage = e.toString();
} catch (InterruptedException e) {
wasInterrupted = true;
exceptionMessage = e.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class SimpleClient {
static FolderViewer fv;
static MessageViewer mv;

public static void main(String argv[]) {
public static void main(String[] argv) {
boolean usage = false;

for (int optind = 0; optind < argv.length; optind++) {
Expand Down
2 changes: 1 addition & 1 deletion demos/client/src/main/java/example/client/TextViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setInputStream(InputStream ins) {
int bytes_read = 0;
// check that we can actually read
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte data[] = new byte[1024];
byte[] data = new byte[1024];

try {
while ((bytes_read = ins.read(data)) > 0)
Expand Down
4 changes: 2 additions & 2 deletions demos/demo/src/main/java/example/app/CRLFOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public void write(int b) throws IOException {
lastb = b;
}

public void write(byte b[]) throws IOException {
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}

public void write(byte b[], int off, int len) throws IOException {
public void write(byte[] b, int off, int len) throws IOException {
int start = off;

len += off;
Expand Down

0 comments on commit 4d06743

Please sign in to comment.