Skip to content

Commit

Permalink
Implement custom marschal/unmarschal for LogItem, hopefully this will…
Browse files Browse the repository at this point in the history
… fix the problems.
  • Loading branch information
schwabe committed Apr 25, 2016
1 parent 4d8c036 commit 3247833
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 252 deletions.
25 changes: 10 additions & 15 deletions main/src/main/java/de/blinkt/openvpn/core/LogFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Parcel;

import java.io.BufferedInputStream;
import java.io.File;
Expand All @@ -18,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.Locale;

Expand Down Expand Up @@ -50,14 +50,14 @@ public void handleMessage(Message msg) {
throw new RuntimeException("mLogFile not null");
readLogCache((File) msg.obj);
openLogFile((File) msg.obj);
} else if (msg.what == LOG_MESSAGE && msg.obj instanceof VpnStatus.LogItem) {
} else if (msg.what == LOG_MESSAGE && msg.obj instanceof LogItem) {
// Ignore log messages if not yet initialized
if (mLogFile == null)
return;
writeLogItemToDisk((VpnStatus.LogItem) msg.obj);
writeLogItemToDisk((LogItem) msg.obj);
} else if (msg.what == TRIM_LOG_FILE) {
trimLogFile();
for (VpnStatus.LogItem li : VpnStatus.getlogbuffer())
for (LogItem li : VpnStatus.getlogbuffer())
writeLogItemToDisk(li);
} else if (msg.what == FLUSH_TO_DISK) {
flushToDisk();
Expand All @@ -84,15 +84,13 @@ private void trimLogFile() {
}
}

private void writeLogItemToDisk(VpnStatus.LogItem li) throws IOException {
Parcel p = Parcel.obtain();
li.writeToParcel(p, 0);
private void writeLogItemToDisk(LogItem li) throws IOException {

// We do not really care if the log cache breaks between Android upgrades,
// write binary format to disc
byte[] liBytes = p.marshall();
byte[] liBytes = li.getMarschaledBytes();

writeEscapedBytes(liBytes);
p.recycle();
}

public void writeEscapedBytes(byte[] bytes) throws IOException {
Expand Down Expand Up @@ -211,19 +209,16 @@ else if (b == 1)

}

protected void restoreLogItem(byte[] buf, int len) {
Parcel p = Parcel.obtain();
p.unmarshall(buf, 0, len);
p.setDataPosition(0);
VpnStatus.LogItem li = VpnStatus.LogItem.CREATOR.createFromParcel(p);
protected void restoreLogItem(byte[] buf, int len) throws UnsupportedEncodingException {

LogItem li = new LogItem(buf, len);
if (li.verify()) {
VpnStatus.newLogItem(li, true);
} else {
VpnStatus.logError(String.format(Locale.getDefault(),
"Could not read log item from file: %d: %s",
len, bytesToHex(buf, Math.max(len, 80))));
}
p.recycle();
}

final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
Expand Down
Loading

0 comments on commit 3247833

Please sign in to comment.