|
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: Implements ODTInputStream |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.tools.themeinstaller.odtconverter; |
|
20 |
|
21 import java.io.ByteArrayOutputStream; |
|
22 import java.io.IOException; |
|
23 import java.io.InputStream; |
|
24 import java.util.Enumeration; |
|
25 |
|
26 /** |
|
27 * Implements ODTInputStream |
|
28 */ |
|
29 public class ODTInputStream extends InputStream |
|
30 { |
|
31 |
|
32 /** ODTDocument */ |
|
33 ODTDocument iODTDocument; |
|
34 |
|
35 /** ByteArrayOutputStream for binary representations */ |
|
36 ByteArrayOutputStream iBaos; |
|
37 |
|
38 /** ODTDataOutputStream for writing data to stream */ |
|
39 ODTDataOutputStream iODTDos; |
|
40 |
|
41 /** byte array for read */ |
|
42 byte[] iByteArray = null; |
|
43 |
|
44 /** index for reading byte array */ |
|
45 int iIndex = 0; |
|
46 |
|
47 /* Literal delimiter is used in separation of theme header and |
|
48 other data in ODT-streaming. */ |
|
49 private static final char DELIM = '#'; |
|
50 |
|
51 // Symbian CDirectFileStore needs these |
|
52 private static final int WRITE_ONCE_FILE_STORE_UID = 268435511; |
|
53 private static final int UNKNOWN_MEDIA_UID = 73066445; |
|
54 |
|
55 /** |
|
56 * Constructor |
|
57 * @param aODTDocument |
|
58 * @throws ODTException |
|
59 */ |
|
60 public ODTInputStream( ODTDocument aODTDocument ) throws ODTException |
|
61 { |
|
62 iODTDocument = aODTDocument; |
|
63 iBaos = new ByteArrayOutputStream(); |
|
64 iODTDos = new ODTDataOutputStream( iBaos ); |
|
65 getBinaryRepresentations(); |
|
66 } |
|
67 |
|
68 /** |
|
69 * gets binary representations of ODTHeader, ODTResources and DOMDocument |
|
70 * @throws ODTException |
|
71 */ |
|
72 private void getBinaryRepresentations() throws ODTException |
|
73 { |
|
74 // first write stuff needed by Symbian CDirectFileStore, then |
|
75 // get and write ODT material |
|
76 try |
|
77 { |
|
78 // write first uid |
|
79 iODTDos.writeInt32( WRITE_ONCE_FILE_STORE_UID ); |
|
80 |
|
81 // write two 0, for keeping stream valid in native side |
|
82 iODTDos.writeInt( 0 ); |
|
83 iODTDos.writeInt( 0 ); |
|
84 |
|
85 // write second uid |
|
86 iODTDos.writeInt32( UNKNOWN_MEDIA_UID ); |
|
87 |
|
88 // write index where ODT header starts |
|
89 iODTDos.writeInt32( 20 ); |
|
90 |
|
91 // get and write ODT header |
|
92 iBaos.write( iODTDocument.getODTHeader().getBinaryODTHeader() ); |
|
93 |
|
94 // write delimiter |
|
95 iODTDos.writeInt16( DELIM ); |
|
96 |
|
97 // write resource count |
|
98 int resCount = iODTDocument.getODTResources().size(); |
|
99 iODTDos.writeInt32( resCount ); |
|
100 |
|
101 // get and write ODTResources |
|
102 for ( Enumeration resources = iODTDocument.getODTResources() |
|
103 .elements(); resources.hasMoreElements(); ) |
|
104 { |
|
105 ODTResource odtResource = ( ODTResource ) resources |
|
106 .nextElement(); |
|
107 iBaos.write( odtResource.getBinaryODTResource() ); |
|
108 } |
|
109 |
|
110 // get and write DOMDocument |
|
111 DOMExternalizer domExt = |
|
112 new DOMExternalizer( iODTDocument.getDOMDocument() ); |
|
113 iBaos.write( domExt.getByteArray() ); |
|
114 } |
|
115 catch( IOException e ) |
|
116 { |
|
117 throw new ODTException( e.getMessage() ); |
|
118 } |
|
119 } |
|
120 |
|
121 /* (non-Javadoc) |
|
122 * @see java.io.InputStream#close() |
|
123 */ |
|
124 public void close() throws IOException |
|
125 { |
|
126 iODTDos.close(); |
|
127 iBaos.close(); |
|
128 } |
|
129 |
|
130 /* (non-Javadoc) |
|
131 * @see java.io.InputStream#read() |
|
132 */ |
|
133 public int read() throws IOException |
|
134 { |
|
135 if( iByteArray == null ) |
|
136 { |
|
137 iByteArray = iBaos.toByteArray(); |
|
138 } |
|
139 if( iIndex >= iByteArray.length ) |
|
140 { |
|
141 iByteArray = null; |
|
142 return -1; |
|
143 } |
|
144 |
|
145 byte b = iByteArray[iIndex++]; |
|
146 // because 0xff byte is -1, which is used as end of stream value, |
|
147 // we must return 0xff int which is 255. |
|
148 if( b == (byte)0xff ) |
|
149 { |
|
150 return 0xff; |
|
151 } |
|
152 return b; |
|
153 } |
|
154 } |