PageRenderTime 81ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/util/IOUtils.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 208 lines | 139 code | 26 blank | 43 comment | 29 complexity | ad836209c5d80e940686f5877ba7cece MD5 | raw file
  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.fastjson.util;
  17. import java.io.Closeable;
  18. import java.nio.ByteBuffer;
  19. import java.nio.CharBuffer;
  20. import java.nio.charset.CharacterCodingException;
  21. import java.nio.charset.CharsetDecoder;
  22. import java.nio.charset.CoderResult;
  23. import com.alibaba.fastjson.JSONException;
  24. /**
  25. * @author wenshao<szujobs@hotmail.com>
  26. */
  27. public class IOUtils {
  28. public static void close(Closeable x) {
  29. if (x != null) {
  30. try {
  31. x.close();
  32. } catch (Exception e) {
  33. // skip
  34. }
  35. }
  36. }
  37. // Requires positive x
  38. public static int stringSize(long x) {
  39. long p = 10;
  40. for (int i = 1; i < 19; i++) {
  41. if (x < p) return i;
  42. p = 10 * p;
  43. }
  44. return 19;
  45. }
  46. public static void getChars(long i, int index, char[] buf) {
  47. long q;
  48. int r;
  49. int charPos = index;
  50. char sign = 0;
  51. if (i < 0) {
  52. sign = '-';
  53. i = -i;
  54. }
  55. // Get 2 digits/iteration using longs until quotient fits into an int
  56. while (i > Integer.MAX_VALUE) {
  57. q = i / 100;
  58. // really: r = i - (q * 100);
  59. r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
  60. i = q;
  61. buf[--charPos] = DigitOnes[r];
  62. buf[--charPos] = DigitTens[r];
  63. }
  64. // Get 2 digits/iteration using ints
  65. int q2;
  66. int i2 = (int) i;
  67. while (i2 >= 65536) {
  68. q2 = i2 / 100;
  69. // really: r = i2 - (q * 100);
  70. r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
  71. i2 = q2;
  72. buf[--charPos] = DigitOnes[r];
  73. buf[--charPos] = DigitTens[r];
  74. }
  75. // Fall thru to fast mode for smaller numbers
  76. // assert(i2 <= 65536, i2);
  77. for (;;) {
  78. q2 = (i2 * 52429) >>> (16 + 3);
  79. r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
  80. buf[--charPos] = digits[r];
  81. i2 = q2;
  82. if (i2 == 0) break;
  83. }
  84. if (sign != 0) {
  85. buf[--charPos] = sign;
  86. }
  87. }
  88. /**
  89. * Places characters representing the integer i into the character array buf. The characters are placed into the
  90. * buffer backwards starting with the least significant digit at the specified index (exclusive), and working
  91. * backwards from there. Will fail if i == Integer.MIN_VALUE
  92. */
  93. public static void getChars(int i, int index, char[] buf) {
  94. int q, r;
  95. int charPos = index;
  96. char sign = 0;
  97. if (i < 0) {
  98. sign = '-';
  99. i = -i;
  100. }
  101. // Generate two digits per iteration
  102. while (i >= 65536) {
  103. q = i / 100;
  104. // really: r = i - (q * 100);
  105. r = i - ((q << 6) + (q << 5) + (q << 2));
  106. i = q;
  107. buf[--charPos] = DigitOnes[r];
  108. buf[--charPos] = DigitTens[r];
  109. }
  110. // Fall thru to fast mode for smaller numbers
  111. // assert(i <= 65536, i);
  112. for (;;) {
  113. q = (i * 52429) >>> (16 + 3);
  114. r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
  115. buf[--charPos] = digits[r];
  116. i = q;
  117. if (i == 0) break;
  118. }
  119. if (sign != 0) {
  120. buf[--charPos] = sign;
  121. }
  122. }
  123. public static void getChars(byte b, int index, char[] buf) {
  124. int i = b;
  125. int q, r;
  126. int charPos = index;
  127. char sign = 0;
  128. if (i < 0) {
  129. sign = '-';
  130. i = -i;
  131. }
  132. // Fall thru to fast mode for smaller numbers
  133. // assert(i <= 65536, i);
  134. for (;;) {
  135. q = (i * 52429) >>> (16 + 3);
  136. r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
  137. buf[--charPos] = digits[r];
  138. i = q;
  139. if (i == 0) break;
  140. }
  141. if (sign != 0) {
  142. buf[--charPos] = sign;
  143. }
  144. }
  145. /**
  146. * All possible chars for representing a number as a String
  147. */
  148. final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
  149. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  150. final static char[] DigitTens = { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1',
  151. '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3',
  152. '3', '3', '3', '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '5', '5', '5',
  153. '5', '5', '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7', '7',
  154. '7', '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', };
  155. final static char[] DigitOnes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5',
  156. '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6',
  157. '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7',
  158. '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8',
  159. '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', };
  160. final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE };
  161. // Requires positive x
  162. static int stringSize(int x) {
  163. for (int i = 0;; i++)
  164. if (x <= sizeTable[i]) return i + 1;
  165. }
  166. public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
  167. try {
  168. CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);
  169. if (!cr.isUnderflow()) {
  170. cr.throwException();
  171. }
  172. cr = charsetDecoder.flush(charByte);
  173. if (!cr.isUnderflow()) {
  174. cr.throwException();
  175. }
  176. } catch (CharacterCodingException x) {
  177. // Substitution is always enabled,
  178. // so this shouldn't happen
  179. throw new JSONException(x.getMessage(), x);
  180. }
  181. }
  182. }