1 package org.apache.fulcrum.jce.crypto;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30
31 /**
32 * Helper class to provde generic stream functions.
33 *
34 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
35 */
36
37 public final class StreamUtil
38 {
39 /** the size of the internal buffer to copy streams */
40 private static final int BUFFER_SIZE = 1024;
41
42 /**
43 * Create an input stream supporting the following types
44 *
45 * <ul>
46 * <li>String (using the UTF-8 encoded content)</li>
47 * <li>File</li>
48 * <li>byte[]</li>
49 * <li>char[]</li>
50 * <li>ByteArrayOutputStream</li>
51 * <li>InputStream</li>
52 * </ul>
53 *
54 * @param source the source object
55 * @return the created input stream
56 * @throws java.io.IOException creating the input stream failed
57 */
58 public static InputStream createInputStream( Object source )
59 throws IOException
60 {
61 InputStream is;
62
63 // create an InputStream
64
65 if( source instanceof String )
66 {
67 byte[] content = ((String) source).getBytes("utf-8");
68 is = new ByteArrayInputStream( content );
69 }
70 else if( source instanceof File )
71 {
72 is = new FileInputStream( (File) source );
73 }
74 else if( source instanceof byte[] )
75 {
76 is = new ByteArrayInputStream( (byte[]) source );
77 }
78 else if( source instanceof char[] )
79 {
80 byte[] content = new String((char[])source).getBytes("utf-8");
81 is = new ByteArrayInputStream( content );
82 }
83 else if( source instanceof ByteArrayOutputStream )
84 {
85 byte[] content = ((ByteArrayOutputStream) source).toByteArray();
86 is = new ByteArrayInputStream( content );
87 }
88 else if( source instanceof InputStream )
89 {
90 is = (InputStream) source;
91 }
92 else
93 {
94 throw new IllegalArgumentException("Don't know hot to handle " + source.getClass().getName());
95 }
96
97 return is;
98 }
99
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 boolean success = parentFile.mkdirs();
184 if ( !success )
185 {
186 System.err.println("Error, could not create directory to write parent file");
187 }
188 }
189 }
190 }