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