1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.email.mail.transport; 18 19 import com.android.email.Email; 20 21 import android.util.Log; 22 23 import java.io.IOException; 24 import java.io.InputStream; 25 26 /** 27 * Simple class used for debugging only that affords us a view of the raw IMAP or POP3 stream, 28 * in addition to the tokenized version. 29 * 30 * Use of this class *MUST* be restricted to logging-enabled situations only. 31 */ 32 public class LoggingInputStream extends InputStream { 33 34 InputStream mIn; 35 StringBuilder mSb; 36 boolean mBufferDirty; 37 38 private final String LINE_TAG = "RAW "; 39 LoggingInputStream(InputStream in)40 public LoggingInputStream(InputStream in) { 41 super(); 42 mIn = in; 43 mSb = new StringBuilder(LINE_TAG); 44 mBufferDirty = false; 45 } 46 47 /** 48 * Collect chars as read, and log them when EOL reached. 49 */ 50 @Override read()51 public int read() throws IOException { 52 int oneByte = mIn.read(); 53 logRaw(oneByte); 54 return oneByte; 55 } 56 57 /** 58 * Collect chars as read, and log them when EOL reached. 59 */ 60 @Override read(byte[] b, int offset, int length)61 public int read(byte[] b, int offset, int length) throws IOException { 62 int bytesRead = mIn.read(b, offset, length); 63 int copyBytes = bytesRead; 64 while (copyBytes > 0) { 65 logRaw((char)b[offset]); 66 copyBytes--; 67 offset++; 68 } 69 70 return bytesRead; 71 } 72 73 /** 74 * Write and clear the buffer 75 */ logRaw(int oneByte)76 private void logRaw(int oneByte) { 77 if (oneByte == '\r' || oneByte == '\n') { 78 if (mBufferDirty) { 79 Log.d(Email.LOG_TAG, mSb.toString()); 80 mSb = new StringBuilder(LINE_TAG); 81 mBufferDirty = false; 82 } 83 } else { 84 mSb.append((char)oneByte); 85 mBufferDirty = true; 86 } 87 } 88 } 89