Merge commit '42b2e5ca519766e37ce6941ba4faecc9691cc403' into upstream
[debian/openrocket] / core / src / net / sf / openrocket / util / TextUtil.java
index 32dffd849a4eb0c13cc92d8c1eb613df76e5b327..0b86876de9055e3047b5ea3649b9c19e50325a31 100644 (file)
@@ -1,12 +1,32 @@
 package net.sf.openrocket.util;
 
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+
 
 public class TextUtil {
+
+       
        private static final char[] HEX = {
                        '0', '1', '2', '3', '4', '5', '6', '7',
                        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        };
-       
+
+       /**
+        * Return the byte array for the string in the given charset.
+        * 
+        * This function is implemented because Froyo (Android API 8) does not support
+        * String.getBytes(Charset)
+        * 
+        * @param string
+        * @param charSet
+        * @return
+        */
+       public static byte[] convertStringToBytes( String string, Charset charSet ) {
+               ByteBuffer encoded = charSet.encode(string);
+               return encoded.array();
+       }
+
        
        /**
         * Return the bytes formatted as a hexadecimal string.  The length of the
@@ -164,4 +184,17 @@ public class TextUtil {
                s = s.replace(">", ">");
                return s;
        }
+       
+       /*
+        * Returns a word-wrapped version of given input string using HTML syntax, wrapped to len characters.
+        */
+       public static String wrap(String in,int len) {
+               in=in.trim();
+               if(in.length()<len) return in;
+               if(in.substring(0, len).contains("\n"))
+                       return in.substring(0, in.indexOf("\n")).trim() + "\n\n" + wrap(in.substring(in.indexOf("\n") + 1), len);
+               int place=Math.max(Math.max(in.lastIndexOf(" ",len),in.lastIndexOf("\t",len)),in.lastIndexOf("-",len));
+               return "<html>"+in.substring(0,place).trim()+"<br>"+wrap(in.substring(place),len);
+       }
+
 }