Home | History | Annotate | Download | only in imap
      1 /*
      2  * Copyright (C) 2010 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.store.imap;
     18 
     19 import com.android.email.FixedLengthInputStream;
     20 import com.android.emailcommon.Logging;
     21 import com.android.emailcommon.utility.Utility;
     22 
     23 import android.util.Log;
     24 
     25 import java.io.ByteArrayInputStream;
     26 import java.io.IOException;
     27 import java.io.InputStream;
     28 
     29 /**
     30  * Subclass of {@link ImapString} used for literals backed by an in-memory byte array.
     31  */
     32 public class ImapMemoryLiteral extends ImapString {
     33     private byte[] mData;
     34 
     35     /* package */ ImapMemoryLiteral(FixedLengthInputStream in) throws IOException {
     36         // We could use ByteArrayOutputStream and IOUtils.copy, but it'd perform an unnecessary
     37         // copy....
     38         mData = new byte[in.getLength()];
     39         int pos = 0;
     40         while (pos < mData.length) {
     41             int read = in.read(mData, pos, mData.length - pos);
     42             if (read < 0) {
     43                 break;
     44             }
     45             pos += read;
     46         }
     47         if (pos != mData.length) {
     48             Log.w(Logging.LOG_TAG, "");
     49         }
     50     }
     51 
     52     @Override
     53     public void destroy() {
     54         mData = null;
     55         super.destroy();
     56     }
     57 
     58     @Override
     59     public String getString() {
     60         return Utility.fromAscii(mData);
     61     }
     62 
     63     @Override
     64     public InputStream getAsStream() {
     65         return new ByteArrayInputStream(mData);
     66     }
     67 
     68     @Override
     69     public String toString() {
     70         return String.format("{%d byte literal(memory)}", mData.length);
     71     }
     72 }
     73