Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.Reader;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.util.Properties;
  7.  
  8. public class FormattedProperties extends Properties {
  9. @Override
  10. public synchronized void load(InputStream inStream) throws IOException {
  11. load0(new LineReader(inStream));
  12. }
  13.  
  14. @Override
  15. public synchronized void load(Reader reader) throws IOException {
  16. load0(new LineReader(reader));
  17. }
  18.  
  19. private void load0(LineReader lr) throws IOException {
  20. char[] convtBuf = new char[1024];
  21. int limit;
  22. int keyLen;
  23. int valueStart;
  24. char c;
  25. boolean hasSep;
  26. boolean precedingBackslash;
  27.  
  28. while ((limit = lr.readLine()) >= 0) {
  29. c = 0;
  30. keyLen = 0;
  31. valueStart = limit;
  32. hasSep = false;
  33.  
  34. //System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
  35. precedingBackslash = false;
  36. while (keyLen < limit) {
  37. c = lr.lineBuf[keyLen];
  38. //need check if escaped.
  39. if ((c == '=' || c == ':') && !precedingBackslash) {
  40. valueStart = keyLen + 1;
  41. hasSep = true;
  42. break;
  43. } else if ((c == ' ' || c == '\t' || c == '\f') && !precedingBackslash) {
  44. valueStart = keyLen + 1;
  45. break;
  46. }
  47. if (c == '\\') {
  48. precedingBackslash = !precedingBackslash;
  49. } else {
  50. precedingBackslash = false;
  51. }
  52. keyLen++;
  53. }
  54. while (valueStart < limit) {
  55. c = lr.lineBuf[valueStart];
  56. if (c != ' ' && c != '\t' && c != '\f') {
  57. if (!hasSep && (c == '=' || c == ':')) {
  58. hasSep = true;
  59. } else {
  60. break;
  61. }
  62. }
  63. valueStart++;
  64. }
  65. try {
  66. Method method = Properties.class.getDeclaredMethod("loadConvert", char[].class, int.class, int.class, char[].class);
  67. method.setAccessible(true);
  68.  
  69. String key = (String) method.invoke(this, lr.lineBuf, 0, keyLen, convtBuf);
  70. String value = (String) method.invoke(this, lr.lineBuf, valueStart, limit - valueStart, convtBuf);
  71. put(key, value);
  72. } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
  73. e.printStackTrace();
  74. }
  75.  
  76. }
  77. }
  78.  
  79. class LineReader {
  80. public LineReader(InputStream inStream) {
  81. this.inStream = inStream;
  82. inByteBuf = new byte[8192];
  83. }
  84.  
  85. public LineReader(Reader reader) {
  86. this.reader = reader;
  87. inCharBuf = new char[8192];
  88. }
  89.  
  90. byte[] inByteBuf;
  91. char[] inCharBuf;
  92. char[] lineBuf = new char[1024];
  93. int inLimit = 0;
  94. int inOff = 0;
  95. InputStream inStream;
  96. Reader reader;
  97.  
  98. int readLine() throws IOException {
  99. int len = 0;
  100. char c = 0;
  101.  
  102. boolean skipWhiteSpace = true;
  103. boolean isCommentLine = false;
  104. boolean isNewLine = true;
  105. boolean appendedLineBegin = false;
  106. boolean precedingBackslash = false;
  107. boolean skipLF = false;
  108.  
  109. while (true) {
  110. if (inOff >= inLimit) {
  111. inLimit = (inStream == null) ? reader.read(inCharBuf)
  112. : inStream.read(inByteBuf);
  113. inOff = 0;
  114. if (inLimit <= 0) {
  115. if (len == 0 || isCommentLine) {
  116. return -1;
  117. }
  118. if (precedingBackslash) {
  119. len--;
  120. }
  121. return len;
  122. }
  123. }
  124. if (inStream != null) {
  125. //The line below is equivalent to calling a
  126. //ISO8859-1 decoder.
  127. c = (char) (0xff & inByteBuf[inOff++]);
  128. } else {
  129. c = inCharBuf[inOff++];
  130. }
  131. if (skipLF) {
  132. skipLF = false;
  133. if (c == '\n') {
  134. continue;
  135. }
  136. }
  137. if (skipWhiteSpace) {
  138. if (c == '\t' || c == '\f') {
  139. continue;
  140. }
  141. if (!appendedLineBegin && (c == '\r' || c == '\n')) {
  142. continue;
  143. }
  144. skipWhiteSpace = false;
  145. appendedLineBegin = false;
  146. }
  147. if (isNewLine) {
  148. isNewLine = false;
  149. if (c == '#' || c == '!') {
  150. isCommentLine = true;
  151. continue;
  152. }
  153. }
  154.  
  155. if (c != '\n' && c != '\r') {
  156. lineBuf[len++] = c;
  157. if (len == lineBuf.length) {
  158. int newLength = lineBuf.length * 2;
  159. if (newLength < 0) {
  160. newLength = Integer.MAX_VALUE;
  161. }
  162. char[] buf = new char[newLength];
  163. System.arraycopy(lineBuf, 0, buf, 0, lineBuf.length);
  164. lineBuf = buf;
  165. }
  166. //flip the preceding backslash flag
  167. if (c == '\\') {
  168. precedingBackslash = !precedingBackslash;
  169. } else {
  170. precedingBackslash = false;
  171. }
  172. } else {
  173. // reached EOL
  174. if (isCommentLine || len == 0) {
  175. isCommentLine = false;
  176. isNewLine = true;
  177. skipWhiteSpace = true;
  178. len = 0;
  179. continue;
  180. }
  181. if (inOff >= inLimit) {
  182. inLimit = (inStream == null)
  183. ? reader.read(inCharBuf)
  184. : inStream.read(inByteBuf);
  185. inOff = 0;
  186. if (inLimit <= 0) {
  187. if (precedingBackslash) {
  188. len--;
  189. }
  190. return len;
  191. }
  192. }
  193. if (precedingBackslash) {
  194. len -= 1;
  195. //skip the leading whitespace characters in following line
  196. skipWhiteSpace = true;
  197. appendedLineBegin = true;
  198. precedingBackslash = false;
  199. if (c == '\r') {
  200. skipLF = true;
  201. }
  202. } else {
  203. return len;
  204. }
  205. }
  206. }
  207. }
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement