You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtil.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.nio.charset.Charset;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. /**
  23. * Collection of string handling utilities
  24. */
  25. @Internal
  26. public class StringUtil {
  27. private static final POILogger logger = POILogFactory
  28. .getLogger(StringUtil.class);
  29. protected static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
  30. public static final Charset UTF16LE = Charset.forName("UTF-16LE");
  31. public static final Charset UTF8 = Charset.forName("UTF-8");
  32. public static final Charset WIN_1252 = Charset.forName("cp1252");
  33. public static final Charset BIG5 = Charset.forName("Big5");
  34. private static Map<Integer,Integer> msCodepointToUnicode;
  35. private StringUtil() {
  36. // no instances of this class
  37. }
  38. /**
  39. * Given a byte array of 16-bit unicode characters in Little Endian
  40. * format (most important byte last), return a Java String representation
  41. * of it.
  42. *
  43. * { 0x16, 0x00 } -0x16
  44. *
  45. * @param string the byte array to be converted
  46. * @param offset the initial offset into the
  47. * byte array. it is assumed that string[ offset ] and string[ offset +
  48. * 1 ] contain the first 16-bit unicode character
  49. * @param len the length of the final string
  50. * @return the converted string, never <code>null</code>.
  51. * @exception ArrayIndexOutOfBoundsException if offset is out of bounds for
  52. * the byte array (i.e., is negative or is greater than or equal to
  53. * string.length)
  54. * @exception IllegalArgumentException if len is too large (i.e.,
  55. * there is not enough data in string to create a String of that
  56. * length)
  57. */
  58. public static String getFromUnicodeLE(
  59. final byte[] string,
  60. final int offset,
  61. final int len)
  62. throws ArrayIndexOutOfBoundsException, IllegalArgumentException {
  63. if ((offset < 0) || (offset >= string.length)) {
  64. throw new ArrayIndexOutOfBoundsException("Illegal offset " + offset + " (String data is of length " + string.length + ")");
  65. }
  66. if ((len < 0) || (((string.length - offset) / 2) < len)) {
  67. throw new IllegalArgumentException("Illegal length " + len);
  68. }
  69. return new String(string, offset, len * 2, UTF16LE);
  70. }
  71. /**
  72. * Given a byte array of 16-bit unicode characters in little endian
  73. * format (most important byte last), return a Java String representation
  74. * of it.
  75. *
  76. * { 0x16, 0x00 } -0x16
  77. *
  78. * @param string the byte array to be converted
  79. * @return the converted string, never <code>null</code>
  80. */
  81. public static String getFromUnicodeLE(byte[] string) {
  82. if(string.length == 0) { return ""; }
  83. return getFromUnicodeLE(string, 0, string.length / 2);
  84. }
  85. /**
  86. * Convert String to 16-bit unicode characters in little endian format
  87. *
  88. * @param string the string
  89. * @return the byte array of 16-bit unicode characters
  90. */
  91. public static byte[] getToUnicodeLE(String string) {
  92. return string.getBytes(UTF16LE);
  93. }
  94. /**
  95. * Read 8 bit data (in ISO-8859-1 codepage) into a (unicode) Java
  96. * String and return.
  97. * (In Excel terms, read compressed 8 bit unicode as a string)
  98. *
  99. * @param string byte array to read
  100. * @param offset offset to read byte array
  101. * @param len length to read byte array
  102. * @return String generated String instance by reading byte array
  103. */
  104. public static String getFromCompressedUnicode(
  105. final byte[] string,
  106. final int offset,
  107. final int len) {
  108. int len_to_use = Math.min(len, string.length - offset);
  109. return new String(string, offset, len_to_use, ISO_8859_1);
  110. }
  111. public static String readCompressedUnicode(LittleEndianInput in, int nChars) {
  112. byte[] buf = new byte[nChars];
  113. in.readFully(buf);
  114. return new String(buf, ISO_8859_1);
  115. }
  116. /**
  117. * InputStream <tt>in</tt> is expected to contain:
  118. * <ol>
  119. * <li>ushort nChars</li>
  120. * <li>byte is16BitFlag</li>
  121. * <li>byte[]/char[] characterData</li>
  122. * </ol>
  123. * For this encoding, the is16BitFlag is always present even if nChars==0.
  124. *
  125. * This structure is also known as a XLUnicodeString.
  126. */
  127. public static String readUnicodeString(LittleEndianInput in) {
  128. int nChars = in.readUShort();
  129. byte flag = in.readByte();
  130. if ((flag & 0x01) == 0) {
  131. return readCompressedUnicode(in, nChars);
  132. }
  133. return readUnicodeLE(in, nChars);
  134. }
  135. /**
  136. * InputStream <tt>in</tt> is expected to contain:
  137. * <ol>
  138. * <li>byte is16BitFlag</li>
  139. * <li>byte[]/char[] characterData</li>
  140. * </ol>
  141. * For this encoding, the is16BitFlag is always present even if nChars==0.
  142. * <br/>
  143. * This method should be used when the nChars field is <em>not</em> stored
  144. * as a ushort immediately before the is16BitFlag. Otherwise, {@link
  145. * #readUnicodeString(LittleEndianInput)} can be used.
  146. */
  147. public static String readUnicodeString(LittleEndianInput in, int nChars) {
  148. byte is16Bit = in.readByte();
  149. if ((is16Bit & 0x01) == 0) {
  150. return readCompressedUnicode(in, nChars);
  151. }
  152. return readUnicodeLE(in, nChars);
  153. }
  154. /**
  155. * OutputStream <tt>out</tt> will get:
  156. * <ol>
  157. * <li>ushort nChars</li>
  158. * <li>byte is16BitFlag</li>
  159. * <li>byte[]/char[] characterData</li>
  160. * </ol>
  161. * For this encoding, the is16BitFlag is always present even if nChars==0.
  162. */
  163. public static void writeUnicodeString(LittleEndianOutput out, String value) {
  164. int nChars = value.length();
  165. out.writeShort(nChars);
  166. boolean is16Bit = hasMultibyte(value);
  167. out.writeByte(is16Bit ? 0x01 : 0x00);
  168. if (is16Bit) {
  169. putUnicodeLE(value, out);
  170. } else {
  171. putCompressedUnicode(value, out);
  172. }
  173. }
  174. /**
  175. * OutputStream <tt>out</tt> will get:
  176. * <ol>
  177. * <li>byte is16BitFlag</li>
  178. * <li>byte[]/char[] characterData</li>
  179. * </ol>
  180. * For this encoding, the is16BitFlag is always present even if nChars==0.
  181. * <br/>
  182. * This method should be used when the nChars field is <em>not</em> stored
  183. * as a ushort immediately before the is16BitFlag. Otherwise, {@link
  184. * #writeUnicodeString(LittleEndianOutput, String)} can be used.
  185. */
  186. public static void writeUnicodeStringFlagAndData(LittleEndianOutput out, String value) {
  187. boolean is16Bit = hasMultibyte(value);
  188. out.writeByte(is16Bit ? 0x01 : 0x00);
  189. if (is16Bit) {
  190. putUnicodeLE(value, out);
  191. } else {
  192. putCompressedUnicode(value, out);
  193. }
  194. }
  195. /**
  196. * @return the number of bytes that would be written by {@link #writeUnicodeString(LittleEndianOutput, String)}
  197. */
  198. public static int getEncodedSize(String value) {
  199. int result = 2 + 1;
  200. result += value.length() * (StringUtil.hasMultibyte(value) ? 2 : 1);
  201. return result;
  202. }
  203. /**
  204. * Takes a unicode (java) string, and returns it as 8 bit data (in ISO-8859-1
  205. * codepage).
  206. * (In Excel terms, write compressed 8 bit unicode)
  207. *
  208. * @param input the String containing the data to be written
  209. * @param output the byte array to which the data is to be written
  210. * @param offset an offset into the byte arrat at which the data is start
  211. * when written
  212. */
  213. public static void putCompressedUnicode(String input, byte[] output, int offset) {
  214. byte[] bytes = input.getBytes(ISO_8859_1);
  215. System.arraycopy(bytes, 0, output, offset, bytes.length);
  216. }
  217. public static void putCompressedUnicode(String input, LittleEndianOutput out) {
  218. byte[] bytes = input.getBytes(ISO_8859_1);
  219. out.write(bytes);
  220. }
  221. /**
  222. * Takes a unicode string, and returns it as little endian (most
  223. * important byte last) bytes in the supplied byte array.
  224. * (In Excel terms, write uncompressed unicode)
  225. *
  226. * @param input the String containing the unicode data to be written
  227. * @param output the byte array to hold the uncompressed unicode, should be twice the length of the String
  228. * @param offset the offset to start writing into the byte array
  229. */
  230. public static void putUnicodeLE(String input, byte[] output, int offset) {
  231. byte[] bytes = input.getBytes(UTF16LE);
  232. System.arraycopy(bytes, 0, output, offset, bytes.length);
  233. }
  234. public static void putUnicodeLE(String input, LittleEndianOutput out) {
  235. byte[] bytes = input.getBytes(UTF16LE);
  236. out.write(bytes);
  237. }
  238. public static String readUnicodeLE(LittleEndianInput in, int nChars) {
  239. byte[] bytes = new byte[nChars*2];
  240. in.readFully(bytes);
  241. return new String(bytes, UTF16LE);
  242. }
  243. /**
  244. * @return the encoding we want to use, currently hardcoded to ISO-8859-1
  245. */
  246. public static String getPreferredEncoding() {
  247. return ISO_8859_1.name();
  248. }
  249. /**
  250. * check the parameter has multibyte character
  251. *
  252. * @param value string to check
  253. * @return boolean result true:string has at least one multibyte character
  254. */
  255. public static boolean hasMultibyte(String value) {
  256. if (value == null)
  257. return false;
  258. for (char c : value.toCharArray()) {
  259. if (c > 0xFF) {
  260. return true;
  261. }
  262. }
  263. return false;
  264. }
  265. /**
  266. * Checks to see if a given String needs to be represented as Unicode
  267. *
  268. * @param value The string to look at.
  269. * @return true if string needs Unicode to be represented.
  270. */
  271. public static boolean isUnicodeString(final String value) {
  272. return !value.equals(new String(value.getBytes(ISO_8859_1), ISO_8859_1));
  273. }
  274. /**
  275. * Tests if the string starts with the specified prefix, ignoring case consideration.
  276. */
  277. public static boolean startsWithIgnoreCase(String haystack, String prefix) {
  278. return haystack.regionMatches(true, 0, prefix, 0, prefix.length());
  279. }
  280. /**
  281. * Tests if the string ends with the specified suffix, ignoring case consideration.
  282. */
  283. public static boolean endsWithIgnoreCase(String haystack, String suffix) {
  284. int length = suffix.length();
  285. int start = haystack.length() - length;
  286. return haystack.regionMatches(true, start, suffix, 0, length);
  287. }
  288. /**
  289. * An Iterator over an array of Strings.
  290. */
  291. public static class StringsIterator implements Iterator<String> {
  292. private String[] strings = {};
  293. private int position = 0;
  294. public StringsIterator(String[] strings) {
  295. if (strings != null) {
  296. this.strings = strings.clone();
  297. }
  298. }
  299. public boolean hasNext() {
  300. return position < strings.length;
  301. }
  302. public String next() {
  303. int ourPos = position++;
  304. if(ourPos >= strings.length) {
  305. throw new ArrayIndexOutOfBoundsException(ourPos);
  306. }
  307. return strings[ourPos];
  308. }
  309. public void remove() {}
  310. }
  311. /**
  312. * Some strings may contain encoded characters of the unicode private use area.
  313. * Currently the characters of the symbol fonts are mapped to the corresponding
  314. * characters in the normal unicode range.
  315. *
  316. * @param string the original string
  317. * @return the string with mapped characters
  318. *
  319. * @see <a href="http://www.alanwood.net/unicode/private_use_area.html#symbol">Private Use Area (symbol)</a>
  320. * @see <a href="http://www.alanwood.net/demos/symbol.html">Symbol font - Unicode alternatives for Greek and special characters in HTML</a>
  321. */
  322. public static String mapMsCodepointString(String string) {
  323. if (string == null || "".equals(string)) return string;
  324. initMsCodepointMap();
  325. StringBuilder sb = new StringBuilder();
  326. final int length = string.length();
  327. for (int offset = 0; offset < length; ) {
  328. Integer msCodepoint = string.codePointAt(offset);
  329. Integer uniCodepoint = msCodepointToUnicode.get(msCodepoint);
  330. sb.appendCodePoint(uniCodepoint == null ? msCodepoint : uniCodepoint);
  331. offset += Character.charCount(msCodepoint);
  332. }
  333. return sb.toString();
  334. }
  335. public static synchronized void mapMsCodepoint(int msCodepoint, int unicodeCodepoint) {
  336. initMsCodepointMap();
  337. msCodepointToUnicode.put(msCodepoint, unicodeCodepoint);
  338. }
  339. private static synchronized void initMsCodepointMap() {
  340. if (msCodepointToUnicode != null) return;
  341. msCodepointToUnicode = new HashMap<Integer,Integer>();
  342. int i=0xF020;
  343. for (int ch : symbolMap_f020) {
  344. msCodepointToUnicode.put(i++, ch);
  345. }
  346. i = 0xf0a0;
  347. for (int ch : symbolMap_f0a0) {
  348. msCodepointToUnicode.put(i++, ch);
  349. }
  350. }
  351. private static final int symbolMap_f020[] = {
  352. ' ', // 0xf020 space
  353. '!', // 0xf021 exclam
  354. 8704, // 0xf022 universal
  355. '#', // 0xf023 numbersign
  356. 8707, // 0xf024 existential
  357. '%', // 0xf025 percent
  358. '&', // 0xf026 ampersand
  359. 8717, // 0xf027 suchthat
  360. '(', // 0xf028 parenleft
  361. ')', // 0xf029 parentright
  362. 8727, // 0xf02a asteriskmath
  363. '+', // 0xf02b plus
  364. ',', // 0xf02c comma
  365. 8722, // 0xf02d minus sign (long -)
  366. '.', // 0xf02e period
  367. '/', // 0xf02f slash
  368. '0', // 0xf030 0
  369. '1', // 0xf031 1
  370. '2', // 0xf032 2
  371. '3', // 0xf033 3
  372. '4', // 0xf034 4
  373. '5', // 0xf035 5
  374. '6', // 0xf036 6
  375. '7', // 0xf037 7
  376. '8', // 0xf038 8
  377. '9', // 0xf039 9
  378. ':', // 0xf03a colon
  379. ';', // 0xf03b semicolon
  380. '<', // 0xf03c less
  381. '=', // 0xf03d equal
  382. '>', // 0xf03e greater
  383. '?', // 0xf03f question
  384. 8773, // 0xf040 congruent
  385. 913, // 0xf041 alpha (upper)
  386. 914, // 0xf042 beta (upper)
  387. 935, // 0xf043 chi (upper)
  388. 916, // 0xf044 delta (upper)
  389. 917, // 0xf045 epsilon (upper)
  390. 934, // 0xf046 phi (upper)
  391. 915, // 0xf047 gamma (upper)
  392. 919, // 0xf048 eta (upper)
  393. 921, // 0xf049 iota (upper)
  394. 977, // 0xf04a theta1 (lower)
  395. 922, // 0xf04b kappa (upper)
  396. 923, // 0xf04c lambda (upper)
  397. 924, // 0xf04d mu (upper)
  398. 925, // 0xf04e nu (upper)
  399. 927, // 0xf04f omicron (upper)
  400. 928, // 0xf050 pi (upper)
  401. 920, // 0xf051 theta (upper)
  402. 929, // 0xf052 rho (upper)
  403. 931, // 0xf053 sigma (upper)
  404. 932, // 0xf054 tau (upper)
  405. 933, // 0xf055 upsilon (upper)
  406. 962, // 0xf056 simga1 (lower)
  407. 937, // 0xf057 omega (upper)
  408. 926, // 0xf058 xi (upper)
  409. 936, // 0xf059 psi (upper)
  410. 918, // 0xf05a zeta (upper)
  411. '[', // 0xf05b bracketleft
  412. 8765, // 0xf05c therefore
  413. ']', // 0xf05d bracketright
  414. 8869, // 0xf05e perpendicular
  415. '_', // 0xf05f underscore
  416. ' ', // 0xf060 radicalex (doesn't exist in unicode)
  417. 945, // 0xf061 alpha (lower)
  418. 946, // 0xf062 beta (lower)
  419. 967, // 0xf063 chi (lower)
  420. 948, // 0xf064 delta (lower)
  421. 949, // 0xf065 epsilon (lower)
  422. 966, // 0xf066 phi (lower)
  423. 947, // 0xf067 gamma (lower)
  424. 951, // 0xf068 eta (lower)
  425. 953, // 0xf069 iota (lower)
  426. 981, // 0xf06a phi1 (lower)
  427. 954, // 0xf06b kappa (lower)
  428. 955, // 0xf06c lambda (lower)
  429. 956, // 0xf06d mu (lower)
  430. 957, // 0xf06e nu (lower)
  431. 959, // 0xf06f omnicron (lower)
  432. 960, // 0xf070 pi (lower)
  433. 952, // 0xf071 theta (lower)
  434. 961, // 0xf072 rho (lower)
  435. 963, // 0xf073 sigma (lower)
  436. 964, // 0xf074 tau (lower)
  437. 965, // 0xf075 upsilon (lower)
  438. 982, // 0xf076 piv (lower)
  439. 969, // 0xf077 omega (lower)
  440. 958, // 0xf078 xi (lower)
  441. 968, // 0xf079 psi (lower)
  442. 950, // 0xf07a zeta (lower)
  443. '{', // 0xf07b braceleft
  444. '|', // 0xf07c bar
  445. '}', // 0xf07d braceright
  446. 8764, // 0xf07e similar '~'
  447. ' ', // 0xf07f not defined
  448. };
  449. private static final int symbolMap_f0a0[] = {
  450. 8364, // 0xf0a0 not defined / euro symbol
  451. 978, // 0xf0a1 upsilon1 (upper)
  452. 8242, // 0xf0a2 minute
  453. 8804, // 0xf0a3 lessequal
  454. 8260, // 0xf0a4 fraction
  455. 8734, // 0xf0a5 infinity
  456. 402, // 0xf0a6 florin
  457. 9827, // 0xf0a7 club
  458. 9830, // 0xf0a8 diamond
  459. 9829, // 0xf0a9 heart
  460. 9824, // 0xf0aa spade
  461. 8596, // 0xf0ab arrowboth
  462. 8591, // 0xf0ac arrowleft
  463. 8593, // 0xf0ad arrowup
  464. 8594, // 0xf0ae arrowright
  465. 8595, // 0xf0af arrowdown
  466. 176, // 0xf0b0 degree
  467. 177, // 0xf0b1 plusminus
  468. 8243, // 0xf0b2 second
  469. 8805, // 0xf0b3 greaterequal
  470. 215, // 0xf0b4 multiply
  471. 181, // 0xf0b5 proportional
  472. 8706, // 0xf0b6 partialdiff
  473. 8729, // 0xf0b7 bullet
  474. 247, // 0xf0b8 divide
  475. 8800, // 0xf0b9 notequal
  476. 8801, // 0xf0ba equivalence
  477. 8776, // 0xf0bb approxequal
  478. 8230, // 0xf0bc ellipsis
  479. 9168, // 0xf0bd arrowvertex
  480. 9135, // 0xf0be arrowhorizex
  481. 8629, // 0xf0bf carriagereturn
  482. 8501, // 0xf0c0 aleph
  483. 8475, // 0xf0c1 Ifraktur
  484. 8476, // 0xf0c2 Rfraktur
  485. 8472, // 0xf0c3 weierstrass
  486. 8855, // 0xf0c4 circlemultiply
  487. 8853, // 0xf0c5 circleplus
  488. 8709, // 0xf0c6 emptyset
  489. 8745, // 0xf0c7 intersection
  490. 8746, // 0xf0c8 union
  491. 8835, // 0xf0c9 propersuperset
  492. 8839, // 0xf0ca reflexsuperset
  493. 8836, // 0xf0cb notsubset
  494. 8834, // 0xf0cc propersubset
  495. 8838, // 0xf0cd reflexsubset
  496. 8712, // 0xf0ce element
  497. 8713, // 0xf0cf notelement
  498. 8736, // 0xf0d0 angle
  499. 8711, // 0xf0d1 gradient
  500. 174, // 0xf0d2 registerserif
  501. 169, // 0xf0d3 copyrightserif
  502. 8482, // 0xf0d4 trademarkserif
  503. 8719, // 0xf0d5 product
  504. 8730, // 0xf0d6 radical
  505. 8901, // 0xf0d7 dotmath
  506. 172, // 0xf0d8 logicalnot
  507. 8743, // 0xf0d9 logicaland
  508. 8744, // 0xf0da logicalor
  509. 8660, // 0xf0db arrowdblboth
  510. 8656, // 0xf0dc arrowdblleft
  511. 8657, // 0xf0dd arrowdblup
  512. 8658, // 0xf0de arrowdblright
  513. 8659, // 0xf0df arrowdbldown
  514. 9674, // 0xf0e0 lozenge
  515. 9001, // 0xf0e1 angleleft
  516. 174, // 0xf0e2 registersans
  517. 169, // 0xf0e3 copyrightsans
  518. 8482, // 0xf0e4 trademarksans
  519. 8721, // 0xf0e5 summation
  520. 9115, // 0xf0e6 parenlefttp
  521. 9116, // 0xf0e7 parenleftex
  522. 9117, // 0xf0e8 parenleftbt
  523. 9121, // 0xf0e9 bracketlefttp
  524. 9122, // 0xf0ea bracketleftex
  525. 9123, // 0xf0eb bracketleftbt
  526. 9127, // 0xf0ec bracelefttp
  527. 9128, // 0xf0ed braceleftmid
  528. 9129, // 0xf0ee braceleftbt
  529. 9130, // 0xf0ef braceex
  530. ' ', // 0xf0f0 not defined
  531. 9002, // 0xf0f1 angleright
  532. 8747, // 0xf0f2 integral
  533. 8992, // 0xf0f3 integraltp
  534. 9134, // 0xf0f4 integralex
  535. 8993, // 0xf0f5 integralbt
  536. 9118, // 0xf0f6 parenrighttp
  537. 9119, // 0xf0f7 parenrightex
  538. 9120, // 0xf0f8 parenrightbt
  539. 9124, // 0xf0f9 bracketrighttp
  540. 9125, // 0xf0fa bracketrightex
  541. 9126, // 0xf0fb bracketrightbt
  542. 9131, // 0xf0fc bracerighttp
  543. 9132, // 0xf0fd bracerightmid
  544. 9133, // 0xf0fe bracerightbt
  545. ' ', // 0xf0ff not defined
  546. };
  547. /**
  548. * This tries to convert a LE byte array in Big5 to a String.
  549. * We know MS zero-padded ascii, and we drop those.
  550. * However, there may be areas for improvement in this.
  551. *
  552. * @param data
  553. * @param offset
  554. * @param lengthInBytes
  555. * @return
  556. */
  557. public static String littleEndianBig5Stream(byte[] data, int offset, int lengthInBytes) {
  558. ByteArrayOutputStream os = new ByteArrayOutputStream();
  559. try {
  560. IOUtils.copy(new LittleEndianBig5Stream(data, offset, lengthInBytes), os);
  561. } catch (IOException e) {
  562. logger.log(POILogger.WARN,
  563. "IOException while copying a byte array stream to a byte array stream?!");
  564. }
  565. return new String(os.toByteArray(), BIG5);
  566. }
  567. // Could be replaced with org.apache.commons.lang3.StringUtils#join
  568. @Internal
  569. public static String join(Object[] array, String separator) {
  570. if (array == null || array.length == 0) return "";
  571. StringBuilder sb = new StringBuilder();
  572. sb.append(array[0]);
  573. for (int i=1; i<array.length; i++) {
  574. sb.append(separator).append(array[i]);
  575. }
  576. return sb.toString();
  577. }
  578. @Internal
  579. public static String join(Object[] array) {
  580. if (array == null) return "";
  581. StringBuilder sb = new StringBuilder();
  582. for (Object o : array) {
  583. sb.append(o);
  584. }
  585. return sb.toString();
  586. }
  587. @Internal
  588. public static String join(String separator, Object... array) {
  589. return join(array, separator);
  590. }
  591. /**
  592. * Count number of occurrences of needle in haystack
  593. * Has same signature as org.apache.commons.lang3.StringUtils#countMatches
  594. *
  595. * @param haystack the CharSequence to check, may be null
  596. * @param needle the character to count the quantity of
  597. * @return the number of occurrences, 0 if the CharSequence is null
  598. */
  599. public static int countMatches(CharSequence haystack, char needle) {
  600. if (haystack == null) return 0;
  601. int count = 0;
  602. final int length = haystack.length();
  603. for (int i=0; i<length; i++) {
  604. if (haystack.charAt(i) == needle) {
  605. count++;
  606. }
  607. }
  608. return count;
  609. }
  610. }