ImapMemoryLiteral.java revision 7e5ba0e1eaee76ab6e6c7ea9362348f660796596
17e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki/* 27e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Copyright (C) 2010 The Android Open Source Project 37e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * 47e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Licensed under the Apache License, Version 2.0 (the "License"); 57e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * you may not use this file except in compliance with the License. 67e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * You may obtain a copy of the License at 77e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * 87e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * http://www.apache.org/licenses/LICENSE-2.0 97e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * 107e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Unless required by applicable law or agreed to in writing, software 117e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * distributed under the License is distributed on an "AS IS" BASIS, 127e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * See the License for the specific language governing permissions and 147e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * limitations under the License. 157e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki */ 167e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 177e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukipackage com.android.email.mail.store.imap; 187e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 197e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.Email; 207e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.FixedLengthInputStream; 217e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport com.android.email.Utility; 227e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 237e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport android.util.Log; 247e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 257e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport java.io.ByteArrayInputStream; 267e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport java.io.IOException; 277e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukiimport java.io.InputStream; 287e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 297e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki/** 307e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki * Subclass of {@link ImapString} used for literals backed by an in-memory byte array. 317e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki */ 327e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onukipublic class ImapMemoryLiteral extends ImapString { 337e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki private final byte[] mData; 347e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 357e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki /* package */ ImapMemoryLiteral(FixedLengthInputStream in) throws IOException { 367e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki // We could use ByteArrayOutputStream and IOUtils.copy, but it'd perform an unnecessary 377e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki // copy.... 387e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki mData = new byte[in.getLength()]; 397e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki int pos = 0; 407e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki while (pos < mData.length) { 417e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki int read = in.read(mData, pos, mData.length - pos); 427e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki if (read < 0) { 437e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki break; 447e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 457e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki pos += read; 467e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 477e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki if (pos != mData.length) { 487e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki Log.w(Email.LOG_TAG, ""); 497e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 507e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 517e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 527e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki @Override 537e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki public String getString() { 547e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki return Utility.fromAscii(mData); 557e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 567e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 577e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki @Override 587e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki public InputStream getAsStream() { 597e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki return new ByteArrayInputStream(mData); 607e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 617e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki 627e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki @Override 637e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki public String toString() { 647e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki return String.format("{%d byte literal(memory)}", mData.length); 657e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki } 667e5ba0e1eaee76ab6e6c7ea9362348f660796596Makoto Onuki} 67