Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.io.Reader;
  5. import java.nio.CharBuffer;
  6. import java.util.ArrayList;
  7. import java.util.Collections;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10.  
  11. /**
  12. * Simple symbol CSV reader class.
  13. *
  14. * @author cab404
  15. */
  16. public class CSVReader {
  17. private final static int INROW = 0;
  18. private final static int INSTRING = 1;
  19. private final static int ENDSTRING = 2;
  20.  
  21. private List<List<String>> table = new LinkedList<List<String>>();
  22. private List<String> currentRow = new ArrayList<String>();
  23. private StringBuilder buff = new StringBuilder();
  24. private int state = 0;
  25.  
  26. private final char textDelimeter;
  27. private final char rowDelimeter;
  28. private final char separator;
  29.  
  30. public CSVReader(char separator, char textDelimeter) {
  31. this.textDelimeter = textDelimeter;
  32. this.rowDelimeter = '\n';
  33. this.separator = separator;
  34. }
  35.  
  36. public CSVReader(char separator, char textDelimeter, char rowDelimeter) {
  37. this.textDelimeter = textDelimeter;
  38. this.rowDelimeter = rowDelimeter;
  39. this.separator = separator;
  40. }
  41.  
  42. /**
  43. * Parses next chunk of data.
  44. */
  45. public void read(CharSequence sequence) {
  46. for (int i = 0; i < sequence.length(); i++) {
  47. char ch = sequence.charAt(i);
  48. switch (state) {
  49. case INROW:
  50. if (ch == rowDelimeter) {
  51. flushRow();
  52. continue;
  53. }
  54. if (ch == textDelimeter) {
  55. state = INSTRING;
  56. continue;
  57. }
  58. if (ch == separator) {
  59. flushCell();
  60. continue;
  61. }
  62. buff.append(ch);
  63. continue;
  64. case INSTRING:
  65. if (ch == textDelimeter) {
  66. state = ENDSTRING;
  67. continue;
  68. }
  69. buff.append(ch);
  70. continue;
  71. case ENDSTRING:
  72. if (ch == rowDelimeter) {
  73. flushRow();
  74. continue;
  75. }
  76. if (ch == separator) {
  77. flushCell();
  78. continue;
  79. }
  80. if (ch == textDelimeter) {
  81. state = INSTRING;
  82. buff.append(textDelimeter);
  83. continue;
  84. }
  85. continue;
  86. }
  87. }
  88. }
  89.  
  90. private void flushCell() {
  91. state = INROW;
  92. currentRow.add(buff.toString());
  93. buff.setLength(0);
  94. }
  95.  
  96. private void flushRow() {
  97. flushCell();
  98. table.add(Collections.unmodifiableList(currentRow));
  99. currentRow = new ArrayList<String>();
  100. }
  101.  
  102. public List<List<String>> getTable() {
  103. return Collections.unmodifiableList(table);
  104. }
  105.  
  106. public void read(Reader reader) throws IOException {
  107. CharBuffer buffer = CharBuffer.allocate(256);
  108. while (reader.read(buffer) != -1) {
  109. buffer.flip();
  110. read(buffer);
  111. buffer.clear();
  112. }
  113. }
  114.  
  115. public void read(InputStream stream) throws IOException {
  116. read(new InputStreamReader(stream));
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement