diff --git a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java index 1f80c2fe..33f972f9 100644 --- a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java +++ b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLink.java @@ -32,7 +32,6 @@ 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; @@ -53,7 +52,8 @@ public class BluetoothLink extends BaseLink { @Override public void run() { StringBuilder sb = new StringBuilder(); - try (Reader reader = new InputStreamReader(socket.getInputStream(), StringsHelper.UTF8)) { + try { + Reader reader = new InputStreamReader(socket.getInputStream(), "UTF-8"); char[] buf = new char[512]; while (continueAccepting) { while (sb.indexOf("\n") == -1 && continueAccepting) { diff --git a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLinkProvider.java b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLinkProvider.java index 918d0e4a..68556d76 100644 --- a/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/BluetoothBackend/BluetoothLinkProvider.java @@ -35,7 +35,6 @@ 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; @@ -188,36 +187,34 @@ 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(StringsHelper.UTF8); + byte[] message = np.serialize().getBytes("UTF-8"); outputStream.write(message); Log.i("BTLinkProvider/Server", "Sent identity package"); // Listen for the response StringBuilder sb = new StringBuilder(); - 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); - - 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"); - - BluetoothLink link = new BluetoothLink(context, socket, - identityPacket.getString("deviceId"), BluetoothLinkProvider.this); - - addLink(identityPacket, link); + 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); } + 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; + } + + Log.i("BTLinkProvider/Server", "Received identity package"); + + BluetoothLink link = new BluetoothLink(context, socket, + identityPacket.getString("deviceId"), BluetoothLinkProvider.this); + + addLink(identityPacket, link); } } diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java index b87fe833..f4847fa0 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLink.java @@ -88,7 +88,8 @@ 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 { @@ -219,7 +220,9 @@ public class LanLink extends BaseLink { } return true; } catch (Exception e) { - callback.onFailure(e); + if (callback != null) { + callback.onFailure(e); + } return false; } finally { //Make sure we close the payload stream, if any diff --git a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java index 2c060558..83e6003a 100644 --- a/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java +++ b/src/org/kde/kdeconnect/Backends/LanBackend/LanLinkProvider.java @@ -93,7 +93,8 @@ 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())); String message = reader.readLine(); networkPacket = NetworkPacket.unserialize(message); //Log.e("TcpListener","Received TCP package: "+networkPacket.serialize()); diff --git a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java index 899ad152..32aee0fa 100644 --- a/src/org/kde/kdeconnect/Helpers/ContactsHelper.java +++ b/src/org/kde/kdeconnect/Helpers/ContactsHelper.java @@ -184,7 +184,8 @@ public class ContactsHelper { ; StringBuilder vcardJumble = new StringBuilder(); - try (BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(context.getContentResolver().openInputStream(vcardURI)))) { + try (InputStream input = context.getContentResolver().openInputStream(vcardURI)) { + BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input)); String line; while ((line = bufferedInput.readLine()) != null) { vcardJumble.append(line).append('\n'); @@ -228,16 +229,15 @@ public class ContactsHelper { throw new NullPointerException("ContentResolver did not give us a stream for the VCard for uID " + ID); } - try (BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input))) { - StringBuilder vCard = new StringBuilder(); - String line; - while ((line = bufferedInput.readLine()) != null) { - vCard.append(line).append('\n'); - } + BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input)); - toReturn.put(ID, new VCardBuilder(vCard.toString())); + StringBuilder vcard = new StringBuilder(); + String line; + while ((line = bufferedInput.readLine()) != null) { + vcard.append(line).append('\n'); } + 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);