00001 package net.threebit.utils.sosc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00059
00060
00061
00062
00063
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
00093 if (destination == null) { destination = ""; }
00094
00095
00096 if (destination.length() > desiredLength) { return destination.substring(0,desiredLength); }
00097
00098
00099 StringBuffer sb = new StringBuffer(destination);
00100 while (sb.length() < desiredLength) { sb.append(padChar); }
00101 return sb.toString();
00102 }
00103
00104 }