@@ -60,12 +60,21 @@ default void sortUnique(Path path) throws IOException {
60
60
List <String > sortedLines = new ArrayList <>((int )(Files .size (path ) / 100 ));
61
61
Path tempOut = Files .createTempFile ("tempfile" , ".tmp" );
62
62
String header ;
63
+ boolean withHeader = hasHeader ();
63
64
if (path .getFileName ().endsWith (".gz" )) {
64
65
try (InputStream fileIn = Files .newInputStream (path );
65
66
GZIPInputStream gzipIn = new GZIPInputStream (fileIn );
66
67
Reader inReader = new InputStreamReader (gzipIn );
67
68
BufferedReader reader = new BufferedReader (inReader )) {
68
- header = readFile (reader , sortedLines , hasHeader ());
69
+ if (testSortedAndUnique (reader , withHeader )) {
70
+ return ;
71
+ }
72
+ }
73
+ try (InputStream fileIn = Files .newInputStream (path );
74
+ GZIPInputStream gzipIn = new GZIPInputStream (fileIn );
75
+ Reader inReader = new InputStreamReader (gzipIn );
76
+ BufferedReader reader = new BufferedReader (inReader )) {
77
+ header = readFile (reader , sortedLines , withHeader );
69
78
}
70
79
try (OutputStream fileOut = Files .newOutputStream (tempOut );
71
80
GZIPOutputStream gzipOut = new GZIPOutputStream (fileOut );
@@ -74,7 +83,12 @@ default void sortUnique(Path path) throws IOException {
74
83
}
75
84
} else {
76
85
try (BufferedReader reader = Files .newBufferedReader (path )) {
77
- header = readFile (reader , sortedLines , hasHeader ());
86
+ if (testSortedAndUnique (reader , withHeader )) {
87
+ return ;
88
+ }
89
+ }
90
+ try (BufferedReader reader = Files .newBufferedReader (path )) {
91
+ header = readFile (reader , sortedLines , withHeader );
78
92
}
79
93
try (BufferedWriter writer = Files .newBufferedWriter (tempOut )) {
80
94
writeFile (writer , header , sortedLines );
@@ -92,9 +106,6 @@ static String readFile(BufferedReader reader, Collection<String> lines, boolean
92
106
String line = reader .readLine ();
93
107
String header ;
94
108
if (withHeader ) {
95
- if (line == null ) {
96
- throw new IOException ("CSV file does not have header" );
97
- }
98
109
header = line ;
99
110
line = reader .readLine ();
100
111
} else {
@@ -125,4 +136,31 @@ static void writeFile(Writer writer, String header, List<String> lines) throws I
125
136
previousLine = line ;
126
137
}
127
138
}
139
+
140
+ static boolean testSortedAndUnique (BufferedReader reader , boolean withHeader ) throws IOException {
141
+ String line = reader .readLine ();
142
+ if (withHeader ) {
143
+ if (line == null ) {
144
+ throw new IOException ("header expected but not found" );
145
+ }
146
+ line = reader .readLine ();
147
+ }
148
+
149
+ // no lines -> sorted & unique
150
+ if (line == null ) {
151
+ return true ;
152
+ }
153
+
154
+ String previousLine = line ;
155
+ line = reader .readLine ();
156
+
157
+ while (line != null ) {
158
+ if (line .compareTo (previousLine ) <= 0 ) {
159
+ return false ;
160
+ }
161
+ previousLine = line ;
162
+ line = reader .readLine ();
163
+ }
164
+ return true ;
165
+ }
128
166
}
0 commit comments