001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter.stream;
018    
019    import java.io.File;
020    import java.io.FileInputStream;
021    import java.io.FileNotFoundException;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    import java.nio.channels.Channels;
026    import java.nio.channels.FileChannel;
027    import java.nio.channels.WritableByteChannel;
028    
029    import org.apache.camel.RuntimeCamelException;
030    import org.apache.camel.StreamCache;
031    import org.apache.camel.util.IOHelper;
032    
033    public class FileInputStreamCache extends InputStream implements StreamCache {
034        private InputStream stream;
035        private File file;
036    
037        public FileInputStreamCache(File file) throws FileNotFoundException {
038            this.file = file;
039            this.stream = null;
040        }
041        
042        @Override
043        public void close() {
044            if (stream != null) {
045                IOHelper.close(stream);
046            }
047        }
048    
049        @Override
050        public void reset() {
051            // reset by closing and creating a new stream based on the file
052            close();
053            // reset by creating a new stream based on the file
054            stream = null;
055            
056            if (!file.exists()) {
057                throw new RuntimeCamelException("Cannot reset stream from file " + file);
058            }
059        }
060    
061        public void writeTo(OutputStream os) throws IOException {
062            if (stream == null) {
063                FileInputStream s = new FileInputStream(file);
064                long len = file.length();
065                WritableByteChannel out;
066                if (os instanceof WritableByteChannel) {
067                    out = (WritableByteChannel)os;
068                } else {
069                    out = Channels.newChannel(os);
070                }
071                FileChannel fc = s.getChannel();
072                long pos = 0;
073                while (pos < len) {
074                    long i = fc.transferTo(pos, len - pos, out);
075                    pos += i;
076                }
077                s.close();
078            } else {
079                IOHelper.copy(getInputStream(), os);
080            }
081        }
082    
083        @Override
084        public int available() throws IOException {
085            return getInputStream().available();
086        }
087    
088        @Override
089        public int read() throws IOException {
090            return getInputStream().read();
091        }
092    
093        protected InputStream getInputStream() throws IOException {
094            if (stream == null) {
095                stream = IOHelper.buffered(new FileInputStream(file));
096            }
097            return stream;
098        }
099    }