Commit aa776739 authored by Albert Vaca Cintora's avatar Albert Vaca Cintora

Try-with-resources on inputstreams

As suggested by lgtm.com
parent e8e01c9a
......@@ -32,6 +32,7 @@ import org.json.JSONObject;
import org.kde.kdeconnect.Backends.BaseLink;
import org.kde.kdeconnect.Backends.BasePairingHandler;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.Helpers.StringsHelper;
import org.kde.kdeconnect.NetworkPacket;
import java.io.IOException;
......@@ -52,8 +53,7 @@ public class BluetoothLink extends BaseLink {
@Override
public void run() {
StringBuilder sb = new StringBuilder();
try {
Reader reader = new InputStreamReader(socket.getInputStream(), "UTF-8");
try (Reader reader = new InputStreamReader(socket.getInputStream(), StringsHelper.UTF8)) {
char[] buf = new char[512];
while (continueAccepting) {
while (sb.indexOf("\n") == -1 && continueAccepting) {
......
......@@ -35,6 +35,7 @@ import android.util.Log;
import org.kde.kdeconnect.Backends.BaseLinkProvider;
import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.Helpers.StringsHelper;
import org.kde.kdeconnect.NetworkPacket;
import java.io.IOException;
......@@ -187,34 +188,36 @@ public class BluetoothLinkProvider extends BaseLinkProvider {
Log.i("BTLinkProvider/Server", "Received connection from " + socket.getRemoteDevice().getAddress());
NetworkPacket np = NetworkPacket.createIdentityPacket(context);
byte[] message = np.serialize().getBytes("UTF-8");
byte[] message = np.serialize().getBytes(StringsHelper.UTF8);
outputStream.write(message);
Log.i("BTLinkProvider/Server", "Sent identity package");
// Listen for the response
StringBuilder sb = new StringBuilder();
Reader reader = new InputStreamReader(socket.getInputStream(), "UTF-8");
int charsRead;
char[] buf = new char[512];
while (sb.lastIndexOf("\n") == -1 && (charsRead = reader.read(buf)) != -1) {
sb.append(buf, 0, charsRead);
}
try (Reader reader = new InputStreamReader(socket.getInputStream(), StringsHelper.UTF8)) {
int charsRead;
char[] buf = new char[512];
while (sb.lastIndexOf("\n") == -1 && (charsRead = reader.read(buf)) != -1) {
sb.append(buf, 0, charsRead);
}
String response = sb.toString();
final NetworkPacket identityPacket = NetworkPacket.unserialize(response);
String response = sb.toString();
final NetworkPacket identityPacket = NetworkPacket.unserialize(response);
if (!identityPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
Log.e("BTLinkProvider/Server", "2 Expecting an identity package");
return;
}
if (!identityPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
Log.e("BTLinkProvider/Server", "2 Expecting an identity package");
return;
}
Log.i("BTLinkProvider/Server", "Received identity package");
Log.i("BTLinkProvider/Server", "Received identity package");
BluetoothLink link = new BluetoothLink(context, socket,
identityPacket.getString("deviceId"), BluetoothLinkProvider.this);
BluetoothLink link = new BluetoothLink(context, socket,
identityPacket.getString("deviceId"), BluetoothLinkProvider.this);
addLink(identityPacket, link);
}
addLink(identityPacket, link);
}
}
......
......@@ -88,8 +88,7 @@ public class LanLink extends BaseLink {
//Log.e("LanLink", "Start listening");
//Create a thread to take care of incoming data for the new socket
new Thread(() -> {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8));
try (BufferedReader reader = new BufferedReader(new InputStreamReader(newSocket.getInputStream(), StringsHelper.UTF8))) {
while (true) {
String packet;
try {
......@@ -220,9 +219,7 @@ public class LanLink extends BaseLink {
}
return true;
} catch (Exception e) {
if (callback != null) {
callback.onFailure(e);
}
callback.onFailure(e);
return false;
} finally {
//Make sure we close the payload stream, if any
......
......@@ -93,8 +93,7 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
private void tcpPacketReceived(Socket socket) {
NetworkPacket networkPacket;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
  • Apparently this closes the socket afterwards, leading to a SocketException

    java.net.SocketException: Socket is closed at java.net.Socket.setSoTimeout(Socket.java:1176) at com.android.org.conscrypt.ConscryptSocketBase.setSoTimeout(ConscryptSocketBase.java:258) at org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper.configureSslSocket(SslHelper.java:208) at org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper.convertToSslSocket(SslHelper.java:226) at org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider.identityPacketReceived(LanLinkProvider.java:218) at org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider.tcpPacketReceived(LanLinkProvider.java:111) at org.kde.kdeconnect.Backends.LanBackend.LanLinkProvider.lambda$setupTcpListener$5(LanLinkProvider.java:328) at org.kde.kdeconnect.Backends.LanBackend.-$$Lambda$LanLinkProvider$bBuPNsOtlmqEa9IG9dEk6eQXOjU.run(Unknown Source:2) at java.lang.Thread.run(Thread.java:764)

    Edited by Nicolas Fella
  • Crap. Can you revert this change? Here and in the Bluetooth link... I'm AFK until Monday. 🙏

  • I pushed the revert, which is branch revert-aa776739 (in case you need access to the changes that way :) )

    Edited by Simon Redman
Please register or sign in to reply
String message = reader.readLine();
networkPacket = NetworkPacket.unserialize(message);
//Log.e("TcpListener","Received TCP package: "+networkPacket.serialize());
......
......@@ -183,8 +183,7 @@ public class ContactsHelper {
;
StringBuilder vcardJumble = new StringBuilder();
try (InputStream input = context.getContentResolver().openInputStream(vcardURI)) {
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input));
try (BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(context.getContentResolver().openInputStream(vcardURI)))) {
String line;
while ((line = bufferedInput.readLine()) != null) {
vcardJumble.append(line).append('\n');
......@@ -228,15 +227,16 @@ public class ContactsHelper {
throw new NullPointerException("ContentResolver did not give us a stream for the VCard for uID " + ID);
}
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input));
try (BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input))) {
StringBuilder vCard = new StringBuilder();
String line;
while ((line = bufferedInput.readLine()) != null) {
vCard.append(line).append('\n');
}
StringBuilder vcard = new StringBuilder();
String line;
while ((line = bufferedInput.readLine()) != null) {
vcard.append(line).append('\n');
toReturn.put(ID, new VCardBuilder(vCard.toString()));
}
toReturn.put(ID, new VCardBuilder(vcard.toString()));
} catch (IOException e) {
// If you are experiencing this, please open a bug report indicating how you got here
Log.e("Contacts", "Exception while fetching vcards", e);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment