Skip to content
This repository has been archived by the owner on Dec 29, 2019. It is now read-only.

Commit

Permalink
Possible fix for issues resulting from incorrectly using buffered str…
Browse files Browse the repository at this point in the history
…eams.
  • Loading branch information
neatorobito committed Nov 17, 2015
1 parent 887b730 commit 9304552
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ public void doOnBackground() {
//TODO Use buffered streams.
Log.v(Salut.TAG, "Sending client registration data to server...");
String serializedClient = LoganSquare.serialize(salutInstance.thisDevice);
BufferedOutputStream bufferedOut = new BufferedOutputStream(registrationSocket.getOutputStream());
DataOutputStream toClient = new DataOutputStream(bufferedOut);
DataOutputStream toClient = new DataOutputStream(registrationSocket.getOutputStream());
toClient.writeUTF(serializedClient);
toClient.flush();

Log.v(Salut.TAG, "Receiving server registration data...");
BufferedInputStream bufferedInput = new BufferedInputStream(registrationSocket.getInputStream());
DataInputStream fromServer = new DataInputStream(bufferedInput);
DataInputStream fromServer = new DataInputStream(registrationSocket.getInputStream());

if(!salutInstance.thisDevice.isRegistered)
{
Log.v(Salut.TAG, "Receiving server registration data...");
String serializedServer = fromServer.readUTF();
SalutDevice serverDevice = LoganSquare.parse(serializedServer, SalutDevice.class);

serverDevice.serviceAddress = registrationSocket.getInetAddress().toString().replace("/", "");
salutInstance.registeredHost = serverDevice;

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/peak/salut/BackgroundDataJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.arasthel.asyncjob.AsyncJob;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.net.Socket;

Expand All @@ -27,8 +28,8 @@ public void doOnBackground() {
{
//If this code is reached, a client has connected and transferred data.
Log.v(Salut.TAG, "A device is sending data...");
BufferedInputStream bufferedRead = new BufferedInputStream(clientSocket.getInputStream());
DataInputStream dataStreamFromOtherDevice = new DataInputStream(bufferedRead);

DataInputStream dataStreamFromOtherDevice = new DataInputStream(clientSocket.getInputStream());
data = dataStreamFromOtherDevice.readUTF();
dataStreamFromOtherDevice.close();

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/peak/salut/BackgroundDataSendJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public void doOnBackground() {

//If this code is reached, a client has connected and transferred data.
Log.d(Salut.TAG, "Connected, transferring data...");
BufferedOutputStream bufferedOut = new BufferedOutputStream(dataSocket.getOutputStream());
DataOutputStream dataStreamToOtherDevice = new DataOutputStream(bufferedOut);
DataOutputStream dataStreamToOtherDevice = new DataOutputStream(dataSocket.getOutputStream());
String dataToSend = LoganSquare.serialize(data);
dataStreamToOtherDevice.writeUTF(dataToSend);
dataStreamToOtherDevice.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ public void doOnBackground() {
Log.d(Salut.TAG, "A device has connected to the server, transferring data...");

Log.v(Salut.TAG, "Receiving client registration data...");
BufferedInputStream bufferInput = new BufferedInputStream(clientSocket.getInputStream());
DataInputStream fromClient = new DataInputStream(bufferInput);
DataInputStream fromClient = new DataInputStream(clientSocket.getInputStream());
String serializedClient = fromClient.readUTF();

System.out.println(serializedClient);


SalutDevice clientDevice = LoganSquare.parse(serializedClient, SalutDevice.class);
clientDevice.serviceAddress = clientSocket.getInetAddress().toString().replace("/", "");

BufferedOutputStream bufferedOut = new BufferedOutputStream(clientSocket.getOutputStream());
DataOutputStream toClient = new DataOutputStream(bufferedOut);
DataOutputStream toClient = new DataOutputStream(clientSocket.getOutputStream());

if (!clientDevice.isRegistered) {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/peak/salut/SalutBroadcastReciever.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void onReceive(Context context, Intent intent) {
NetworkInfo networkInfo = (NetworkInfo) intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO);

if (networkInfo.isConnected() && networkInfo.getTypeName().equals("WIFI_P2P")) {

salutInstance.isConnectedToAnotherDevice = true;
manager.requestConnectionInfo(channel, salutInstance);

Expand Down

0 comments on commit 9304552

Please sign in to comment.