|
1 /* |
|
2 * Copyright (c) 2007 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: File operation for storing an ODT file to the file system |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.tools.themeinstaller.defrep.operations; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.FileOutputStream; |
|
23 import java.io.IOException; |
|
24 import java.io.InputStream; |
|
25 |
|
26 /** |
|
27 * Operation for storing data to the file system. |
|
28 */ |
|
29 public class StoreOperation extends FileOperation |
|
30 { |
|
31 |
|
32 // CONSTANTS |
|
33 private static final int READ_BUFFER_SIZE = 1024; |
|
34 |
|
35 // Input stream for data input |
|
36 private InputStream iStream; |
|
37 |
|
38 /** |
|
39 * Create a store operation |
|
40 * |
|
41 * @param aDestination Destination file |
|
42 * @param aStream Input stream containing the data |
|
43 */ |
|
44 public StoreOperation( File aDestination, InputStream aStream ) |
|
45 { |
|
46 iFile = aDestination; |
|
47 iStream = aStream; |
|
48 } |
|
49 |
|
50 public void run() |
|
51 { |
|
52 int error = FileOperationEvent.UNKNOWN_ERROR; |
|
53 |
|
54 try |
|
55 { |
|
56 doStoreOperation(); |
|
57 error = FileOperationEvent.OPERATION_SUCCESSFUL; |
|
58 } |
|
59 catch ( IOException ioe ) |
|
60 { |
|
61 error = FileOperationEvent.IO_ERROR; |
|
62 } |
|
63 |
|
64 super.finished( iFile, error ); |
|
65 } |
|
66 |
|
67 private void doStoreOperation() |
|
68 throws IOException |
|
69 { |
|
70 // Overwrite the existing file, if any |
|
71 if( iFile.exists() ) |
|
72 { |
|
73 iFile.delete(); |
|
74 } |
|
75 |
|
76 // Create the required directory structure for the destination file |
|
77 // (ignore return value) |
|
78 FileOperationUtils.createDirs( iFile ); |
|
79 |
|
80 FileOutputStream output = null; |
|
81 |
|
82 try |
|
83 { |
|
84 output = new FileOutputStream( iFile ); |
|
85 |
|
86 // Create a buffer for the data transfer |
|
87 byte[] buffer = new byte[ READ_BUFFER_SIZE ]; |
|
88 int i = 0; |
|
89 |
|
90 // Read data to the buffer and write it to the output stream until |
|
91 // the whole stream is processed |
|
92 while( ( i = iStream.read( buffer ) ) != -1 ) |
|
93 { |
|
94 output.write( buffer, 0, i ); |
|
95 } |
|
96 } |
|
97 finally |
|
98 { |
|
99 if( output != null ) |
|
100 { |
|
101 output.close(); |
|
102 } |
|
103 } |
|
104 } |
|
105 |
|
106 } |