2
0

StringUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  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. /*
  17. * StringUtil.java
  18. */
  19. package org.maxkey.crypto.cert;
  20. import java.io.BufferedReader;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.math.BigInteger;
  26. import java.util.Locale;
  27. import org.bouncycastle.asn1.ASN1Integer;
  28. /**
  29. * String utilities.
  30. *
  31. * @author
  32. */
  33. public class StringUtil
  34. {
  35. /**
  36. * Convert the supplied object to hex characters sub-divided by spaces every given number of characters,
  37. * and left-padded with zeros to fill group size.
  38. *
  39. * @param obj Object (byte array, BigInteger, DERInteger)
  40. * @param groupSize number of characters to group hex characters by
  41. * @param separator grouping separator
  42. * @return Hex string
  43. * @throws IllegalArgumentException if obj is not a BigInteger, byte array, or a DERInteger, or groupSize
  44. * < 0
  45. */
  46. public static StringBuilder toHex(Object obj, int groupSize, String separator)
  47. {
  48. if (groupSize < 0)
  49. {
  50. throw new IllegalArgumentException("Group size must be >= 0");
  51. }
  52. BigInteger bigInt;
  53. if (obj instanceof BigInteger)
  54. {
  55. bigInt = (BigInteger) obj;
  56. }
  57. else if (obj instanceof byte[])
  58. {
  59. bigInt = new BigInteger(1, (byte[]) obj);
  60. }
  61. else if (obj instanceof ASN1Integer)
  62. {
  63. bigInt = ((ASN1Integer) obj).getValue();
  64. }
  65. else
  66. {
  67. throw new IllegalArgumentException("Don't know how to convert " + obj.getClass().getName() +
  68. " to a hex string");
  69. }
  70. // Convert to hex
  71. StringBuilder sb = new StringBuilder(bigInt.toString(16).toUpperCase(Locale.ENGLISH));
  72. // Left-pad if asked and necessary
  73. if (groupSize != 0)
  74. {
  75. int len = groupSize - (sb.length() % groupSize);
  76. if (len != groupSize)
  77. {
  78. for (int i = 0; i < len; i++)
  79. {
  80. sb.insert(0, '0');
  81. }
  82. }
  83. }
  84. // Place separator at every groupSize characters
  85. if (sb.length() > groupSize && !separator.isEmpty())
  86. {
  87. for (int i = groupSize; i < sb.length(); i += groupSize + separator.length())
  88. {
  89. sb.insert(i, separator);
  90. }
  91. }
  92. return sb;
  93. }
  94. // 1. String --> InputStream
  95. public static InputStream String2InputStream(String str) {
  96. ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
  97. return stream;
  98. }
  99. // 2. InputStream --> String
  100. public static String inputStream2String(InputStream is) throws IOException {
  101. BufferedReader in = new BufferedReader(new InputStreamReader(is));
  102. StringBuffer buffer = new StringBuffer();
  103. String line = "";
  104. while ((line = in.readLine()) != null) {
  105. buffer.append(line);
  106. }
  107. return buffer.toString();
  108. }
  109. }