KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > io > ConcatenatedSource


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.io;
37
38 import java.io.IOException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * Source that concats the underlying Sources.
43  *
44  * @author tstich
45  *
46  */

47 public class ConcatenatedSource implements Source {
48
49     ArrayList JavaDoc sources;
50     ArrayList JavaDoc nextSourceBegin;
51     int position;
52     int length;
53     int sourceIndex;
54     /**
55      * Constructs an empty ConcatenatedSource.
56      */

57     public ConcatenatedSource() {
58         sources = new ArrayList JavaDoc();
59         nextSourceBegin = new ArrayList JavaDoc();
60         nextSourceBegin.add(new Integer JavaDoc(0));
61         position = 0;
62         length = 0;
63         sourceIndex = 0;
64     }
65
66     /**
67      * Add the Source to the end.
68      *
69      * @param source
70      */

71     public void addSource( Source source ) {
72         sources.add( source );
73         length += source.length();
74         nextSourceBegin.add( new Integer JavaDoc(length) );
75     }
76
77     /**
78      * @see org.columba.ristretto.io.Source#fromActualPosition()
79      */

80     public Source fromActualPosition() {
81         ConcatenatedSource newsource = new ConcatenatedSource();
82         newsource.addSource(((Source)sources.get(sourceIndex)).fromActualPosition());
83         for( int i=sourceIndex+1; i<sources.size(); i++) {
84             newsource.addSource((Source)sources.get(i));
85         }
86         return newsource;
87     }
88
89     /**
90      * @see org.columba.ristretto.io.Source#getPosition()
91      */

92     public int getPosition() {
93         return position;
94     }
95
96     /**
97      * @see org.columba.ristretto.io.Source#isEOF()
98      */

99     public boolean isEOF() {
100         return position == length;
101     }
102
103     /**
104      * @see org.columba.ristretto.io.Source#next()
105      */

106     public char next() throws IOException JavaDoc {
107         if( ((Source)sources.get(sourceIndex)).isEOF() ) {
108             sourceIndex++;
109         }
110         
111         return ((Source)sources.get(sourceIndex)).next();
112     }
113
114     /**
115      * @see org.columba.ristretto.io.Source#seek(int)
116      */

117     public void seek(int arg0) throws IOException JavaDoc {
118         // check if position is in this source
119
if( !(((Integer JavaDoc)nextSourceBegin.get(sourceIndex)).intValue() < arg0 && arg0 < ((Integer JavaDoc)nextSourceBegin.get(sourceIndex+1)).intValue()) ) {
120             // find the source
121
sourceIndex = 0;
122             while( !(((Integer JavaDoc)nextSourceBegin.get(sourceIndex)).intValue() <= arg0 && arg0 < ((Integer JavaDoc)nextSourceBegin.get(sourceIndex+1)).intValue()) ) {
123                 sourceIndex++;
124             }
125         }
126         int posInSource = arg0 - ((Integer JavaDoc)nextSourceBegin.get(sourceIndex)).intValue();
127         position = arg0;
128         
129         ((Source)sources.get(sourceIndex)).seek(posInSource);
130     }
131
132     /**
133      * @see java.lang.CharSequence#charAt(int)
134      */

135     public char charAt(int arg0) {
136         try {
137             seek( arg0 );
138         } catch (IOException JavaDoc e) {
139             return (char)0;
140         }
141         return ((Source)sources.get(sourceIndex)).charAt(((Source)sources.get(sourceIndex)).getPosition());
142     }
143
144     /**
145      * @see java.lang.CharSequence#length()
146      */

147     public int length() {
148         return length;
149     }
150
151     /**
152      * @see java.lang.CharSequence#subSequence(int, int)
153      */

154     public CharSequence JavaDoc subSequence(int arg0, int arg1) {
155         if( arg1 - arg0 < 1024) {
156             StringBuffer JavaDoc result = new StringBuffer JavaDoc( arg1- arg0);
157             for( int i=arg0; i<arg1; i++ ) {
158                 result.append(charAt(i));
159             }
160             return result;
161         } else {
162             return subSource( arg0, arg1);
163         }
164     }
165     
166     
167     /**
168      * @see org.columba.ristretto.io.Source#subSource(int, int)
169      */

170     public Source subSource(int arg0, int arg1) {
171         ConcatenatedSource newsource = new ConcatenatedSource();
172     
173         // find the begin source
174
int beginSourceIndex = 0;
175         while( !(((Integer JavaDoc)nextSourceBegin.get(beginSourceIndex)).intValue() <= arg0 && arg0 < ((Integer JavaDoc)nextSourceBegin.get(beginSourceIndex+1)).intValue()) ) {
176             beginSourceIndex++;
177         }
178     
179         int beginPos = arg0 - ((Integer JavaDoc)nextSourceBegin.get(beginSourceIndex)).intValue();
180
181
182         // find the end source
183
int endSourceIndex = 0;
184         while( !(((Integer JavaDoc)nextSourceBegin.get(endSourceIndex)).intValue() <= arg1 && arg1 < ((Integer JavaDoc)nextSourceBegin.get(endSourceIndex+1)).intValue()) ) {
185             endSourceIndex++;
186         }
187     
188         int endPos = arg1 - ((Integer JavaDoc)nextSourceBegin.get(endSourceIndex)).intValue();
189
190         // create newsource
191
if( beginSourceIndex == endSourceIndex ) {
192             newsource.addSource( (Source) ((Source)sources.get(beginSourceIndex)).subSequence( beginPos, endPos ));
193         } else {
194             newsource.addSource( (Source) ((Source)sources.get(beginSourceIndex)).subSequence( beginPos, ((Source)sources.get(beginSourceIndex)).length()) );
195             for( int i=beginSourceIndex+1; i<endSourceIndex-1; i++) {
196                 newsource.addSource( (Source) sources.get(i));
197             }
198             newsource.addSource( (Source) ((Source)sources.get(endSourceIndex)).subSequence( 0, endPos ));
199         }
200
201         return newsource;
202     }
203
204     /**
205      * @see java.lang.Object#toString()
206      */

207     public String JavaDoc toString() {
208         StringBuffer JavaDoc result = new StringBuffer JavaDoc(length);
209         for( int i=0; i<sources.size(); i++) {
210             result.append( sources.get(i).toString() );
211         }
212         return result.toString();
213     }
214
215     /**
216      * @see org.columba.ristretto.io.Source#close()
217      */

218     public void close() throws IOException JavaDoc {
219         for( int i=0; i<sources.size(); i++) {
220             ((Source)sources.get(i)).close();
221         }
222         sources = null;
223     }
224
225     /**
226      * @see org.columba.ristretto.io.Source#deepClose()
227      */

228     public void deepClose() throws IOException JavaDoc {
229         for( int i=0; i<sources.size(); i++) {
230             ((Source)sources.get(i)).deepClose();
231         }
232         sources = null;
233     }
234 }
235
Popular Tags