KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
39 import java.io.IOException JavaDoc;
40
41 /**
42  * Source that wraps a File.
43  *
44  * @author tstich
45  *
46  */

47 public class FileSource implements Source {
48
49     private int start, end;
50     private int pos;
51     
52     private FileSourceModel model;
53
54
55     /**
56      * Constructs the FileSource.
57      *
58      * @param file
59      * @throws IOException
60      */

61     public FileSource( File JavaDoc file ) throws IOException JavaDoc {
62         model = new FileSourceModel( file );
63         model.incReferences();
64         start = 0;
65         end = model.length();
66         pos = 0;
67     }
68     
69     protected FileSource( FileSource parent ) {
70         this.model = parent.model;
71         this.model.incReferences();
72         this.start = parent.start;
73         this.end = parent.end;
74         this.pos = 0;
75     }
76
77     /**
78      * @see org.columba.ristretto.io.Source#fromActualPosition()
79      */

80     public Source fromActualPosition() {
81         FileSource subsource = new FileSource( this );
82         subsource.start = start + pos;
83
84         return subsource;
85     }
86
87     /**
88      * @see org.columba.ristretto.io.Source#getPosition()
89      */

90     public int getPosition() {
91         return pos;
92     }
93
94     /**
95      * @see org.columba.ristretto.io.Source#seek(int)
96      */

97     public void seek(int position) throws IOException JavaDoc {
98         pos = position;
99     }
100
101     /**
102      * @see org.columba.ristretto.io.Source#next()
103      */

104     public char next() throws IOException JavaDoc {
105         return model.get(start + pos++);
106     }
107
108     /**
109      * @see java.lang.CharSequence#length()
110      */

111     public int length() {
112         return end - start;
113     }
114
115     /**
116      * @see java.lang.CharSequence#charAt(int)
117      */

118     public char charAt(int arg0){
119         try {
120             return model.get(start + arg0);
121         } catch (IOException JavaDoc e) {
122             e.printStackTrace();
123             return 0;
124         }
125     }
126
127     /**
128      * @see org.columba.ristretto.io.Source#subSource(int, int)
129      */

130     public Source subSource(int arg0, int arg1) {
131         FileSource subsource = new FileSource( this );
132         subsource.start = start + arg0;
133         subsource.end = start + arg1;
134         return subsource;
135     }
136
137     /**
138      * @see java.lang.CharSequence#subSequence(int, int)
139      */

140     public CharSequence JavaDoc subSequence(int arg0, int arg1) {
141         if( arg1 - arg0 < 1024) {
142             StringBuffer JavaDoc result = new StringBuffer JavaDoc( arg1- arg0);
143             for( int i=arg0; i<arg1; i++ ) {
144                 result.append(charAt(i));
145             }
146             return result;
147         } else {
148             return subSource( arg0, arg1);
149         }
150     }
151     
152     /**
153      * @see java.lang.Object#toString()
154      */

155     public String JavaDoc toString() {
156         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(length());
157         for( int i=0; i < length(); i++) {
158             buffer.append(charAt(i));
159         }
160         return buffer.toString();
161     }
162
163
164
165     /**
166      * @see org.columba.ristretto.io.Source#isEOF()
167      */

168     public boolean isEOF() {
169         return pos == end;
170     }
171     
172     
173
174     /**
175      * @see java.lang.Object#finalize()
176      */

177     protected void finalize() throws Throwable JavaDoc {
178         super.finalize();
179         if( model != null ) {
180             model.decReferences();
181             model = null;
182         }
183     }
184     /**
185      * @see org.columba.ristretto.io.Source#close()
186      */

187     public void close() throws IOException JavaDoc {
188         if( model != null ) {
189             model.decReferences();
190             model = null;
191         }
192     }
193
194     /**
195      * @see org.columba.ristretto.io.Source#deepClose()
196      */

197     public void deepClose() throws IOException JavaDoc {
198         if ( model != null)
199             model.close();
200         model = null;
201     }
202     
203     /**
204      * Get the File.
205      *
206      * @return file
207      */

208     public File JavaDoc getFile() {
209         return model.getFile();
210     }
211 }
212
Popular Tags