KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > CloneStreamMaster


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.io;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * The model that needs to be instanciated if you want to create
31  * CloneInputStreams from a master.
32  *
33  * @author Timo Stich <tstich@users.sourceforge.net>
34  */

35 public class CloneStreamMaster {
36     private static int uid = 0;
37
38     private InputStream JavaDoc master;
39
40     private int nextId;
41
42     private List JavaDoc<InputStream JavaDoc> streamList;
43
44     private File JavaDoc tempFile;
45
46     private byte[] buffer;
47
48     private int openClones;
49
50     private boolean usesFile;
51
52     private int size;
53
54     /**
55      * Constructs a CloneStreamMaster. Note that the master must NOT be read
56      * from after the construction!
57      *
58      * @param master
59      */

60     public CloneStreamMaster(InputStream JavaDoc master) throws IOException JavaDoc {
61         super();
62         this.master = master;
63
64         streamList = new ArrayList JavaDoc<InputStream JavaDoc>(2);
65
66         if (this.master.available() > 51200) {
67             tempFile = File.createTempFile("columba-stream-clone" + (uid++),
68                     ".tmp");
69
70             // make sure file is deleted automatically when closing VM
71
tempFile.deleteOnExit();
72
73             FileOutputStream JavaDoc tempOut = new FileOutputStream JavaDoc(tempFile);
74
75             size = (int) StreamUtils.streamCopy(master, tempOut);
76
77             tempOut.close();
78
79             usesFile = true;
80         } else {
81             ByteArrayOutputStream JavaDoc tempOut = new ByteArrayOutputStream JavaDoc();
82
83             size = (int) StreamUtils.streamCopy(master, tempOut);
84             tempOut.close();
85
86             buffer = tempOut.toByteArray();
87             usesFile = false;
88         }
89
90         this.master.close();
91     }
92
93     /**
94      * Gets a new clone of the master.
95      *
96      * @return Clone of the master
97      */

98     public CloneInputStream getClone() {
99         if (usesFile) {
100             try {
101                 // add a new inputstream to read from
102
streamList.add(new FileInputStream JavaDoc(tempFile));
103             } catch (FileNotFoundException JavaDoc e) {
104                 e.printStackTrace();
105
106                 // only if tempfile was corrupted
107
}
108         } else {
109             streamList.add(new ByteArrayInputStream JavaDoc(buffer));
110         }
111
112         openClones++;
113
114         return new CloneInputStream(this, nextId++);
115     }
116
117     public int read(int id) throws IOException JavaDoc {
118         return streamList.get(id).read();
119     }
120
121     public int read(int id, byte[] out, int offset, int length)
122             throws IOException JavaDoc {
123         return streamList.get(id).read(out, offset, length);
124     }
125
126     /**
127      * @return
128      */

129     public int available() throws IOException JavaDoc {
130         return size;
131     }
132
133     /**
134      * {@inheritDoc}
135      */

136     @Override JavaDoc
137     protected void finalize() throws Throwable JavaDoc {
138         super.finalize();
139
140         if (usesFile) {
141             // Delete the tempfile immedietly
142
tempFile.delete();
143         }
144     }
145
146     /**
147      * @param id
148      */

149     public void close(int id) throws IOException JavaDoc {
150         streamList.get(id).close();
151     }
152 }
153
Popular Tags