1 package org.kite9.framework.common;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.io.Reader;
8 import java.io.Writer;
9
10 public class RepositoryHelp {
11
12 public static File prepareFileName(String id, String filename, String baseDir, boolean createDirs) {
13 File f = new File(baseDir);
14
15 if (createDirs) {
16 f.mkdirs();
17 }
18
19 String full = id.replace(".", "/");
20 f = new File(f, full);
21 if (createDirs) {
22 f.mkdirs();
23 }
24
25 if (filename != null) {
26 File f3 = new File(f, filename);
27 return f3;
28 } else {
29 return f;
30 }
31 }
32
33 public static void streamCopy(InputStream zis, OutputStream fos, boolean closeOs) throws IOException {
34 try {
35 byte[] buffer = new byte[2000];
36 int amt = 0;
37 while (amt != -1) {
38 fos.write(buffer, 0, amt);
39 amt = zis.read(buffer);
40 }
41 } finally {
42 try {
43 if (closeOs)
44 fos.close();
45 } catch (IOException e) {
46 }
47 }
48 }
49
50 public static void streamCopy(Reader zis, Writer fos, boolean closeOs) throws IOException {
51 try {
52 char[] buffer = new char[2000];
53 int amt = 0;
54 while (amt != -1) {
55 fos.write(buffer, 0, amt);
56 amt = zis.read(buffer);
57 }
58 } finally {
59 try {
60 if (closeOs)
61 fos.close();
62 } catch (IOException e) {
63 }
64 }
65 }
66
67 }