|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.IO; |
|
19 using System.Text; |
|
20 using System.Threading; |
|
21 using System.Collections; |
|
22 using SymbianUtils.Tracer; |
|
23 |
|
24 namespace SymbianUtils |
|
25 { |
|
26 public abstract class AsyncTextFileReader : AsyncTextReader |
|
27 { |
|
28 #region Constructors |
|
29 protected AsyncTextFileReader( string aFileName ) |
|
30 : this( aFileName, new AsyncTextReaderPrefix(), null ) |
|
31 { |
|
32 } |
|
33 |
|
34 protected AsyncTextFileReader( string aFileName, ITracer aTracer ) |
|
35 : this( aFileName, new AsyncTextReaderPrefix(), false, aTracer ) |
|
36 { |
|
37 } |
|
38 |
|
39 protected AsyncTextFileReader( string aFileName, bool aRouteBlankLines, ITracer aTracer ) |
|
40 : this( aFileName, new AsyncTextReaderPrefix(), aRouteBlankLines, aTracer ) |
|
41 { |
|
42 } |
|
43 |
|
44 protected AsyncTextFileReader( string aFileName, AsyncTextReaderPrefix aPrefixes ) |
|
45 : this( aFileName, aPrefixes, false, null ) |
|
46 { |
|
47 } |
|
48 |
|
49 protected AsyncTextFileReader( string aFileName, AsyncTextReaderPrefix aPrefixes, ITracer aTracer ) |
|
50 : this( aFileName, aPrefixes, false, aTracer ) |
|
51 { |
|
52 } |
|
53 |
|
54 protected AsyncTextFileReader( string aFileName, AsyncTextReaderPrefix aPrefixes, bool aRouteBlankLines ) |
|
55 : this( aFileName, aPrefixes, aRouteBlankLines, null ) |
|
56 { |
|
57 } |
|
58 |
|
59 protected AsyncTextFileReader( string aFileName, AsyncTextReaderPrefix aPrefixes, bool aRouteBlankLines, ITracer aTracer ) |
|
60 : base( aPrefixes, aRouteBlankLines, aTracer ) |
|
61 { |
|
62 iSourceFileName = aFileName; |
|
63 } |
|
64 #endregion |
|
65 |
|
66 #region From DisposableObject - Cleanup Framework |
|
67 protected override void CleanupManagedResources() |
|
68 { |
|
69 try |
|
70 { |
|
71 Cleanup(); |
|
72 } |
|
73 finally |
|
74 { |
|
75 base.CleanupManagedResources(); |
|
76 } |
|
77 } |
|
78 #endregion |
|
79 |
|
80 #region Properties |
|
81 public string FileName |
|
82 { |
|
83 get |
|
84 { |
|
85 string fileName = string.Empty; |
|
86 // |
|
87 lock( this ) |
|
88 { |
|
89 fileName = iSourceFileName; |
|
90 } |
|
91 // |
|
92 return fileName; |
|
93 } |
|
94 } |
|
95 |
|
96 public long LineNumber |
|
97 { |
|
98 get |
|
99 { |
|
100 long lineNumber = 0; |
|
101 // |
|
102 lock( this ) |
|
103 { |
|
104 lineNumber = iLineNumber; |
|
105 } |
|
106 // |
|
107 return lineNumber; |
|
108 } |
|
109 } |
|
110 #endregion |
|
111 |
|
112 #region From AsyncReaderBase |
|
113 protected override void HandleReadStarted() |
|
114 { |
|
115 FileStream stream = new FileStream( FileName, FileMode.Open, FileAccess.Read, FileShare.Read ); |
|
116 iReader = new StreamReader( stream, Encoding.UTF8, false, 1024 ); |
|
117 |
|
118 // Cache length because this property is VERY expensive to call. |
|
119 if ( iReader.BaseStream != null ) |
|
120 { |
|
121 iSize = iReader.BaseStream.Length; |
|
122 } |
|
123 // |
|
124 base.HandleReadStarted(); |
|
125 } |
|
126 |
|
127 protected override void HandleReadCompleted() |
|
128 { |
|
129 try |
|
130 { |
|
131 Cleanup(); |
|
132 } |
|
133 finally |
|
134 { |
|
135 base.HandleReadCompleted(); |
|
136 } |
|
137 } |
|
138 #endregion |
|
139 |
|
140 #region Abstract reading framework |
|
141 protected override string ProvideReadLine() |
|
142 { |
|
143 ++iLineNumber; |
|
144 return iReader.ReadLine(); |
|
145 } |
|
146 |
|
147 protected override long Size |
|
148 { |
|
149 get |
|
150 { |
|
151 long size = 0; |
|
152 // |
|
153 lock( this ) |
|
154 { |
|
155 size = iSize; |
|
156 } |
|
157 // |
|
158 return size; |
|
159 } |
|
160 } |
|
161 |
|
162 protected override long Position |
|
163 { |
|
164 get |
|
165 { |
|
166 long position = 0; |
|
167 // |
|
168 lock( this ) |
|
169 { |
|
170 if (iReader != null && iReader.BaseStream != null) |
|
171 { |
|
172 position = iReader.BaseStream.Position; |
|
173 } |
|
174 } |
|
175 // |
|
176 return position; |
|
177 } |
|
178 } |
|
179 #endregion |
|
180 |
|
181 #region Internal methods |
|
182 private void Cleanup() |
|
183 { |
|
184 lock( this ) |
|
185 { |
|
186 if ( iReader != null ) |
|
187 { |
|
188 iReader.Close(); |
|
189 iReader = null; |
|
190 } |
|
191 } |
|
192 } |
|
193 #endregion |
|
194 |
|
195 #region Data members |
|
196 private readonly string iSourceFileName; |
|
197 private StreamReader iReader; |
|
198 private long iLineNumber; |
|
199 private long iSize; |
|
200 #endregion |
|
201 } |
|
202 } |