Utils.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.jute;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. /**
  22. * Various utility functions for Hadoop record I/O runtime.
  23. * @author Milind Bhandarkar
  24. */
  25. public class Utils {
  26. /** Cannot create a new instance of Utils */
  27. private Utils() {
  28. super();
  29. }
  30. /**
  31. * equals function that actually compares two buffers.
  32. *
  33. * @param onearray First buffer
  34. * @param twoarray Second buffer
  35. * @return true if one and two contain exactly the same content, else false.
  36. */
  37. public static boolean bufEquals(byte onearray[], byte twoarray[] ) {
  38. if (onearray == twoarray) return true;
  39. boolean ret = (onearray.length == twoarray.length);
  40. if (!ret) {
  41. return ret;
  42. }
  43. for (int idx = 0; idx < onearray.length; idx++) {
  44. if (onearray[idx] != twoarray[idx]) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. private static final char[] hexchars = { '0', '1', '2', '3', '4', '5',
  51. '6', '7', '8', '9', 'A', 'B',
  52. 'C', 'D', 'E', 'F' };
  53. /**
  54. *
  55. * @param s
  56. * @return
  57. */
  58. static String toXMLString(String s) {
  59. if (s == null)
  60. return "";
  61. StringBuffer sb = new StringBuffer();
  62. for (int idx = 0; idx < s.length(); idx++) {
  63. char ch = s.charAt(idx);
  64. if (ch == '<') {
  65. sb.append("&lt;");
  66. } else if (ch == '&') {
  67. sb.append("&amp;");
  68. } else if (ch == '%') {
  69. sb.append("%25");
  70. } else if (ch < 0x20) {
  71. sb.append("%");
  72. sb.append(hexchars[ch/16]);
  73. sb.append(hexchars[ch%16]);
  74. } else {
  75. sb.append(ch);
  76. }
  77. }
  78. return sb.toString();
  79. }
  80. static private int h2c(char ch) {
  81. if (ch >= '0' && ch <= '9') {
  82. return ch - '0';
  83. } else if (ch >= 'A' && ch <= 'F') {
  84. return ch - 'A';
  85. } else if (ch >= 'a' && ch <= 'f') {
  86. return ch - 'a';
  87. }
  88. return 0;
  89. }
  90. /**
  91. *
  92. * @param s
  93. * @return
  94. */
  95. static String fromXMLString(String s) {
  96. StringBuffer sb = new StringBuffer();
  97. for (int idx = 0; idx < s.length();) {
  98. char ch = s.charAt(idx++);
  99. if (ch == '%') {
  100. char ch1 = s.charAt(idx++);
  101. char ch2 = s.charAt(idx++);
  102. char res = (char)(h2c(ch1)*16 + h2c(ch2));
  103. sb.append(res);
  104. } else {
  105. sb.append(ch);
  106. }
  107. }
  108. return sb.toString();
  109. }
  110. /**
  111. *
  112. * @param s
  113. * @return
  114. */
  115. static String toCSVString(String s) {
  116. if (s == null)
  117. return "";
  118. StringBuffer sb = new StringBuffer(s.length()+1);
  119. sb.append('\'');
  120. int len = s.length();
  121. for (int i = 0; i < len; i++) {
  122. char c = s.charAt(i);
  123. switch(c) {
  124. case '\0':
  125. sb.append("%00");
  126. break;
  127. case '\n':
  128. sb.append("%0A");
  129. break;
  130. case '\r':
  131. sb.append("%0D");
  132. break;
  133. case ',':
  134. sb.append("%2C");
  135. break;
  136. case '}':
  137. sb.append("%7D");
  138. break;
  139. case '%':
  140. sb.append("%25");
  141. break;
  142. default:
  143. sb.append(c);
  144. }
  145. }
  146. return sb.toString();
  147. }
  148. /**
  149. *
  150. * @param s
  151. * @throws java.io.IOException
  152. * @return
  153. */
  154. static String fromCSVString(String s) throws IOException {
  155. if (s.charAt(0) != '\'') {
  156. throw new IOException("Error deserializing string.");
  157. }
  158. int len = s.length();
  159. StringBuffer sb = new StringBuffer(len-1);
  160. for (int i = 1; i < len; i++) {
  161. char c = s.charAt(i);
  162. if (c == '%') {
  163. char ch1 = s.charAt(i+1);
  164. char ch2 = s.charAt(i+2);
  165. i += 2;
  166. if (ch1 == '0' && ch2 == '0') { sb.append('\0'); }
  167. else if (ch1 == '0' && ch2 == 'A') { sb.append('\n'); }
  168. else if (ch1 == '0' && ch2 == 'D') { sb.append('\r'); }
  169. else if (ch1 == '2' && ch2 == 'C') { sb.append(','); }
  170. else if (ch1 == '7' && ch2 == 'D') { sb.append('}'); }
  171. else if (ch1 == '2' && ch2 == '5') { sb.append('%'); }
  172. else {throw new IOException("Error deserializing string.");}
  173. } else {
  174. sb.append(c);
  175. }
  176. }
  177. return sb.toString();
  178. }
  179. /**
  180. *
  181. * @param s
  182. * @return
  183. */
  184. static String toXMLBuffer(byte barr[]) {
  185. if (barr == null || barr.length == 0) {
  186. return "";
  187. }
  188. StringBuffer sb = new StringBuffer(2*barr.length);
  189. for (int idx = 0; idx < barr.length; idx++) {
  190. sb.append(Integer.toHexString(barr[idx]));
  191. }
  192. return sb.toString();
  193. }
  194. /**
  195. *
  196. * @param s
  197. * @throws java.io.IOException
  198. * @return
  199. */
  200. static byte[] fromXMLBuffer(String s)
  201. throws IOException {
  202. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  203. if (s.length() == 0) { return stream.toByteArray(); }
  204. int blen = s.length()/2;
  205. byte[] barr = new byte[blen];
  206. for (int idx = 0; idx < blen; idx++) {
  207. char c1 = s.charAt(2*idx);
  208. char c2 = s.charAt(2*idx+1);
  209. barr[idx] = Byte.parseByte(""+c1+c2, 16);
  210. }
  211. stream.write(barr);
  212. return stream.toByteArray();
  213. }
  214. /**
  215. *
  216. * @param buf
  217. * @return
  218. */
  219. static String toCSVBuffer(byte barr[]) {
  220. if (barr == null || barr.length == 0) {
  221. return "";
  222. }
  223. StringBuffer sb = new StringBuffer(barr.length + 1);
  224. sb.append('#');
  225. for(int idx = 0; idx < barr.length; idx++) {
  226. sb.append(Integer.toHexString(barr[idx]));
  227. }
  228. return sb.toString();
  229. }
  230. /**
  231. * Converts a CSV-serialized representation of buffer to a new
  232. * ByteArrayOutputStream.
  233. * @param s CSV-serialized representation of buffer
  234. * @throws java.io.IOException
  235. * @return Deserialized ByteArrayOutputStream
  236. */
  237. static byte[] fromCSVBuffer(String s)
  238. throws IOException {
  239. if (s.charAt(0) != '#') {
  240. throw new IOException("Error deserializing buffer.");
  241. }
  242. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  243. if (s.length() == 1) { return stream.toByteArray(); }
  244. int blen = (s.length()-1)/2;
  245. byte[] barr = new byte[blen];
  246. for (int idx = 0; idx < blen; idx++) {
  247. char c1 = s.charAt(2*idx+1);
  248. char c2 = s.charAt(2*idx+2);
  249. barr[idx] = Byte.parseByte(""+c1+c2, 16);
  250. }
  251. stream.write(barr);
  252. return stream.toByteArray();
  253. }
  254. public static int compareBytes(byte b1[], int off1, int len1, byte b2[], int off2, int len2) {
  255. int i;
  256. for(i=0; i < len1 && i < len2; i++) {
  257. if (b1[off1+i] != b2[off2+i]) {
  258. return b1[off1+i] < b2[off2+1] ? -1 : 1;
  259. }
  260. }
  261. if (len1 != len2) {
  262. return len1 < len2 ? -1 : 1;
  263. }
  264. return 0;
  265. }
  266. }