upstream version 1.2.2
[debian/freetts] / com / sun / speech / freetts / util / Utilities.java
1 /**
2  * Copyright 2001 Sun Microsystems, Inc.
3  * 
4  * See the file "license.terms" for information on usage and
5  * redistribution of this file, and for a DISCLAIMER OF ALL 
6  * WARRANTIES.
7  */
8 package com.sun.speech.freetts.util;
9
10 import java.io.PrintWriter;
11 import java.net.URL;
12 import java.io.FileInputStream;
13 import java.io.DataInputStream;
14 import java.io.DataOutputStream;
15 import java.nio.ByteBuffer;
16 import java.io.InputStream;
17 import java.io.IOException;
18
19 /**
20  * Provides a set of generic utilities used by freetts.
21  */
22 public class Utilities {
23
24     /**
25      * Avoid construction.
26      */
27     private Utilities() {
28     }
29
30     /**
31      * Returns a string with the given number of
32      * spaces.
33      *
34      * @param padding the number of spaces in the string
35      *
36      * @return a string of length 'padding' containg only the SPACE
37      * char.
38      */
39     public static String pad(int padding) {
40         if (padding > 0) {
41             StringBuffer sb = new StringBuffer(padding);
42             for (int i = 0; i < padding; i++) {
43                 sb.append(' ');
44             }
45             return sb.toString();
46          } else {
47              return "";
48          }
49     }
50
51     /**
52      * Pads with spaces or truncates the given string to guarantee that it is
53      * exactly the desired length.
54      *
55      * @param string the string to be padded
56      * @param minLength the desired length of the string
57      *
58      * @return a string of length conntaining string
59      * padded with whitespace or truncated
60      */
61     public static String pad(String string, int minLength) {
62         String result = string;
63         int pad = minLength - string.length();
64         if (pad > 0) {
65             result =  string + pad(minLength - string.length());
66         } else if (pad < 0) {
67             result = string.substring(0, minLength);
68         }
69         return result;
70     }
71
72     /**
73      * Removes all instances of the specified character from the given String.
74      *
75      * @param  fromString  the String to delete characters from
76      * @param  charToDelete  the character to delete from the given String
77      *
78      * @return  a String with all instances of the specified char deleted
79      */
80     public static String deleteChar(String fromString, char charToDelete) {
81         StringBuffer buffer = new StringBuffer(fromString.length());
82         for (int i = 0; i < fromString.length(); i++) {
83             if (fromString.charAt(i) != charToDelete) {
84                 buffer.append(fromString.charAt(i));
85             }
86         }
87         return new String(buffer);
88     }
89     
90     /**
91      * Dumps padded text. This is a simple tool for helping dump text 
92      * with padding to a Writer.
93      *
94      * @param pw the stream to send the output
95      * @param padding the number of spaces in the string
96      * @param string the string to output
97      */
98     public static void dump(PrintWriter pw, int padding, String string) {
99         pw.print(pad(padding));
100         pw.println(string);
101     }
102
103     /**
104      * Returns an input stream for the given URL. If the URL
105      * is pointing to a local file, returns a file input stream
106      * suitable for MemoryMapped IO, otherwise, returns a buffered
107      * input stream.
108      *
109      * @param url the url to open as a stream
110      * @return the stream associated with the URL
111      *
112      * @throws IOException if there is trouble creating the stream
113      */
114     public static InputStream getInputStream(URL url) throws IOException {
115         if (url.getProtocol().equals("file")) {
116             return new FileInputStream(url.getFile());
117         } else {
118             return url.openStream();
119         }
120     }
121
122     /**
123      * Outputs a string to the given stream.
124      *
125      * @param dos the stream
126      * @param s the string to output
127      *
128      * @throws IOException if an I/O error occurs
129      */
130     public static void outString(DataOutputStream dos, String s) 
131                         throws IOException {
132         dos.writeShort((short) s.length());
133         for (int i = 0; i < s.length(); i++) {
134             dos.writeChar(s.charAt(i));
135         }
136     }
137
138     /**
139      * Inputs a string from a DataInputStream.
140      *
141      * @param dis the stream
142      *
143      * @return  the string 
144      *
145      * @throws IOException if an I/O error occurs
146      */
147     public static String getString(DataInputStream dis) throws IOException {
148         int size = dis.readShort();
149         char[] charBuffer = new char[size];
150         for (int i = 0; i < size; i++) {
151             charBuffer[i] = dis.readChar();
152         }
153         return new String(charBuffer, 0, size);
154     }
155
156     /**
157      * Inputs a string from a ByteBuffer.
158      *
159      * @param bb the input byte buffer
160      *
161      * @return  the string 
162      *
163      * @throws IOException if an I/O error occurs
164      */
165     public static String getString(ByteBuffer bb) throws IOException {
166         int size = bb.getShort();
167         char[] charBuffer = new char[size];
168         for (int i = 0; i < size; i++) {
169             charBuffer[i] = bb.getChar();
170         }
171         return new String(charBuffer, 0, size);
172     }
173
174
175     /**
176      * Gets a property by name and returns its value. If the property
177      * cannot be found, the default is returned
178      *
179      * @param name the name of the property
180      *
181      * @param defaultValue the default value to use if the property
182      * cannot be found.
183      *
184      * @return the string value for the property, or the defaultValue if 
185      *  the property cannot be found
186      */
187     public static String getProperty(String name, String defaultValue) {
188         String value;
189         try {
190             value = System.getProperty(name, defaultValue);
191         } catch (SecurityException se) {
192             value = defaultValue;
193         }
194         return value;
195     }
196
197     /**
198      * Gets a boolean property by name. 
199      *
200      * @param name the name of the property
201      *
202      * @return  If there is no property with the specified name, or 
203      *  if the specified name is empty or null, then false is returned. 
204      *  otherwise the boolean value of the property is returned
205      *
206      */
207     public static boolean getBoolean(String name) {
208         boolean value;
209         try {
210             value = Boolean.getBoolean(name);
211         } catch (SecurityException se) {
212             value = false;
213         }
214         return value;
215     }
216
217     /**
218      * Gets a long property by name. 
219      *
220      * @param name the name of the property
221      *
222      * @param defaultValue the default value to use if the property
223      * cannot be found.
224      *
225      * @return the long value for the property, or the defaultValue if 
226      *  the property cannot be found
227      *
228      */
229     public static Long getLong(String name, long defaultValue) {
230         Long value;
231         try {
232             value = Long.getLong(name, defaultValue);
233         } catch (SecurityException se) {
234             value = new Long(defaultValue);
235         }
236         return value;
237     }
238
239     /**
240      * Gets an Integer property by name. 
241      *
242      * @param name the name of the property
243      *
244      * @param defaultValue the default value to use if the property
245      * cannot be found.
246      *
247      * @return the Integer value for the property, or the defaultValue if 
248      *  the property cannot be found
249      *
250      */
251     public static Integer getInteger(String name, int defaultValue) {
252         Integer value;
253         try {
254             value = Integer.getInteger(name, defaultValue);
255         } catch (SecurityException se) {
256             value = new Integer(defaultValue);
257         }
258         return value;
259     }
260     
261 }
262
263