Utils.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 com.yahoo.jute;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.DataInput;
  21. import java.io.DataOutput;
  22. import java.io.IOException;
  23. import java.io.UnsupportedEncodingException;
  24. import java.nio.charset.CharacterCodingException;
  25. /**
  26. * Various utility functions for Hadooop record I/O runtime.
  27. * @author Milind Bhandarkar
  28. */
  29. public class Utils {
  30. /** Cannot create a new instance of Utils */
  31. private Utils() {
  32. }
  33. /**
  34. * equals function that actually compares two buffers.
  35. *
  36. * @param onearray First buffer
  37. * @param twoarray Second buffer
  38. * @return true if one and two contain exactly the same content, else false.
  39. */
  40. public static boolean bufEquals(byte onearray[], byte twoarray[] ) {
  41. if (onearray == twoarray) return true;
  42. boolean ret = (onearray.length == twoarray.length);
  43. if (!ret) {
  44. return ret;
  45. }
  46. for (int idx = 0; idx < onearray.length; idx++) {
  47. if (onearray[idx] != twoarray[idx]) {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. public static final char[] hexchars = { '0', '1', '2', '3', '4', '5',
  54. '6', '7', '8', '9', 'A', 'B',
  55. 'C', 'D', 'E', 'F' };
  56. /**
  57. *
  58. * @param s
  59. * @return
  60. */
  61. static String toXMLString(String t) {
  62. String s = t.toString();
  63. StringBuffer sb = new StringBuffer();
  64. for (int idx = 0; idx < s.length(); idx++) {
  65. char ch = s.charAt(idx);
  66. if (ch == '<') {
  67. sb.append("&lt;");
  68. } else if (ch == '&') {
  69. sb.append("&amp;");
  70. } else if (ch == '%') {
  71. sb.append("%25");
  72. } else if (ch < 0x20) {
  73. sb.append("%");
  74. sb.append(hexchars[ch/16]);
  75. sb.append(hexchars[ch%16]);
  76. } else {
  77. sb.append(ch);
  78. }
  79. }
  80. return sb.toString();
  81. }
  82. static private int h2c(char ch) {
  83. if (ch >= '0' && ch <= '9') {
  84. return ch - '0';
  85. } else if (ch >= 'A' && ch <= 'F') {
  86. return ch - 'A';
  87. } else if (ch >= 'a' && ch <= 'f') {
  88. return ch - 'a';
  89. }
  90. return 0;
  91. }
  92. /**
  93. *
  94. * @param s
  95. * @return
  96. */
  97. static String fromXMLString(String s) {
  98. StringBuffer sb = new StringBuffer();
  99. for (int idx = 0; idx < s.length();) {
  100. char ch = s.charAt(idx++);
  101. if (ch == '%') {
  102. char ch1 = s.charAt(idx++);
  103. char ch2 = s.charAt(idx++);
  104. char res = (char)(h2c(ch1)*16 + h2c(ch2));
  105. sb.append(res);
  106. } else {
  107. sb.append(ch);
  108. }
  109. }
  110. return sb.toString();
  111. }
  112. /**
  113. *
  114. * @param s
  115. * @return
  116. */
  117. static String toCSVString(String t) {
  118. String s = t.toString();
  119. StringBuffer sb = new StringBuffer(s.length()+1);
  120. sb.append('\'');
  121. int len = s.length();
  122. for (int i = 0; i < len; i++) {
  123. char c = s.charAt(i);
  124. switch(c) {
  125. case '\0':
  126. sb.append("%00");
  127. break;
  128. case '\n':
  129. sb.append("%0A");
  130. break;
  131. case '\r':
  132. sb.append("%0D");
  133. break;
  134. case ',':
  135. sb.append("%2C");
  136. break;
  137. case '}':
  138. sb.append("%7D");
  139. break;
  140. case '%':
  141. sb.append("%25");
  142. break;
  143. default:
  144. sb.append(c);
  145. }
  146. }
  147. return sb.toString();
  148. }
  149. /**
  150. *
  151. * @param s
  152. * @throws java.io.IOException
  153. * @return
  154. */
  155. static String fromCSVString(String s) throws IOException {
  156. if (s.charAt(0) != '\'') {
  157. throw new IOException("Error deserializing string.");
  158. }
  159. int len = s.length();
  160. StringBuffer sb = new StringBuffer(len-1);
  161. for (int i = 1; i < len; i++) {
  162. char c = s.charAt(i);
  163. if (c == '%') {
  164. char ch1 = s.charAt(i+1);
  165. char ch2 = s.charAt(i+2);
  166. i += 2;
  167. if (ch1 == '0' && ch2 == '0') { sb.append('\0'); }
  168. else if (ch1 == '0' && ch2 == 'A') { sb.append('\n'); }
  169. else if (ch1 == '0' && ch2 == 'D') { sb.append('\r'); }
  170. else if (ch1 == '2' && ch2 == 'C') { sb.append(','); }
  171. else if (ch1 == '7' && ch2 == 'D') { sb.append('}'); }
  172. else if (ch1 == '2' && ch2 == '5') { sb.append('%'); }
  173. else {throw new IOException("Error deserializing string.");}
  174. } else {
  175. sb.append(c);
  176. }
  177. }
  178. return sb.toString();
  179. }
  180. /**
  181. *
  182. * @param s
  183. * @return
  184. */
  185. static String toXMLBuffer(byte barr[]) {
  186. StringBuffer sb = new StringBuffer(2*barr.length);
  187. for (int idx = 0; idx < barr.length; idx++) {
  188. sb.append(Integer.toHexString((int)barr[idx]));
  189. }
  190. return sb.toString();
  191. }
  192. /**
  193. *
  194. * @param s
  195. * @throws java.io.IOException
  196. * @return
  197. */
  198. static byte[] fromXMLBuffer(String s)
  199. throws IOException {
  200. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  201. if (s.length() == 0) { return stream.toByteArray(); }
  202. int blen = s.length()/2;
  203. byte[] barr = new byte[blen];
  204. for (int idx = 0; idx < blen; idx++) {
  205. char c1 = s.charAt(2*idx);
  206. char c2 = s.charAt(2*idx+1);
  207. barr[idx] = Byte.parseByte(""+c1+c2, 16);
  208. }
  209. stream.write(barr);
  210. return stream.toByteArray();
  211. }
  212. /**
  213. *
  214. * @param buf
  215. * @return
  216. */
  217. static String toCSVBuffer(byte barr[]) {
  218. StringBuffer sb = new StringBuffer(barr.length+1);
  219. sb.append('#');
  220. for(int idx = 0; idx < barr.length; idx++) {
  221. sb.append(Integer.toHexString((int)barr[idx]));
  222. }
  223. return sb.toString();
  224. }
  225. /**
  226. * Converts a CSV-serialized representation of buffer to a new
  227. * ByteArrayOutputStream.
  228. * @param s CSV-serialized representation of buffer
  229. * @throws java.io.IOException
  230. * @return Deserialized ByteArrayOutputStream
  231. */
  232. static byte[] fromCSVBuffer(String s)
  233. throws IOException {
  234. if (s.charAt(0) != '#') {
  235. throw new IOException("Error deserializing buffer.");
  236. }
  237. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  238. if (s.length() == 1) { return stream.toByteArray(); }
  239. int blen = (s.length()-1)/2;
  240. byte[] barr = new byte[blen];
  241. for (int idx = 0; idx < blen; idx++) {
  242. char c1 = s.charAt(2*idx+1);
  243. char c2 = s.charAt(2*idx+2);
  244. barr[idx] = Byte.parseByte(""+c1+c2, 16);
  245. }
  246. stream.write(barr);
  247. return stream.toByteArray();
  248. }
  249. public static int compareBytes(byte b1[], int off1, int len1, byte b2[], int off2, int len2) {
  250. int i;
  251. for(i=0; i < len1 && i < len2; i++) {
  252. if (b1[off1+i] != b2[off2+i]) {
  253. return b1[off1+i] < b2[off2+1] ? -1 : 1;
  254. }
  255. }
  256. if (len1 != len2) {
  257. return len1 < len2 ? -1 : 1;
  258. }
  259. return 0;
  260. }
  261. }