Merge "Add a flag to dump email messages."

This commit is contained in:
Makoto Onuki 2010-03-17 15:46:00 -07:00 committed by Android (Google) Code Review
commit a29495c03c
5 changed files with 77 additions and 29 deletions

View File

@ -482,4 +482,15 @@ public class Utility {
// If the top 2 bits is '10', it's not a first byte.
return (b & 0xc0) != 0x80;
}
public static String byteToHex(int b) {
return byteToHex(new StringBuilder(), b).toString();
}
public static StringBuilder byteToHex(StringBuilder sb, int b) {
b &= 0xFF;
sb.append("0123456789ABCDEF".charAt(b >> 4));
sb.append("0123456789ABCDEF".charAt(b & 0xF));
return sb;
}
}

View File

@ -17,9 +17,11 @@
package com.android.email.mail.transport;
import com.android.email.Email;
import com.android.email.Utility;
import android.util.Log;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -29,19 +31,25 @@ import java.io.InputStream;
*
* Use of this class *MUST* be restricted to logging-enabled situations only.
*/
public class LoggingInputStream extends InputStream {
InputStream mIn;
StringBuilder mSb;
boolean mBufferDirty;
private final String LINE_TAG = "RAW ";
public class LoggingInputStream extends FilterInputStream {
private StringBuilder mSb;
private boolean mDumpEmptyLines;
private final String mTag;
public LoggingInputStream(InputStream in) {
super();
mIn = in;
mSb = new StringBuilder(LINE_TAG);
mBufferDirty = false;
this(in, "RAW", false);
}
public LoggingInputStream(InputStream in, String tag, boolean dumpEmptyLines) {
super(in);
mTag = tag + " ";
mDumpEmptyLines = dumpEmptyLines;
initBuffer();
Log.d(Email.LOG_TAG, mTag + "dump start");
}
private void initBuffer() {
mSb = new StringBuilder(mTag);
}
/**
@ -49,7 +57,7 @@ public class LoggingInputStream extends InputStream {
*/
@Override
public int read() throws IOException {
int oneByte = mIn.read();
int oneByte = super.read();
logRaw(oneByte);
return oneByte;
}
@ -59,10 +67,10 @@ public class LoggingInputStream extends InputStream {
*/
@Override
public int read(byte[] b, int offset, int length) throws IOException {
int bytesRead = mIn.read(b, offset, length);
int bytesRead = super.read(b, offset, length);
int copyBytes = bytesRead;
while (copyBytes > 0) {
logRaw((char)b[offset]);
logRaw(b[offset] & 0xFF);
copyBytes--;
offset++;
}
@ -74,15 +82,29 @@ public class LoggingInputStream extends InputStream {
* Write and clear the buffer
*/
private void logRaw(int oneByte) {
if (oneByte == '\r' || oneByte == '\n') {
if (mBufferDirty) {
Log.d(Email.LOG_TAG, mSb.toString());
mSb = new StringBuilder(LINE_TAG);
mBufferDirty = false;
}
} else {
if (oneByte == '\r') {
// Don't log.
} else if (oneByte == '\n') {
flushLog();
} else if (0x20 <= oneByte && oneByte <= 0x7e) { // Printable ASCII.
mSb.append((char)oneByte);
mBufferDirty = true;
} else {
// email protocols are supposed to be all 7bits, but there are wrong implementations
// that do send 8 bit characters...
mSb.append("\\x" + Utility.byteToHex(oneByte));
}
}
private void flushLog() {
if (mDumpEmptyLines || (mSb.length() > mTag.length())) {
Log.d(Email.LOG_TAG, mSb.toString());
initBuffer();
}
}
@Override
public void close() throws IOException {
super.close();
flushLog();
}
}

View File

@ -222,9 +222,7 @@ public class EmailSyncAdapter extends AbstractSyncAdapter {
byte[] bytes = Base64.decode(guid, Base64.DEFAULT);
// Then go through the bytes and write out the hex values as characters
for (byte b: bytes) {
int unsignedByte = (b < 0) ? b + 256 : b;
sb.append("0123456789ABCDEF".charAt(unsignedByte >> 4));
sb.append("0123456789ABCDEF".charAt(unsignedByte & 0xF));
Utility.byteToHex(sb, b);
}
packedString.put(MeetingInfo.MEETING_UID, sb.toString());
break;

View File

@ -19,14 +19,17 @@
package org.apache.james.mime4j;
import com.android.email.Email;
import com.android.email.mail.transport.LoggingInputStream;
import org.apache.james.mime4j.decoder.Base64InputStream;
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;
import java.util.LinkedList;
import org.apache.james.mime4j.decoder.Base64InputStream;
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
/**
* <p>
* Parses MIME (or RFC822) message streams of bytes or characters and reports
@ -49,7 +52,10 @@ import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;
*/
public class MimeStreamParser {
private static final Log log = LogFactory.getLog(MimeStreamParser.class);
// STOPSHIP - DO NOT RELEASE AS 'TRUE'
private static final boolean DEBUG_LOG_MESSAGE = false;
private static BitSet fieldChars = null;
private RootInputStream rootStream = null;
@ -80,6 +86,9 @@ public class MimeStreamParser {
* @throws IOException on I/O errors.
*/
public void parse(InputStream is) throws IOException {
if (DEBUG_LOG_MESSAGE && Email.DEBUG) {
is = new LoggingInputStream(is, "MIME", true);
}
rootStream = new RootInputStream(is);
parseMessage(rootStream);
}

View File

@ -142,4 +142,12 @@ public class UtilityUnitTests extends AndroidTestCase {
assertFalse(Integer.toString(i), Utility.isFirstUtf8Byte(bytes[i]));
}
}
public void testByteToHex() {
for (int i = 0; i <= 0xFF; i++) {
String hex = Utility.byteToHex((byte) i);
assertEquals("val=" + i, 2, hex.length());
assertEquals("val=" + i, i, Integer.parseInt(hex, 16));
}
}
}