1 package net.sf.openrocket.util;
3 import java.nio.ByteBuffer;
4 import java.nio.charset.Charset;
7 public class TextUtil {
10 private static final char[] HEX = {
11 '0', '1', '2', '3', '4', '5', '6', '7',
12 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
16 * Return the byte array for the string in the given charset.
18 * This function is implemented because Froyo (Android API 8) does not support
19 * String.getBytes(Charset)
25 public static byte[] convertStringToBytes( String string, Charset charSet ) {
26 ByteBuffer encoded = charSet.encode(string);
27 return encoded.array();
32 * Return the bytes formatted as a hexadecimal string. The length of the
33 * string will be twice the number of bytes, with no spacing between the bytes
34 * and lowercase letters utilized.
36 * @param bytes the bytes to convert.
37 * @return the bytes in hexadecimal notation.
39 public static final String hexString(byte[] bytes) {
40 StringBuilder sb = new StringBuilder(bytes.length * 2);
41 for (byte b : bytes) {
42 sb.append(HEX[(b >>> 4) & 0xF]);
43 sb.append(HEX[b & 0xF]);
49 * Return a string of the double value with suitable precision (5 digits).
50 * The string is the shortest representation of the value including the
53 * @param d the value to present.
54 * @return a representation with suitable precision.
56 public static final String doubleToString(double d) {
58 // Check for special cases
59 if (MathUtil.equals(d, 0))
65 if (Double.isInfinite(d)) {
73 final String sign = (d < 0) ? "-" : "";
74 double abs = Math.abs(d);
76 // Small and large values always in exponential notation
77 if (abs < 0.001 || abs >= 100000000) {
78 return sign + exponentialFormat(abs);
81 // Check whether decimal or exponential notation is shorter
83 String exp = exponentialFormat(abs);
84 String dec = decimalFormat(abs);
86 if (dec.length() <= exp.length())
94 * value must be positive and not zero!
96 private static String exponentialFormat(double value) {
100 while (value < 1.0) {
104 while (value >= 10.0) {
109 return shortDecimal(value, 4) + "e" + exp;
114 * value must be positive and not zero!
116 private static String decimalFormat(double value) {
118 return "" + (int) (value + 0.5);
127 return shortDecimal(value, decimals);
134 * value must be positive!
136 private static String shortDecimal(double value, int decimals) {
138 // Calculate rounding and limit values (rounding slightly smaller)
141 for (int i = 0; i < decimals; i++) {
147 value = (Math.rint(value * rounding) + 0.1) / rounding;
150 int whole = (int) value;
158 StringBuilder sb = new StringBuilder();
159 sb.append("" + whole);
163 for (int i = 0; i < decimals; i++) {
168 sb.append((char) ('0' + whole));
171 return sb.toString();
176 return sb.toString();
180 public static String htmlEncode(String s) {
181 s = s.replace("&", "&");
182 s = s.replace("\"", """);
183 s = s.replace("<", "<");
184 s = s.replace(">", ">");
189 * Returns a word-wrapped version of given input string using HTML syntax, wrapped to len characters.
191 public static String wrap(String in,int len) {
193 if(in.length()<len) return in;
194 if(in.substring(0, len).contains("\n"))
195 return in.substring(0, in.indexOf("\n")).trim() + "\n\n" + wrap(in.substring(in.indexOf("\n") + 1), len);
196 int place=Math.max(Math.max(in.lastIndexOf(" ",len),in.lastIndexOf("\t",len)),in.lastIndexOf("-",len));
197 return "<html>"+in.substring(0,place).trim()+"<br>"+wrap(in.substring(place),len);