001    package org.apache.fulcrum.jce.crypto;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.ByteArrayInputStream;
023    import java.io.ByteArrayOutputStream;
024    import java.io.File;
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    
031    /**
032     * Helper class to provde generic stream functions.
033     *
034     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
035     */
036    
037    public final class StreamUtil
038    {
039        /** the size of the internal buffer to copy streams */
040        private static final int BUFFER_SIZE = 1024;
041    
042        /**
043         * Create an input stream supporting the following types
044         *
045         * <ul>
046         *  <li>String (using the UTF-8 encoded content)</li>
047         *  <li>File</li>
048         *  <li>byte[]</li>
049         *  <li>char[]</li>
050         *  <li>ByteArrayOutputStream</li>
051         *  <li>InputStream</li>
052         * </ul>
053         *
054         * @param source the source object
055         * @return the created input stream
056         * @throws java.io.IOException creating the input stream failed
057         */
058        public static InputStream createInputStream( Object source )
059            throws IOException
060        {
061            InputStream is;
062    
063            // create an InputStream
064    
065            if( source instanceof String )
066            {
067                byte[] content = ((String) source).getBytes("utf-8");
068                is = new ByteArrayInputStream( content );
069            }
070            else if( source instanceof File )
071            {
072                is = new FileInputStream( (File) source );
073            }
074            else if( source instanceof byte[] )
075            {
076                is = new ByteArrayInputStream( (byte[]) source );
077            }
078            else if( source instanceof char[] )
079            {
080                byte[] content = new String((char[])source).getBytes("utf-8");
081                is = new ByteArrayInputStream( content );
082            }
083            else if( source instanceof ByteArrayOutputStream )
084            {
085                byte[] content = ((ByteArrayOutputStream) source).toByteArray();
086                is = new ByteArrayInputStream( content );
087            }
088            else if( source instanceof InputStream )
089            {
090                is = (InputStream) source;
091            }
092            else
093            {
094                throw new IllegalArgumentException("Don't know hot to handle " + source.getClass().getName());
095            }
096    
097            return is;
098        }
099    
100        /**
101         * Create an output stream supporting the following types
102         *
103         * <ul>
104         *  <li>File</li>
105         *  <li>String</li>
106         *  <li>OutputStream</li>
107         * </ul>
108         *
109         * @param target the target object
110         * @return the output stream
111         * @throws java.io.IOException creating the output stream failed
112         */
113        public static OutputStream createOutputStream( Object target )
114            throws IOException
115        {
116            OutputStream os;
117    
118            if( target instanceof File )
119            {
120                File currFile = (File) target;
121                createParentFile(currFile);
122                os = new FileOutputStream(currFile);
123            }
124            else if( target instanceof String )
125            {
126                File currFile = new File((String) target);
127                createParentFile(currFile);
128                os = new FileOutputStream(currFile);
129            }
130            else if( target instanceof OutputStream )
131            {
132                os = (OutputStream) target;
133            }
134            else
135            {
136                throw new IllegalArgumentException("Don't know hot to handle " + target.getClass().getName());
137            }
138    
139            return os;
140        }
141    
142        /**
143         * Pumps the input stream to the output stream.
144         *
145         * @param is the source input stream
146         * @param os the target output stream
147         * @return the number of bytes copied
148         * @throws java.io.IOException the copying failed
149         */
150        public static long copy( InputStream is, OutputStream os )
151            throws IOException
152        {
153            byte[] buf = new byte[BUFFER_SIZE];
154            int n = 0;
155            long total = 0;
156    
157            while ((n = is.read(buf)) > 0)
158            {
159                os.write(buf, 0, n);
160                total += n;
161            }
162    
163            is.close();
164    
165            os.flush();
166            os.close();
167    
168            return total;
169        }
170    
171        /**
172         * Ensure that the parent directories exists before writing to
173         * the file.
174         * 
175         * @param currFile the file to write to
176         */
177        private static void createParentFile(File currFile)
178        {
179            File parentFile = currFile.getParentFile();
180            
181            if((parentFile != null) && !parentFile.exists())
182            {
183                parentFile.mkdirs();
184            }
185        }
186    }