001/** 002 * Copyright 2001 Sun Microsystems, Inc. 003 * 004 * See the file "license.terms" for information on usage and 005 * redistribution of this file, and for a DISCLAIMER OF ALL 006 * WARRANTIES. 007 */ 008package com.sun.speech.freetts.util; 009 010import java.io.PrintWriter; 011import java.net.URL; 012import java.io.FileInputStream; 013import java.io.DataInputStream; 014import java.io.DataOutputStream; 015import java.nio.ByteBuffer; 016import java.io.InputStream; 017import java.io.IOException; 018 019/** 020 * Provides a set of generic utilities used by freetts. 021 */ 022public class Utilities { 023 024 /** 025 * Avoid construction. 026 */ 027 private Utilities() { 028 } 029 030 /** 031 * Returns a string with the given number of 032 * spaces. 033 * 034 * @param padding the number of spaces in the string 035 * 036 * @return a string of length 'padding' containg only the SPACE 037 * char. 038 */ 039 public static String pad(int padding) { 040 if (padding > 0) { 041 StringBuffer sb = new StringBuffer(padding); 042 for (int i = 0; i < padding; i++) { 043 sb.append(' '); 044 } 045 return sb.toString(); 046 } else { 047 return ""; 048 } 049 } 050 051 /** 052 * Pads with spaces or truncates the given string to guarantee that it is 053 * exactly the desired length. 054 * 055 * @param string the string to be padded 056 * @param minLength the desired length of the string 057 * 058 * @return a string of length conntaining string 059 * padded with whitespace or truncated 060 */ 061 public static String pad(String string, int minLength) { 062 String result = string; 063 int pad = minLength - string.length(); 064 if (pad > 0) { 065 result = string + pad(minLength - string.length()); 066 } else if (pad < 0) { 067 result = string.substring(0, minLength); 068 } 069 return result; 070 } 071 072 /** 073 * Removes all instances of the specified character from the given String. 074 * 075 * @param fromString the String to delete characters from 076 * @param charToDelete the character to delete from the given String 077 * 078 * @return a String with all instances of the specified char deleted 079 */ 080 public static String deleteChar(String fromString, char charToDelete) { 081 StringBuffer buffer = new StringBuffer(fromString.length()); 082 for (int i = 0; i < fromString.length(); i++) { 083 if (fromString.charAt(i) != charToDelete) { 084 buffer.append(fromString.charAt(i)); 085 } 086 } 087 return new String(buffer); 088 } 089 090 /** 091 * Dumps padded text. This is a simple tool for helping dump text 092 * with padding to a Writer. 093 * 094 * @param pw the stream to send the output 095 * @param padding the number of spaces in the string 096 * @param string the string to output 097 */ 098 public static void dump(PrintWriter pw, int padding, String string) { 099 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