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

optimize array code style #4031

Merged
merged 1 commit into from
May 12, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public int read() throws IOException {
}

@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
* UnsafeByteArrayInputStream.
*/
public class UnsafeByteArrayInputStream extends InputStream {
protected byte mData[];
protected byte[] mData;

protected int mPosition, mLimit, mMark = 0;

public UnsafeByteArrayInputStream(byte buf[]) {
public UnsafeByteArrayInputStream(byte[] buf) {
this(buf, 0, buf.length);
}

public UnsafeByteArrayInputStream(byte buf[], int offset) {
public UnsafeByteArrayInputStream(byte[] buf, int offset) {
this(buf, offset, buf.length - offset);
}

public UnsafeByteArrayInputStream(byte buf[], int offset, int length) {
public UnsafeByteArrayInputStream(byte[] buf, int offset, int length) {
mData = buf;
mPosition = mMark = offset;
mLimit = Math.min(offset + length, buf.length);
Expand All @@ -47,7 +47,7 @@ public int read() {
}

@Override
public int read(byte b[], int off, int len) {
public int read(byte[] b, int off, int len) {
if (b == null) {
throw new NullPointerException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* UnsafeByteArrayOutputStream.
*/
public class UnsafeByteArrayOutputStream extends OutputStream {
protected byte mBuffer[];
protected byte[] mBuffer;

protected int mCount;

Expand All @@ -51,7 +51,7 @@ public void write(int b) {
}

@Override
public void write(byte b[], int off, int len) {
public void write(byte[] b, int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ public void writeValue(Object obj, JSONWriter jb, boolean writeClass) throws IOE
jb.objectBegin();

Wrapper w = Wrapper.getWrapper(c);
String pns[] = w.getPropertyNames();
String[] pns = w.getPropertyNames();

for (String pn : pns) {
if ((obj instanceof Throwable) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static Object toArray(Class<?> c, Stack<Object> list, int len) throws Pa
return EMPTY_STRING_ARRAY;
} else {
Object o;
String ss[] = new String[len];
String[] ss = new String[len];
for (int i = len - 1; i >= 0; i--) {
o = list.pop();
ss[i] = (o == null ? null : o.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Yylex {
* at the beginning of a line
* l is of the form l = 2*k, k a non negative integer
*/
private static final int ZZ_LEXSTATE[] = {
private static final int[] ZZ_LEXSTATE = {
0, 0, 1, 1, 2, 2
};

Expand Down Expand Up @@ -92,7 +92,7 @@ public class Yylex {
/**
* The transition table of the DFA
*/
private static final int ZZ_TRANS[] = {
private static final int[] ZZ_TRANS = {
3, 4, 5, 5, 6, 3, 5, 3, 7, 8,
3, 9, 3, 5, 10, 11, 5, 12, 5, 5,
13, 5, 5, 5, 5, 5, 14, 5, 5, 5,
Expand Down Expand Up @@ -247,7 +247,7 @@ public class Yylex {
private static final int ZZ_NO_MATCH = 1;
private static final int ZZ_PUSHBACK_2BIG = 2;
/* error messages for the codes above */
private static final String ZZ_ERROR_MSG[] = {
private static final String[] ZZ_ERROR_MSG = {
"Unkown internal scanner error",
"Error: could not match input",
"Error: pushback value was too large"
Expand Down Expand Up @@ -276,7 +276,7 @@ public class Yylex {
* this buffer contains the current text to be matched and is
* the source of the yytext() string
*/
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
private char[] zzBuffer = new char[ZZ_BUFFERSIZE];
/**
* the textposition at the last accepting state
*/
Expand Down Expand Up @@ -447,7 +447,7 @@ private boolean zzRefill() throws java.io.IOException {
/* is the buffer big enough? */
if (zzCurrentPos >= zzBuffer.length) {
/* if not: blow it up */
char newBuffer[] = new char[zzCurrentPos * 2];
char[] newBuffer = new char[zzCurrentPos * 2];
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
zzBuffer = newBuffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@
*/
package org.apache.dubbo.common.threadpool.support;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.JVMUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ExecutorService;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.JVMUtil;

/**
* Abort Policy.
Expand All @@ -46,6 +46,16 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {

private static volatile long lastPrintTime = 0;

private static final long TEN_MINUTES_MILLS = 10 * 60 * 1000;

private static final String OS_WIN_PREFIX = "win";

private static final String OS_NAME_KEY = "os.name";

private static final String WIN_DATETIME_FORMAT = "yyyy-MM-dd_HH-mm-ss";

private static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd_HH:mm:ss";

private static Semaphore guard = new Semaphore(1);

public AbortPolicyWithReport(String threadName, URL url) {
Expand All @@ -56,11 +66,13 @@ public AbortPolicyWithReport(String threadName, URL url) {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
String msg = String.format("Thread pool is EXHAUSTED!" +
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: %d)," +
" Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!",
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(), e.getLargestPoolSize(),
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
url.getProtocol(), url.getIp(), url.getPort());
" Thread Name: %s, Pool Size: %d (active: %d, core: %d, max: %d, largest: %d), Task: %d (completed: "
+ "%d)," +
" Executor status:(isShutdown:%s, isTerminated:%s, isTerminating:%s), in %s://%s:%d!",
threadName, e.getPoolSize(), e.getActiveCount(), e.getCorePoolSize(), e.getMaximumPoolSize(),
e.getLargestPoolSize(),
e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(), e.isTerminated(), e.isTerminating(),
url.getProtocol(), url.getIp(), url.getPort());
logger.warn(msg);
dumpJStack();
throw new RejectedExecutionException(msg);
Expand All @@ -70,7 +82,7 @@ private void dumpJStack() {
long now = System.currentTimeMillis();

//dump every 10 minutes
if (now - lastPrintTime < 10 * 60 * 1000) {
if (now - lastPrintTime < TEN_MINUTES_MILLS) {
return;
}

Expand All @@ -84,18 +96,19 @@ private void dumpJStack() {

SimpleDateFormat sdf;

String os = System.getProperty("os.name").toLowerCase();
String os = System.getProperty(OS_NAME_KEY).toLowerCase();

// window system don't support ":" in file name
if (os.contains("win")) {
sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
if (os.contains(OS_WIN_PREFIX)) {
sdf = new SimpleDateFormat(WIN_DATETIME_FORMAT);
} else {
sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
sdf = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
}

String dateStr = sdf.format(new Date());
//try-with-resources
try (FileOutputStream jStackStream = new FileOutputStream(new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
try (FileOutputStream jStackStream = new FileOutputStream(
new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
JVMUtil.jstack(jStackStream);
} catch (Throwable t) {
logger.error("dump jStack error", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) {
if (message == null || message.trim().length() == 0) {
buf.append("EXAMPLE: log error / log 100");
} else {
String str[] = message.split(" ");
String[] str = message.split(" ");
if (!StringUtils.isInteger(str[0])) {
LoggerFactory.setLevel(Level.valueOf(message.toUpperCase()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.dubbo.remoting.transport;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.RemotingConstants;
Expand All @@ -26,23 +29,25 @@
import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.Codec2;

import java.io.IOException;
import java.net.InetSocketAddress;

/**
* AbstractCodec
*/
public abstract class AbstractCodec implements Codec2 {

private static final Logger logger = LoggerFactory.getLogger(AbstractCodec.class);

private static final String CLIENT_SIDE = "client";

private static final String SERVER_SIDE = "server";

protected static void checkPayload(Channel channel, long size) throws IOException {
int payload = RemotingConstants.DEFAULT_PAYLOAD;
if (channel != null && channel.getUrl() != null) {
payload = channel.getUrl().getParameter(RemotingConstants.PAYLOAD_KEY, RemotingConstants.DEFAULT_PAYLOAD);
}
if (payload > 0 && size > payload) {
ExceedPayloadLimitException e = new ExceedPayloadLimitException("Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel);
ExceedPayloadLimitException e = new ExceedPayloadLimitException(
"Data length too large: " + size + ", max payload: " + payload + ", channel: " + channel);
logger.error(e);
throw e;
}
Expand All @@ -53,21 +58,21 @@ protected Serialization getSerialization(Channel channel) {
}

protected boolean isClientSide(Channel channel) {
String side = (String) channel.getAttribute(Constants.SIDE_KEY);
if ("client".equals(side)) {
String side = (String)channel.getAttribute(Constants.SIDE_KEY);
if (CLIENT_SIDE.equals(side)) {
return true;
} else if ("server".equals(side)) {
} else if (SERVER_SIDE.equals(side)) {
return false;
} else {
InetSocketAddress address = channel.getRemoteAddress();
URL url = channel.getUrl();
boolean client = url.getPort() == address.getPort()
&& NetUtils.filterLocalHost(url.getIp()).equals(
NetUtils.filterLocalHost(address.getAddress()
.getHostAddress()));
channel.setAttribute(Constants.SIDE_KEY, client ? "client"
: "server");
return client;
boolean isClient = url.getPort() == address.getPort()
&& NetUtils.filterLocalHost(url.getIp()).equals(
NetUtils.filterLocalHost(address.getAddress()
.getHostAddress()));
channel.setAttribute(Constants.SIDE_KEY, isClient ? CLIENT_SIDE
: SERVER_SIDE);
return isClient;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public String telnet(Channel channel, String message) {
if (message == null || message.trim().length() == 0) {
buf.append("EXAMPLE: log error / log 100");
} else {
String str[] = message.split(" ");
String[] str = message.split(" ");
if (!StringUtils.isInteger(str[0])) {
LoggerFactory.setLevel(Level.valueOf(message.toUpperCase()));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Deprecated
public class RandomAccessByteArrayOutputStream extends OutputStream {

protected byte buffer[];
protected byte[] buffer;

protected int count;

Expand Down Expand Up @@ -54,7 +54,7 @@ public void write(int b) {
}

@Override
public void write(byte b[], int off, int len) {
public void write(byte[] b, int off, int len) {

if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
Expand Down