1 19 20 package jxl.biff; 21 22 import java.io.UnsupportedEncodingException ; 23 24 import common.Logger; 25 26 import jxl.WorkbookSettings; 27 28 32 public final class StringHelper 33 { 34 37 private static Logger logger = Logger.getLogger(StringHelper.class); 38 39 42 private StringHelper() 43 { 44 } 45 46 54 public static byte[] getBytes(String s) 55 { 56 return s.getBytes(); 57 } 58 59 66 public static byte[] getBytes(String s, WorkbookSettings ws) 67 { 68 try 69 { 70 return s.getBytes(ws.getEncoding()); 71 } 72 catch (UnsupportedEncodingException e) 73 { 74 return null; 76 } 77 } 78 79 85 public static byte[] getUnicodeBytes(String s) 86 { 87 try 88 { 89 byte[] b = s.getBytes("UnicodeLittle"); 90 91 if (b.length == (s.length() * 2 + 2)) 94 { 95 byte[] b2 = new byte[b.length - 2]; 96 System.arraycopy(b, 2, b2, 0, b2.length); 97 b = b2; 98 } 99 return b; 100 } 101 catch (UnsupportedEncodingException e) 102 { 103 return null; 105 } 106 } 107 108 116 public static void getBytes(String s, byte[] d, int pos) 117 { 118 byte[] b = getBytes(s); 119 System.arraycopy(b, 0, d, pos, b.length); 120 } 121 122 130 public static void getUnicodeBytes(String s, byte[] d, int pos) 131 { 132 byte[] b = getUnicodeBytes(s); 133 System.arraycopy(b, 0, d, pos, b.length); 134 } 135 136 146 public static String getString(byte[] d, int length, int pos, 147 WorkbookSettings ws) 148 { 149 try 150 { 151 byte[] b = new byte[length]; 152 System.arraycopy(d, pos, b, 0, length); 153 return new String (b, ws.getEncoding()); 154 } 155 catch (UnsupportedEncodingException e) 156 { 157 logger.warn(e.toString()); 158 return ""; 159 } 160 } 161 162 170 public static String getUnicodeString(byte[] d, int length, int pos) 171 { 172 try 173 { 174 byte[] b = new byte[length * 2]; 175 System.arraycopy(d, pos, b, 0, length * 2); 176 return new String (b, "UnicodeLittle"); 177 } 178 catch (UnsupportedEncodingException e) 179 { 180 return ""; 182 } 183 } 184 } 185 186 187 | Popular Tags |