Commit dc5f10c0 authored by Simon Redman's avatar Simon Redman

Revert "Try-with-resources on inputstreams"

This reverts commit aa776739
parent 21b97d33
......@@ -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) {
......
......@@ -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);
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);
}
if (!identityPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
Log.e("BTLinkProvider/Server", "2 Expecting an identity package");
return;
}
String response = sb.toString();
final NetworkPacket identityPacket = NetworkPacket.unserialize(response);
Log.i("BTLinkProvider/Server", "Received identity package");
if (!identityPacket.getType().equals(NetworkPacket.PACKET_TYPE_IDENTITY)) {
Log.e("BTLinkProvider/Server", "2 Expecting an identity package");
return;
}
BluetoothLink link = new BluetoothLink(context, socket,
identityPacket.getString("deviceId"), BluetoothLinkProvider.this);
Log.i("BTLinkProvider/Server", "Received identity package");
addLink(identityPacket, link);
}
BluetoothLink link = new BluetoothLink(context, socket,
identityPacket.getString("deviceId"), BluetoothLinkProvider.this);
addLink(identityPacket, link);
}
}
......
......@@ -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
......
......@@ -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());
......
......@@ -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);
......
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