|
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: String pool for DOM Externalizer. |
|
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.util.Vector; |
|
24 |
|
25 /** |
|
26 * String pool is a set of strings where a string is only listed once. |
|
27 */ |
|
28 public class StringPool |
|
29 { |
|
30 |
|
31 // The pool for storing the strings |
|
32 private Vector iStringPool; |
|
33 |
|
34 /** |
|
35 * Constructor. |
|
36 */ |
|
37 public StringPool() |
|
38 { |
|
39 iStringPool = new Vector(); |
|
40 } |
|
41 |
|
42 /** |
|
43 * Add a string to the pool. Each string is added only once. |
|
44 * @param aString String to add |
|
45 * @return Index in the pool |
|
46 */ |
|
47 public int addString( String aString ) |
|
48 { |
|
49 if( aString == null ) |
|
50 { |
|
51 aString = ""; |
|
52 } |
|
53 if( !iStringPool.contains( aString ) ) |
|
54 { |
|
55 iStringPool.addElement( aString ); |
|
56 } |
|
57 return iStringPool.lastIndexOf( aString ); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Get a string from the pool. |
|
62 * @param aIndex Index number of the string |
|
63 * @return The requested string |
|
64 */ |
|
65 public String getString( int aIndex ) |
|
66 { |
|
67 return ( String ) iStringPool.elementAt( aIndex ); |
|
68 } |
|
69 |
|
70 |
|
71 /** |
|
72 * Get string pool contents in a byte array. |
|
73 * @return String pool contents in a byte array |
|
74 * @throws IOException if writing to a stream fails |
|
75 * @throws ODTException if writing to a stream fails |
|
76 */ |
|
77 public byte[] toByteArray() throws IOException, ODTException |
|
78 { |
|
79 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
80 ODTDataOutputStream dos = new ODTDataOutputStream( baos ); |
|
81 int count = iStringPool.size(); |
|
82 byte[] result = null; |
|
83 |
|
84 try |
|
85 { |
|
86 // Write string count and each string to the stream |
|
87 dos.writeInt16( count ); |
|
88 for( int i = 0; i < count; i++ ) |
|
89 { |
|
90 String string = ( String ) iStringPool.elementAt( i ); |
|
91 dos.writeInt16( string.length() ); |
|
92 dos.writeString8( string ); |
|
93 } |
|
94 |
|
95 result = baos.toByteArray(); |
|
96 } |
|
97 finally |
|
98 { |
|
99 if( dos != null ) |
|
100 { |
|
101 dos.close(); |
|
102 } |
|
103 if( baos != null ) |
|
104 { |
|
105 baos.close(); |
|
106 } |
|
107 } |
|
108 |
|
109 return result; |
|
110 } |
|
111 |
|
112 } |