Skip to content

Commit

Permalink
[#noissue] fix findbug issue
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad authored and koo-taejin committed Sep 20, 2017
1 parent 496e951 commit ce3c64c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@
import java.util.List;
import java.util.StringTokenizer;

public class StringUtils {
public final class StringUtils {

private static final int DEFAULT_ABBREVIATE_MAX_WIDTH = 64;

private static final String NULL_STRING = "null";

public StringUtils() {
throw new AssertionError();
private StringUtils() {
}

public static String defaultString(final String str, final String defaultStr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author Taejin Koo
*/
public abstract class AbstractNetworkChecker implements NetworkChecker {
public abstract class AbstractNetworkChecker implements NetworkChecker {

private static final String WHITE_SPACE = " "; // 4space
private static final String LINE_SEPARATOR = "\r\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.navercorp.pinpoint.tools.network;

import com.navercorp.pinpoint.tools.utils.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -31,9 +33,7 @@ protected boolean check(InetSocketAddress address) throws IOException {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
IOUtils.closeQuietly(socket);
}
return false;
}
Expand All @@ -51,17 +51,24 @@ protected boolean check(InetSocketAddress address, byte[] requestData, byte[] ex
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
IOUtils.closeQuietly(socket);
}
return false;
}

private Socket createSocket(InetSocketAddress socketAddress) throws IOException {
Socket socket = new Socket();
socket.connect(socketAddress);
socket.setSoTimeout(3000);
final Socket socket = new Socket();

boolean success = false;
try {
socket.setSoTimeout(3000);
socket.connect(socketAddress);
success = true;
} finally {
if (!success) {
IOUtils.closeQuietly(socket);
}
}
return socket;
}

Expand All @@ -75,9 +82,13 @@ private byte[] read(Socket socket, int readSize) throws IOException {
byte[] buf = new byte[readSize];

InputStream inputStream = socket.getInputStream();
inputStream.read(buf);
// TODO readFully()
int read = inputStream.read(buf);
if (read == IOUtils.EOF) {
return IOUtils.EMPTY_BYTES;
}

return buf;
return Arrays.copyOf(buf, read);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.navercorp.pinpoint.tools.network;

import com.navercorp.pinpoint.tools.utils.IOUtils;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
Expand Down Expand Up @@ -30,9 +32,7 @@ protected boolean check(InetSocketAddress address) throws IOException {
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
IOUtils.closeQuietly(socket);
}
return false;
}
Expand All @@ -50,9 +50,7 @@ protected boolean check(InetSocketAddress address, byte[] requestData, byte[] ex
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
IOUtils.closeQuietly(socket);
}
return false;
}
Expand All @@ -65,10 +63,19 @@ private DatagramSocket createSocket() throws IOException {
}

private DatagramSocket createSocket(InetSocketAddress socketAddress) throws IOException {
DatagramSocket socket = new DatagramSocket();
socket.connect(socketAddress);
final DatagramSocket socket = new DatagramSocket();

boolean success = false;
try {
socket.setSoTimeout(3000);
socket.connect(socketAddress);
success = true;
} finally {
if (!success) {
IOUtils.closeQuietly(socket);
}
}

socket.setSoTimeout(3000);
return socket;
}

Expand All @@ -83,7 +90,11 @@ private byte[] read(DatagramSocket socket, int readSize) throws IOException {
DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
socket.receive(datagramPacket);

return buf;
int length = datagramPacket.getLength();
if (length == 0) {
return IOUtils.EMPTY_BYTES;
}
return Arrays.copyOf(buf, length);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2017 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.tools.utils;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.Socket;

/**
* @author Woonduk Kang(emeroad)
*/
public final class IOUtils {

public static final byte[] EMPTY_BYTES = new byte[0];

public static final int EOF = -1;

private IOUtils() {
}

public static void closeQuietly(Socket socket) {
if (socket != null) {
try {
socket.close();
} catch (IOException ignore) {
// skip
}
}
}

public static void closeQuietly(DatagramSocket socket) {
if (socket != null) {
socket.close();
}
}

}

0 comments on commit ce3c64c

Please sign in to comment.