KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > bpe > util > FileSystemJarInputStream


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.bpe.util;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.PipedInputStream JavaDoc;
23 import java.io.PipedOutputStream JavaDoc;
24 import java.util.jar.JarOutputStream JavaDoc;
25
26 import org.apache.servicemix.jbi.util.FileUtil;
27
28 public class FileSystemJarInputStream extends InputStream JavaDoc implements Runnable JavaDoc {
29
30     private File JavaDoc root;
31     private PipedInputStream JavaDoc input;
32     private PipedOutputStream JavaDoc output;
33     private Thread JavaDoc runner;
34     private IOException JavaDoc exception;
35     
36     public FileSystemJarInputStream(File JavaDoc root) throws IOException JavaDoc {
37         this.root = root;
38         input = new PipedInputStream JavaDoc();
39         output = new PipedOutputStream JavaDoc(input);
40     }
41
42     public int read() throws IOException JavaDoc {
43         if (runner == null) {
44             runner = new Thread JavaDoc(this);
45             runner.setDaemon(true);
46             runner.start();
47         }
48         if (exception != null) {
49             throw exception;
50         }
51         return input.read();
52     }
53
54     public void run() {
55         try {
56             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(output);
57             FileUtil.zipDir(root.getAbsolutePath(), jos, "");
58             jos.close();
59         } catch (IOException JavaDoc e) {
60             exception = e;
61             try {
62                 output.close();
63             } catch (IOException JavaDoc e2) {
64                 e2.printStackTrace();
65             }
66         }
67     }
68
69 }
70
Popular Tags