Skip to content

Commit 2297777

Browse files
committed
Revert "Close resources on ServerListPing"
This reverts commit 9873a64.
1 parent 9873a64 commit 2297777

File tree

1 file changed

+66
-72
lines changed

1 file changed

+66
-72
lines changed

balancer/src/main/java/com/jaimemartz/playerbalancer/utils/ServerListPing.java

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -48,79 +48,73 @@ private static void writeVarInt(DataOutputStream out, int paramInt) throws IOExc
4848
}
4949

5050
public StatusResponse ping(InetSocketAddress host, int timeout) throws IOException {
51-
Socket socket = new Socket();
52-
53-
socket.setSoTimeout(timeout);
54-
socket.connect(host, timeout);
55-
56-
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
57-
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
58-
59-
ByteArrayOutputStream byteArrStream = new ByteArrayOutputStream();
60-
DataOutputStream handshake = new DataOutputStream(byteArrStream);
61-
handshake.writeByte(0x00); //packet id for handshake
62-
writeVarInt(handshake, 4); //protocol version
63-
writeVarInt(handshake, host.getHostString().length()); //host length
64-
handshake.writeBytes(host.getHostString()); //host string
65-
handshake.writeShort(host.getPort()); //port
66-
writeVarInt(handshake, 1); //state (1 for handshake)
67-
handshake.close(); // close handshake packet write stream
68-
69-
writeVarInt(dataOutputStream, byteArrStream.size()); //prepend size
70-
dataOutputStream.write(byteArrStream.toByteArray()); //write handshake packet
71-
72-
dataOutputStream.writeByte(0x01); //size is only 1
73-
dataOutputStream.writeByte(0x00); //packet id for ping
74-
75-
int size = readVarInt(dataInputStream); //size of packet
76-
int id = readVarInt(dataInputStream); //packet id
77-
78-
if (id == -1) {
79-
throw new IOException("Premature end of stream.");
80-
}
81-
82-
if (id != 0x00) { //we want a status response
83-
throw new IOException("Invalid packetID");
84-
}
85-
86-
int length = readVarInt(dataInputStream); //length of json string
87-
if (length == -1) {
88-
throw new IOException("Premature end of stream.");
89-
}
90-
91-
if (length == 0) {
92-
throw new IOException("Invalid string length.");
93-
}
94-
95-
byte[] in = new byte[length];
96-
dataInputStream.readFully(in); //read json string
97-
String json = new String(in);
98-
99-
long now = System.currentTimeMillis();
100-
dataOutputStream.writeByte(0x09); //size of packet
101-
dataOutputStream.writeByte(0x01); //0x01 for ping
102-
dataOutputStream.writeLong(now); //time!?
103-
104-
dataOutputStream.close(); // close request write stream
105-
106-
readVarInt(dataInputStream);
107-
id = readVarInt(dataInputStream);
108-
if (id == -1) {
109-
throw new IOException("Premature end of stream.");
110-
}
111-
112-
if (id != 0x01) {
113-
throw new IOException("Invalid packetID");
51+
try (Socket socket = new Socket()) {
52+
socket.setSoTimeout(timeout);
53+
socket.connect(host, timeout);
54+
55+
try (DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
56+
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream())) {
57+
ByteArrayOutputStream b = new ByteArrayOutputStream();
58+
DataOutputStream handshake = new DataOutputStream(b);
59+
handshake.writeByte(0x00); //packet id for handshake
60+
writeVarInt(handshake, 4); //protocol version
61+
writeVarInt(handshake, host.getHostString().length()); //host length
62+
handshake.writeBytes(host.getHostString()); //host string
63+
handshake.writeShort(host.getPort()); //port
64+
writeVarInt(handshake, 1); //state (1 for handshake)
65+
66+
writeVarInt(dataOutputStream, b.size()); //prepend size
67+
dataOutputStream.write(b.toByteArray()); //write handshake packet
68+
69+
70+
dataOutputStream.writeByte(0x01); //size is only 1
71+
dataOutputStream.writeByte(0x00); //packet id for ping
72+
int size = readVarInt(dataInputStream); //size of packet
73+
int id = readVarInt(dataInputStream); //packet id
74+
75+
if (id == -1) {
76+
throw new IOException("Premature end of stream.");
77+
}
78+
79+
if (id != 0x00) { //we want a status response
80+
throw new IOException("Invalid packetID");
81+
}
82+
83+
int length = readVarInt(dataInputStream); //length of json string
84+
if (length == -1) {
85+
throw new IOException("Premature end of stream.");
86+
}
87+
88+
if (length == 0) {
89+
throw new IOException("Invalid string length.");
90+
}
91+
92+
byte[] in = new byte[length];
93+
dataInputStream.readFully(in); //read json string
94+
String json = new String(in);
95+
96+
97+
long now = System.currentTimeMillis();
98+
dataOutputStream.writeByte(0x09); //size of packet
99+
dataOutputStream.writeByte(0x01); //0x01 for ping
100+
dataOutputStream.writeLong(now); //time!?
101+
102+
readVarInt(dataInputStream);
103+
id = readVarInt(dataInputStream);
104+
if (id == -1) {
105+
throw new IOException("Premature end of stream.");
106+
}
107+
108+
if (id != 0x01) {
109+
throw new IOException("Invalid packetID");
110+
}
111+
112+
long pingTime = dataInputStream.readLong(); //read response
113+
StatusResponse response = gson.fromJson(json, StatusResponse.class);
114+
response.time = (int) (now - pingTime);
115+
return response;
116+
}
114117
}
115-
116-
long pingTime = dataInputStream.readLong(); //read response
117-
StatusResponse response = gson.fromJson(json, StatusResponse.class);
118-
response.time = (int) (now - pingTime);
119-
120-
dataInputStream.close(); // close response read stream
121-
socket.close(); // close socket
122-
123-
return response;
124118
}
125119

126120
public static class StatusResponse {

0 commit comments

Comments
 (0)