Compact when NativeDaemonConnector hits buffer limit
If NativeDaemonConnector hits its buffer limit, it would truncate the data and lose some information. This change compacts the buffer and then retries to read the rest of the data. Change-Id: I0d5fee097bdd6808212ef3ad6fb4abbc6310fd4a
This commit is contained in:
@@ -48,6 +48,8 @@ final class NativeDaemonConnector implements Runnable {
|
|||||||
private String mSocket;
|
private String mSocket;
|
||||||
private INativeDaemonConnectorCallbacks mCallbacks;
|
private INativeDaemonConnectorCallbacks mCallbacks;
|
||||||
|
|
||||||
|
private final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
class ResponseCode {
|
class ResponseCode {
|
||||||
public static final int ActionInitiated = 100;
|
public static final int ActionInitiated = 100;
|
||||||
|
|
||||||
@@ -87,7 +89,7 @@ final class NativeDaemonConnector implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void listenToSocket() throws IOException {
|
private void listenToSocket() throws IOException {
|
||||||
LocalSocket socket = null;
|
LocalSocket socket = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
socket = new LocalSocket();
|
socket = new LocalSocket();
|
||||||
@@ -100,13 +102,13 @@ final class NativeDaemonConnector implements Runnable {
|
|||||||
InputStream inputStream = socket.getInputStream();
|
InputStream inputStream = socket.getInputStream();
|
||||||
mOutputStream = socket.getOutputStream();
|
mOutputStream = socket.getOutputStream();
|
||||||
|
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
|
int start = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int count = inputStream.read(buffer);
|
int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
|
||||||
if (count < 0) break;
|
if (count < 0) break;
|
||||||
|
|
||||||
int start = 0;
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (buffer[i] == 0) {
|
if (buffer[i] == 0) {
|
||||||
String event = new String(buffer, start, i - start);
|
String event = new String(buffer, start, i - start);
|
||||||
@@ -139,6 +141,13 @@ final class NativeDaemonConnector implements Runnable {
|
|||||||
start = i + 1;
|
start = i + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (start != count) {
|
||||||
|
final int remaining = BUFFER_SIZE - start;
|
||||||
|
System.arraycopy(buffer, start, buffer, 0, remaining);
|
||||||
|
start = remaining;
|
||||||
|
} else {
|
||||||
|
start = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Slog.e(TAG, "Communications error", ex);
|
Slog.e(TAG, "Communications error", ex);
|
||||||
|
|||||||
Reference in New Issue
Block a user