1 package org.apache.fulcrum.jce.crypto;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37 public final class StreamUtil
38 {
39
40 private static final int BUFFER_SIZE = 1024;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public static InputStream createInputStream( Object source )
59 throws IOException
60 {
61 InputStream is;
62
63
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
102
103
104
105
106
107
108
109
110
111
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
144
145
146
147
148
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
173
174
175
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 }