2 * Copyright 2001 Sun Microsystems, Inc.
4 * See the file "license.terms" for information on usage and
5 * redistribution of this file, and for a DISCLAIMER OF ALL
8 package com.sun.speech.freetts.util;
10 import java.io.PrintWriter;
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;
20 * Provides a set of generic utilities used by freetts.
22 public class Utilities {
31 * Returns a string with the given number of
34 * @param padding the number of spaces in the string
36 * @return a string of length 'padding' containg only the SPACE
39 public static String pad(int padding) {
41 StringBuffer sb = new StringBuffer(padding);
42 for (int i = 0; i < padding; i++) {
52 * Pads with spaces or truncates the given string to guarantee that it is
53 * exactly the desired length.
55 * @param string the string to be padded
56 * @param minLength the desired length of the string
58 * @return a string of length conntaining string
59 * padded with whitespace or truncated
61 public static String pad(String string, int minLength) {
62 String result = string;
63 int pad = minLength - string.length();
65 result = string + pad(minLength - string.length());
67 result = string.substring(0, minLength);
73 * Removes all instances of the specified character from the given String.
75 * @param fromString the String to delete characters from
76 * @param charToDelete the character to delete from the given String
78 * @return a String with all instances of the specified char deleted
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));
87 return new String(buffer);
91 * Dumps padded text. This is a simple tool for helping dump text
92 * with padding to a Writer.
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
98 public static void dump(PrintWriter pw, int padding, String string) {
99 pw.print(pad(padding));
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
109 * @param url the url to open as a stream
110 * @return the stream associated with the URL
112 * @throws IOException if there is trouble creating the stream
114 public static InputStream getInputStream(URL url) throws IOException {
115 if (url.getProtocol().equals("file")) {
116 return new FileInputStream(url.getFile());
118 return url.openStream();
123 * Outputs a string to the given stream.
125 * @param dos the stream
126 * @param s the string to output
128 * @throws IOException if an I/O error occurs
130 public static void outString(DataOutputStream dos, String s)
132 dos.writeShort((short) s.length());
133 for (int i = 0; i < s.length(); i++) {
134 dos.writeChar(s.charAt(i));
139 * Inputs a string from a DataInputStream.
141 * @param dis the stream
145 * @throws IOException if an I/O error occurs
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();
153 return new String(charBuffer, 0, size);
157 * Inputs a string from a ByteBuffer.
159 * @param bb the input byte buffer
163 * @throws IOException if an I/O error occurs
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();
171 return new String(charBuffer, 0, size);
176 * Gets a property by name and returns its value. If the property
177 * cannot be found, the default is returned
179 * @param name the name of the property
181 * @param defaultValue the default value to use if the property
184 * @return the string value for the property, or the defaultValue if
185 * the property cannot be found
187 public static String getProperty(String name, String defaultValue) {
190 value = System.getProperty(name, defaultValue);
191 } catch (SecurityException se) {
192 value = defaultValue;
198 * Gets a boolean property by name.
200 * @param name the name of the property
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
207 public static boolean getBoolean(String name) {
210 value = Boolean.getBoolean(name);
211 } catch (SecurityException se) {
218 * Gets a long property by name.
220 * @param name the name of the property
222 * @param defaultValue the default value to use if the property
225 * @return the long value for the property, or the defaultValue if
226 * the property cannot be found
229 public static Long getLong(String name, long defaultValue) {
232 value = Long.getLong(name, defaultValue);
233 } catch (SecurityException se) {
234 value = new Long(defaultValue);
240 * Gets an Integer property by name.
242 * @param name the name of the property
244 * @param defaultValue the default value to use if the property
247 * @return the Integer value for the property, or the defaultValue if
248 * the property cannot be found
251 public static Integer getInteger(String name, int defaultValue) {
254 value = Integer.getInteger(name, defaultValue);
255 } catch (SecurityException se) {
256 value = new Integer(defaultValue);