MemoryBinaryBody.java revision a31cb777eb3c60947a9b955a5e7e8246e33204b1
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.message;
21
22import java.io.ByteArrayInputStream;
23import java.io.ByteArrayOutputStream;
24import java.io.IOException;
25import java.io.InputStream;
26import java.io.OutputStream;
27
28import org.apache.commons.io.IOUtils;
29//BEGIN android-changed: Stubbing out logging
30import org.apache.james.mime4j.Log;
31import org.apache.james.mime4j.LogFactory;
32//END android-changed
33import org.apache.james.mime4j.util.TempPath;
34import org.apache.james.mime4j.util.TempStorage;
35
36
37/**
38 * Binary body backed by a {@link org.apache.james.mime4j.util.TempFile}.
39 *
40 *
41 * @version $Id: TempFileBinaryBody.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
42 */
43class MemoryBinaryBody extends AbstractBody implements BinaryBody {
44    private static Log log = LogFactory.getLog(MemoryBinaryBody.class);
45
46    private Entity parent = null;
47    private byte[] tempFile = null;
48
49    /**
50     * Use the given InputStream to build the TemporyFileBinaryBody
51     *
52     * @param is the InputStream to use as source
53     * @throws IOException
54     */
55    public MemoryBinaryBody(InputStream is) throws IOException {
56
57        TempPath tempPath = TempStorage.getInstance().getRootTempPath();
58
59        ByteArrayOutputStream out = new ByteArrayOutputStream();
60        IOUtils.copy(is, out);
61        out.close();
62        tempFile = out.toByteArray();
63    }
64
65    /**
66     * @see org.apache.james.mime4j.message.AbstractBody#getParent()
67     */
68    public Entity getParent() {
69        return parent;
70    }
71
72    /**
73     * @see org.apache.james.mime4j.message.AbstractBody#setParent(org.apache.james.mime4j.message.Entity)
74     */
75    public void setParent(Entity parent) {
76        this.parent = parent;
77    }
78
79    /**
80     * @see org.apache.james.mime4j.message.BinaryBody#getInputStream()
81     */
82    public InputStream getInputStream() throws IOException {
83        return new ByteArrayInputStream(tempFile);
84    }
85
86    /**
87     * @see org.apache.james.mime4j.message.Body#writeTo(java.io.OutputStream)
88     */
89    public void writeTo(OutputStream out) throws IOException {
90	IOUtils.copy(getInputStream(),out);
91    }
92}
93