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.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25
26import org.apache.commons.io.IOUtils;
27//BEGIN android-changed: Stubbing out logging
28import org.apache.james.mime4j.Log;
29import org.apache.james.mime4j.LogFactory;
30//END android-changed
31import org.apache.james.mime4j.util.TempFile;
32import org.apache.james.mime4j.util.TempPath;
33import org.apache.james.mime4j.util.TempStorage;
34
35
36/**
37 * Binary body backed by a {@link org.apache.james.mime4j.util.TempFile}.
38 *
39 *
40 * @version $Id: TempFileBinaryBody.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $
41 */
42class TempFileBinaryBody extends AbstractBody implements BinaryBody {
43    private static Log log = LogFactory.getLog(TempFileBinaryBody.class);
44
45    private Entity parent = null;
46    private TempFile tempFile = null;
47
48    /**
49     * Use the given InputStream to build the TemporyFileBinaryBody
50     *
51     * @param is the InputStream to use as source
52     * @throws IOException
53     */
54    public TempFileBinaryBody(InputStream is) throws IOException {
55
56        TempPath tempPath = TempStorage.getInstance().getRootTempPath();
57        tempFile = tempPath.createTempFile("attachment", ".bin");
58
59        OutputStream out = tempFile.getOutputStream();
60        IOUtils.copy(is, out);
61        out.close();
62    }
63
64    /**
65     * @see org.apache.james.mime4j.message.AbstractBody#getParent()
66     */
67    public Entity getParent() {
68        return parent;
69    }
70
71    /**
72     * @see org.apache.james.mime4j.message.AbstractBody#setParent(org.apache.james.mime4j.message.Entity)
73     */
74    public void setParent(Entity parent) {
75        this.parent = parent;
76    }
77
78    /**
79     * @see org.apache.james.mime4j.message.BinaryBody#getInputStream()
80     */
81    public InputStream getInputStream() throws IOException {
82        return tempFile.getInputStream();
83    }
84
85    /**
86     * @see org.apache.james.mime4j.message.Body#writeTo(java.io.OutputStream)
87     */
88    public void writeTo(OutputStream out) throws IOException {
89	IOUtils.copy(getInputStream(),out);
90    }
91}
92