2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 21:27:40 +00:00

Revert "Try-with-resources on inputstreams"

This reverts commit aa776739b7feed778826593949f9cb471a6a9fb6
This commit is contained in:
Simon Redman 2019-04-19 05:10:11 +00:00
parent 21b97d335c
commit dc5f10c073
5 changed files with 37 additions and 36 deletions

View File

@ -32,7 +32,6 @@ import org.json.JSONObject;
import org.kde.kdeconnect.Backends.BaseLink; import org.kde.kdeconnect.Backends.BaseLink;
import org.kde.kdeconnect.Backends.BasePairingHandler; import org.kde.kdeconnect.Backends.BasePairingHandler;
import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.Helpers.StringsHelper;
import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.NetworkPacket;
import java.io.IOException; import java.io.IOException;
@ -53,7 +52,8 @@ public class BluetoothLink extends BaseLink {
@Override @Override
public void run() { public void run() {
StringBuilder sb = new StringBuilder(); 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]; char[] buf = new char[512];
while (continueAccepting) { while (continueAccepting) {
while (sb.indexOf("\n") == -1 && continueAccepting) { while (sb.indexOf("\n") == -1 && continueAccepting) {

View File

@ -35,7 +35,6 @@ import android.util.Log;
import org.kde.kdeconnect.Backends.BaseLinkProvider; import org.kde.kdeconnect.Backends.BaseLinkProvider;
import org.kde.kdeconnect.Device; import org.kde.kdeconnect.Device;
import org.kde.kdeconnect.Helpers.StringsHelper;
import org.kde.kdeconnect.NetworkPacket; import org.kde.kdeconnect.NetworkPacket;
import java.io.IOException; import java.io.IOException;
@ -188,36 +187,34 @@ public class BluetoothLinkProvider extends BaseLinkProvider {
Log.i("BTLinkProvider/Server", "Received connection from " + socket.getRemoteDevice().getAddress()); Log.i("BTLinkProvider/Server", "Received connection from " + socket.getRemoteDevice().getAddress());
NetworkPacket np = NetworkPacket.createIdentityPacket(context); NetworkPacket np = NetworkPacket.createIdentityPacket(context);
byte[] message = np.serialize().getBytes(StringsHelper.UTF8); byte[] message = np.serialize().getBytes("UTF-8");
outputStream.write(message); outputStream.write(message);
Log.i("BTLinkProvider/Server", "Sent identity package"); Log.i("BTLinkProvider/Server", "Sent identity package");
// Listen for the response // Listen for the response
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try (Reader reader = new InputStreamReader(socket.getInputStream(), StringsHelper.UTF8)) { Reader reader = new InputStreamReader(socket.getInputStream(), "UTF-8");
int charsRead; int charsRead;
char[] buf = new char[512]; char[] buf = new char[512];
while (sb.lastIndexOf("\n") == -1 && (charsRead = reader.read(buf)) != -1) { while (sb.lastIndexOf("\n") == -1 && (charsRead = reader.read(buf)) != -1) {
sb.append(buf, 0, charsRead); 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);
} }
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);
} }
} }

View File

@ -88,7 +88,8 @@ public class LanLink extends BaseLink {
//Log.e("LanLink", "Start listening"); //Log.e("LanLink", "Start listening");
//Create a thread to take care of incoming data for the new socket //Create a thread to take care of incoming data for the new socket
new Thread(() -> { 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) { while (true) {
String packet; String packet;
try { try {
@ -219,7 +220,9 @@ public class LanLink extends BaseLink {
} }
return true; return true;
} catch (Exception e) { } catch (Exception e) {
callback.onFailure(e); if (callback != null) {
callback.onFailure(e);
}
return false; return false;
} finally { } finally {
//Make sure we close the payload stream, if any //Make sure we close the payload stream, if any

View File

@ -93,7 +93,8 @@ public class LanLinkProvider extends BaseLinkProvider implements LanLink.LinkDis
private void tcpPacketReceived(Socket socket) { private void tcpPacketReceived(Socket socket) {
NetworkPacket networkPacket; NetworkPacket networkPacket;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = reader.readLine(); String message = reader.readLine();
networkPacket = NetworkPacket.unserialize(message); networkPacket = NetworkPacket.unserialize(message);
//Log.e("TcpListener","Received TCP package: "+networkPacket.serialize()); //Log.e("TcpListener","Received TCP package: "+networkPacket.serialize());

View File

@ -184,7 +184,8 @@ public class ContactsHelper {
; ;
StringBuilder vcardJumble = new StringBuilder(); 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; String line;
while ((line = bufferedInput.readLine()) != null) { while ((line = bufferedInput.readLine()) != null) {
vcardJumble.append(line).append('\n'); 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); throw new NullPointerException("ContentResolver did not give us a stream for the VCard for uID " + ID);
} }
try (BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input))) { BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(input));
StringBuilder vCard = new StringBuilder();
String line;
while ((line = bufferedInput.readLine()) != null) {
vCard.append(line).append('\n');
}
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) { } catch (IOException e) {
// If you are experiencing this, please open a bug report indicating how you got here // If you are experiencing this, please open a bug report indicating how you got here
Log.e("Contacts", "Exception while fetching vcards", e); Log.e("Contacts", "Exception while fetching vcards", e);