01: package fr.aliacom.util;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.io.PrintWriter;
07:
08: /**
09: * @author tom
10: *
11: * To change this generated comment edit the template variable "typecomment":
12: * Window>Preferences>Java>Templates.
13: * To enable and disable the creation of type comments go to
14: * Window>Preferences>Java>Code Generation.
15: */
16: public class StringFunctions {
17: /**
18: * Replaces a by b in c
19: */
20: public static String strReplace(String a, String b, final String c) {
21: StringBuffer ret = new StringBuffer(
22: (b.length() > a.length()) ? c.length() * 2 : c.length());
23: // try to prevent reallocations
24: for (int i = 0; i < c.length(); i++) {
25: if (c.startsWith(a, i)) {
26: ret.append(b);
27: i += a.length() - 1;
28: } else {
29: ret.append(c.charAt(i));
30: }
31: }
32: return ret.toString();
33: }
34:
35: public static String read(InputStream in) throws IOException {
36: final byte[] buf = new byte[4096];
37: int readCount;
38: ByteArrayOutputStream out = new ByteArrayOutputStream();
39:
40: while ((readCount = in.read(buf, 0, buf.length)) > 0) {
41: out.write(buf, 0, readCount);
42: }
43:
44: out.flush();
45: out.close();
46: in.close();
47: return out.toString();
48: }
49:
50: public static String getExceptionTraceText(Throwable t) {
51: ByteArrayOutputStream out = new ByteArrayOutputStream();
52: t.printStackTrace(new PrintWriter(out));
53: return out.toString();
54: }
55:
56: /**
57: * Trims the whitespaces at the end
58: * of a string.
59: * @param s the string to trim
60: */
61: public static String rtrim(String s) {
62: int i = s.length();
63: while (i > 0 && s.charAt(i - 1) == ' ') {
64: i--;
65: }
66: return s.substring(0, i);
67: }
68: }
|