Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

StringTool.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 import com.oreilly.servlet.*;
00023 import java.io.*;
00024 import java.sql.*;
00025 import java.util.*;
00026 import javax.servlet.jsp.*;
00027 import javax.servlet.http.*;
00028 
00033 public class StringTool {
00034 
00045         static public String anglosize (String original) throws Exception {
00046                 if (original == null) { return null; }
00047                 if (original.length() == 0) { return original; }
00048                 byte[] bytes = original.getBytes();
00049                 return new String(bytes, 0, bytes.length, "US-ASCII");
00050         }
00051 
00055   static public String fileToString (String path) throws IOException {
00056                 return streamToString( new FileInputStream( path ));
00057                 /*
00058                 File file = new File(path);
00059     StringBuffer sb = new StringBuffer();
00060     String line;
00061     BufferedReader br = new BufferedReader( new FileReader( file ));
00062     while ((line = br.readLine()) != null) { sb.append(line + "\n"); }
00063     return sb.toString();
00064                 */
00065   }
00066 
00070         static public String streamToString (InputStream in) throws IOException {
00071     StringBuffer sb = new StringBuffer();
00072     String line;
00073     BufferedReader br = new BufferedReader( new InputStreamReader( in ));
00074     while ((line = br.readLine()) != null) { sb.append(line + "\n"); }
00075     return sb.toString();
00076         }
00077 
00081         static public String appendPadding (String destination, int desiredLength) throws Exception {
00082                 return appendPadding(destination, " ", desiredLength);
00083         }
00084 
00088         static public String appendPadding (String destination, String padChar, int desiredLength) throws Exception {
00089                 if (padChar == null) { throw new Exception("Cannot pad the string with a null!"); }
00090                 if (padChar.length() != 1) { throw new Exception("Pad char must be of length 1 ('" + padChar + "')"); }
00091 
00092                 // Silently allow nulls to be treated as empty strings.
00093                 if (destination == null) { destination = ""; }
00094 
00095                 // Truncate if necessary; no need to pad if we are truncating
00096                 if (destination.length() > desiredLength) { return destination.substring(0,desiredLength); }
00097 
00098                 // Padd the end with the specified character and return.
00099                 StringBuffer sb = new StringBuffer(destination);
00100                 while (sb.length() < desiredLength) { sb.append(padChar); }
00101                 return sb.toString();
00102         }
00103 
00104 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15