|
1 /* |
|
2 * Copyright (c) 2008 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 |
|
18 package com.nokia.mj.impl.fileutils; |
|
19 |
|
20 import java.io.DataInputStream; |
|
21 import java.io.DataOutputStream; |
|
22 import java.io.IOException; |
|
23 import java.io.InputStream; |
|
24 import java.io.InterruptedIOException; |
|
25 import java.io.OutputStream; |
|
26 |
|
27 import com.nokia.mj.impl.rt.support.ApplicationInfo; |
|
28 import com.nokia.mj.impl.rt.support.ApplicationUtils; |
|
29 import com.nokia.mj.impl.rt.support.Finalizer; |
|
30 import com.nokia.mj.impl.rt.support.Jvm; |
|
31 |
|
32 /** |
|
33 * FileStreamHandler helps in creation of InputStream and OutputStream and |
|
34 * provides data to them from the native side. |
|
35 */ |
|
36 class FileStreamHandler |
|
37 { |
|
38 protected int iNativeHandle; |
|
39 |
|
40 protected FileInputStream iInputStream; |
|
41 |
|
42 protected FileOutputStream iOutputStream; |
|
43 |
|
44 protected DataInputStream iDataInputStream; |
|
45 |
|
46 protected DataOutputStream iDataOutputStream; |
|
47 |
|
48 // Synchronizes close and open operations. |
|
49 protected final Object iCloseOperation; |
|
50 |
|
51 protected boolean iIsClosed; |
|
52 |
|
53 private Finalizer iFinalizer; |
|
54 |
|
55 private FileStreamHandler() |
|
56 { |
|
57 iCloseOperation = new Object(); |
|
58 } |
|
59 |
|
60 protected FileStreamHandler(String aFileName) |
|
61 { |
|
62 iNativeHandle = createNativePeer(aFileName); |
|
63 iCloseOperation = new Object(); |
|
64 iIsClosed = false; |
|
65 iFinalizer = registerFinalize(); |
|
66 } |
|
67 |
|
68 protected int createNativePeer(String aFileName) |
|
69 { |
|
70 return _createNativePeer(aFileName); |
|
71 } |
|
72 |
|
73 /** |
|
74 * Registers with Finalizer to call a method when the object gets collected |
|
75 * by GC |
|
76 * |
|
77 * @return Finalizer object that will be notified when GC happens |
|
78 */ |
|
79 private Finalizer registerFinalize() |
|
80 { |
|
81 return new Finalizer() |
|
82 { |
|
83 public void finalizeImpl() |
|
84 { |
|
85 doFinalize(); |
|
86 } |
|
87 }; |
|
88 } |
|
89 |
|
90 private synchronized void doFinalize() |
|
91 { |
|
92 _dispose(iNativeHandle); |
|
93 } |
|
94 |
|
95 protected void stopRead() |
|
96 { |
|
97 synchronized (iCloseOperation) |
|
98 { |
|
99 _stopReading(iNativeHandle); |
|
100 closeInputStreams(); |
|
101 } |
|
102 } |
|
103 |
|
104 protected void stopWrite() |
|
105 { |
|
106 synchronized (iCloseOperation) |
|
107 { |
|
108 closeOutputStreams(); |
|
109 _stopWriting(iNativeHandle); |
|
110 } |
|
111 } |
|
112 |
|
113 protected InputStream openInputStream() throws IOException |
|
114 { |
|
115 synchronized (iCloseOperation) |
|
116 { |
|
117 if (null == iInputStream) |
|
118 { |
|
119 iInputStream = new FileInputStream(this); |
|
120 } |
|
121 else |
|
122 { |
|
123 throw new IOException("InputStream already open"); |
|
124 } |
|
125 } |
|
126 return iInputStream; |
|
127 } |
|
128 |
|
129 protected DataInputStream openDataInputStream() throws IOException |
|
130 { |
|
131 synchronized (iCloseOperation) |
|
132 { |
|
133 if (null == iDataInputStream) |
|
134 { |
|
135 iDataInputStream = new DataInputStream(openInputStream()); |
|
136 } |
|
137 else |
|
138 { // TCK2 compliance |
|
139 throw new IOException("InputStream already open"); |
|
140 } |
|
141 } |
|
142 return iDataInputStream; |
|
143 } |
|
144 |
|
145 /** |
|
146 * This method is called by InputStreamBase whenever it needs to fill its |
|
147 * internal buffer with the next block of data. |
|
148 * |
|
149 * @param aBuffer |
|
150 * is the buffer that needs to be filled with data. |
|
151 * @return the number of bytes read |
|
152 * @throws InterruptedIOException |
|
153 * in case connection was closed while read was in progress. |
|
154 * @throws IOException |
|
155 * in case where there were problems during read. |
|
156 */ |
|
157 protected synchronized int readBytes(byte[] aBuffer, int aOffset, |
|
158 int aLength) throws InterruptedIOException, IOException |
|
159 { |
|
160 return _readData(iNativeHandle, aBuffer, aOffset, aLength); |
|
161 } |
|
162 |
|
163 /** |
|
164 * Closes all input streams |
|
165 * |
|
166 */ |
|
167 protected void closeInputStreams() |
|
168 { |
|
169 synchronized (iCloseOperation) |
|
170 { |
|
171 try |
|
172 { |
|
173 if (iInputStream != null) |
|
174 { |
|
175 iInputStream.close(); |
|
176 iInputStream = null; |
|
177 } |
|
178 |
|
179 if (iDataInputStream != null) |
|
180 { |
|
181 iDataInputStream.close(); |
|
182 iDataInputStream = null; |
|
183 } |
|
184 } |
|
185 catch (IOException ex) |
|
186 { |
|
187 // Do nothing. Deleting Streams anyhow |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 protected OutputStream openOutputStream() throws IOException |
|
193 { |
|
194 synchronized (iCloseOperation) |
|
195 { |
|
196 if (null == iOutputStream) |
|
197 { |
|
198 iOutputStream = new FileOutputStream(this); |
|
199 } |
|
200 else |
|
201 { // TCK2 compliance |
|
202 throw new IOException("OutputStream already open"); |
|
203 } |
|
204 } |
|
205 return iOutputStream; |
|
206 } |
|
207 |
|
208 protected DataOutputStream openDataOutputStream() throws IOException |
|
209 { |
|
210 synchronized (iCloseOperation) |
|
211 { |
|
212 if (null == iDataOutputStream) |
|
213 { |
|
214 iDataOutputStream = new DataOutputStream(openOutputStream()); |
|
215 } |
|
216 else |
|
217 { |
|
218 throw new IOException("OutputStream already open"); |
|
219 } |
|
220 } |
|
221 return iDataOutputStream; |
|
222 } |
|
223 |
|
224 /** |
|
225 * This method is called by OutputStreamBase whenever its internal buffer |
|
226 * overruns or when flush is called or when output stream is closed. |
|
227 * |
|
228 * @param aBytes |
|
229 * the buffer that has to be written to native. |
|
230 * @param aOffset |
|
231 * offset from which data needs to be written in aBytes. |
|
232 * @param aLength |
|
233 * the length of data from offset that has to be written. |
|
234 * @throws IOException |
|
235 * in case write operation fails. |
|
236 */ |
|
237 protected synchronized void writeBytes(byte[] aBytes, int aOffset, |
|
238 int aLength) throws IOException |
|
239 { |
|
240 _writeData(iNativeHandle, aBytes, aOffset, aLength); |
|
241 } |
|
242 |
|
243 /** |
|
244 * Closes all output streams. |
|
245 * |
|
246 */ |
|
247 protected void closeOutputStreams() |
|
248 { |
|
249 synchronized (iCloseOperation) |
|
250 { |
|
251 try |
|
252 { |
|
253 if (iOutputStream != null) |
|
254 { |
|
255 iOutputStream.close(); |
|
256 iOutputStream = null; |
|
257 } |
|
258 |
|
259 if (iDataOutputStream != null) |
|
260 { |
|
261 iDataOutputStream.close(); |
|
262 iDataOutputStream = null; |
|
263 } |
|
264 } |
|
265 catch (IOException ex) |
|
266 { |
|
267 // Do nothing. Deleting Streams anyhow |
|
268 } |
|
269 } |
|
270 } |
|
271 |
|
272 /** |
|
273 * Checks the validity of the parameters passed. |
|
274 * |
|
275 * @param aBytes |
|
276 * byte array with data |
|
277 * @param aOffset |
|
278 * offset from which operations have to start |
|
279 * @param aLength |
|
280 * length of data from offset |
|
281 */ |
|
282 static void checkIOParams(byte[] aBytes, int aOffset, int aLength) |
|
283 { |
|
284 if (aOffset < 0 || aLength < 0 || (aOffset + aLength) > aBytes.length) |
|
285 { |
|
286 throw new IndexOutOfBoundsException(); |
|
287 } |
|
288 } |
|
289 |
|
290 protected void closeFileStream() |
|
291 { |
|
292 _closeFileStream(iNativeHandle); |
|
293 } |
|
294 |
|
295 protected void openFileForReading() |
|
296 { |
|
297 _openFileForReading(iNativeHandle); |
|
298 } |
|
299 |
|
300 protected void openFileForWriting(long aOffset) |
|
301 { |
|
302 _openFileForWriting(iNativeHandle, aOffset); |
|
303 } |
|
304 |
|
305 /** |
|
306 * Moves the current read position on the native side forward and back. |
|
307 * NOTE: Method needs to be called by InputStream only. Native only sets |
|
308 * read position forward or backward. |
|
309 * |
|
310 * @param aOffset |
|
311 * relative offset to the current native position. Positive moves |
|
312 * the offset forward and negative moves it back. |
|
313 * @return actual number of bytes that can be skipped. |
|
314 */ |
|
315 protected long skip(long aOffset) |
|
316 { |
|
317 return _skip(iNativeHandle, aOffset); |
|
318 } |
|
319 |
|
320 protected long available() |
|
321 { |
|
322 return _available(iNativeHandle); |
|
323 } |
|
324 |
|
325 private native long _available(int aNativePeer); |
|
326 |
|
327 private native long _skip(int aNativePeer, long aOffset); |
|
328 |
|
329 private native int _readData(int aNativePeer, byte[] aBuffer, int aOffset, |
|
330 int aLength); |
|
331 |
|
332 private native void _writeData(int aNativePeer, byte[] aBytes, int aOffset, |
|
333 int aLength); |
|
334 |
|
335 private native void _closeFileStream(int aHandle); |
|
336 |
|
337 private native void _openFileForReading(int aHandle); |
|
338 |
|
339 private native void _openFileForWriting(int aHandle, long aOffset); |
|
340 |
|
341 private native void _dispose(int aNativeHandle); |
|
342 |
|
343 private native void _stopReading(int iNativeHandle); |
|
344 |
|
345 private native void _stopWriting(int iNativeHandle); |
|
346 |
|
347 private native int _createNativePeer(String aFileName); |
|
348 |
|
349 } |