embulk-input-filesplit 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+ package org.embulk.input.filesplit;
2
+
3
+ import java.io.BufferedReader;
4
+ import java.io.BufferedWriter;
5
+ import java.io.File;
6
+ import java.io.FileReader;
7
+ import java.io.FileWriter;
8
+ import java.io.IOException;
9
+ import java.net.URISyntaxException;
10
+ import java.util.Arrays;
11
+ import java.util.regex.Matcher;
12
+ import java.util.regex.Pattern;
13
+
14
+ import org.embulk.EmbulkService;
15
+ import org.embulk.config.ConfigLoader;
16
+ import org.embulk.config.ConfigSource;
17
+ import org.embulk.exec.ExecutionResult;
18
+ import org.embulk.exec.LocalExecutor;
19
+ import org.embulk.plugin.InjectedPluginSource;
20
+ import org.embulk.spi.ExecSession;
21
+
22
+ import com.google.inject.Binder;
23
+ import com.google.inject.Injector;
24
+ import com.google.inject.Module;
25
+
26
+ public class EmbulkPluginTester {
27
+
28
+ private final Class<?> iface;
29
+ private final String name;
30
+ private final Class<?> impl;
31
+
32
+
33
+ public EmbulkPluginTester(Class<?> iface, String name, Class<?> impl)
34
+ {
35
+ this.iface = iface;
36
+ this.name = name;
37
+ this.impl = impl;
38
+ }
39
+
40
+ public void run(String ymlPath) throws Exception
41
+ {
42
+ EmbulkService service = new EmbulkService(new EmptyConfigSource()) {
43
+ protected Iterable<? extends Module> getAdditionalModules(ConfigSource systemConfig)
44
+ {
45
+ return Arrays.asList(new Module()
46
+ {
47
+ @Override
48
+ public void configure(Binder binder)
49
+ {
50
+ InjectedPluginSource.registerPluginTo(binder, iface, name, impl);
51
+ }
52
+ });
53
+ }
54
+ };
55
+ Injector injector = service.getInjector();
56
+ ConfigSource config = injector.getInstance(ConfigLoader.class).fromYamlFile(convert(ymlPath));
57
+ ExecSession session = new ExecSession(injector, config);
58
+ LocalExecutor executor = injector.getInstance(LocalExecutor.class);
59
+ ExecutionResult result = executor.run(session, config);
60
+ System.out.println(result);
61
+ }
62
+
63
+ private File convert(String yml) {
64
+ try {
65
+ File rootPath = new File(EmbulkPluginTester.class.getResource("/resource.txt").toURI()).getParentFile();
66
+ File ymlPath = new File(EmbulkPluginTester.class.getResource(yml).toURI());
67
+ File tempYmlPath = new File(ymlPath.getParentFile(), "temp-" + ymlPath.getName());
68
+ Pattern pathPrefixPattern = Pattern.compile("^ *path(_prefix)?: '(.*)'$");
69
+ try (BufferedReader reader = new BufferedReader(new FileReader(ymlPath))) {
70
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempYmlPath))) {
71
+ String line;
72
+ while ((line = reader.readLine()) != null) {
73
+ Matcher matcher = pathPrefixPattern.matcher(line);
74
+ if (matcher.matches()) {
75
+ int group = 2;
76
+ writer.write(line.substring(0, matcher.start(group)));
77
+ writer.write(new File(rootPath, matcher.group(group)).getAbsolutePath());
78
+ writer.write(line.substring(matcher.end(group)));
79
+ } else {
80
+ writer.write(line);
81
+ }
82
+ writer.newLine();
83
+ }
84
+ }
85
+ }
86
+ return tempYmlPath.getAbsoluteFile();
87
+
88
+ } catch (IOException e) {
89
+ throw new RuntimeException(e);
90
+ } catch (URISyntaxException e) {
91
+ throw new RuntimeException(e);
92
+ }
93
+ }
94
+
95
+ }
@@ -0,0 +1,107 @@
1
+ package org.embulk.input.filesplit;
2
+
3
+ import java.util.Collections;
4
+ import java.util.List;
5
+ import java.util.Map.Entry;
6
+
7
+ import org.embulk.config.ConfigSource;
8
+ import org.embulk.config.DataSource;
9
+
10
+ import com.fasterxml.jackson.databind.JsonNode;
11
+ import com.fasterxml.jackson.databind.node.ObjectNode;
12
+
13
+ public class EmptyConfigSource implements ConfigSource
14
+ {
15
+
16
+ @Override
17
+ public <E> E get(Class<E> type, String attrName)
18
+ {
19
+ return null;
20
+ }
21
+
22
+ @Override
23
+ public <E> E get(Class<E> type, String attrName, E defaultValue)
24
+ {
25
+ return defaultValue;
26
+ }
27
+
28
+ @Override
29
+ public List<String> getAttributeNames()
30
+ {
31
+ return Collections.emptyList();
32
+ }
33
+
34
+ @Override
35
+ public Iterable<Entry<String, JsonNode>> getAttributes()
36
+ {
37
+ return Collections.emptyList();
38
+ }
39
+
40
+ @Override
41
+ public ObjectNode getObjectNode()
42
+ {
43
+ return null;
44
+ }
45
+
46
+ @Override
47
+ public boolean isEmpty()
48
+ {
49
+ return true;
50
+ }
51
+
52
+ @Override
53
+ public ConfigSource deepCopy()
54
+ {
55
+ return this;
56
+ }
57
+
58
+ @Override
59
+ public ConfigSource getNested(String s)
60
+ {
61
+ // TODO 自動生成されたメソッド・スタブ
62
+ return null;
63
+ }
64
+
65
+ @Override
66
+ public ConfigSource getNestedOrSetEmpty(String s)
67
+ {
68
+ // TODO 自動生成されたメソッド・スタブ
69
+ return null;
70
+ }
71
+
72
+ @Override
73
+ public <T> T loadConfig(Class<T> class1)
74
+ {
75
+ // TODO 自動生成されたメソッド・スタブ
76
+ return null;
77
+ }
78
+
79
+ @Override
80
+ public ConfigSource merge(DataSource datasource)
81
+ {
82
+ // TODO 自動生成されたメソッド・スタブ
83
+ return null;
84
+ }
85
+
86
+ @Override
87
+ public ConfigSource set(String s, Object obj)
88
+ {
89
+ // TODO 自動生成されたメソッド・スタブ
90
+ return null;
91
+ }
92
+
93
+ @Override
94
+ public ConfigSource setAll(DataSource datasource)
95
+ {
96
+ // TODO 自動生成されたメソッド・スタブ
97
+ return null;
98
+ }
99
+
100
+ @Override
101
+ public ConfigSource setNested(String s, DataSource datasource)
102
+ {
103
+ // TODO 自動生成されたメソッド・スタブ
104
+ return null;
105
+ }
106
+
107
+ }
@@ -0,0 +1,94 @@
1
+ package org.embulk.input.filesplit;
2
+
3
+ import java.io.File;
4
+ import java.io.IOException;
5
+ import java.net.URISyntaxException;
6
+ import java.nio.charset.Charset;
7
+ import java.nio.file.FileSystem;
8
+ import java.nio.file.FileSystems;
9
+ import java.nio.file.Files;
10
+ import java.util.ArrayList;
11
+ import java.util.Collections;
12
+ import java.util.List;
13
+
14
+ import org.embulk.spi.InputPlugin;
15
+ import org.junit.Test;
16
+ import static org.junit.Assert.assertEquals;
17
+
18
+ public class LocalFileSplitInputPluginTest {
19
+
20
+ private EmbulkPluginTester tester = new EmbulkPluginTester(InputPlugin.class, "filesplit", LocalFileSplitInputPlugin.class);
21
+
22
+ @Test
23
+ public void test() throws Exception
24
+ {
25
+ run("/yml/test.yml", "/data/test.csv");
26
+ }
27
+
28
+ @Test
29
+ public void testTasks() throws Exception
30
+ {
31
+ run("/yml/test-tasks.yml", "/data/test.csv");
32
+ }
33
+
34
+ @Test
35
+ public void testHeader() throws Exception
36
+ {
37
+ run("/yml/test-header.yml", "/data/test.csv");
38
+ }
39
+
40
+ @Test
41
+ public void testOnlyHeader() throws Exception
42
+ {
43
+ run("/yml/test-only-header.yml", "/data/empty.csv");
44
+ }
45
+
46
+ private void run(String ymlPath, String expectedName) throws Exception
47
+ {
48
+ List<String> expected = readAll(expectedName);
49
+ Collections.sort(expected);
50
+
51
+ File file = prepare();
52
+ tester.run(ymlPath);
53
+
54
+ List<String> actual= readAll(file);
55
+ Collections.sort(actual);
56
+
57
+ assertEquals(expected, actual);
58
+ }
59
+
60
+ private File prepare() throws URISyntaxException
61
+ {
62
+ File file = new File(new File(getClass().getResource("/resource.txt").toURI()).getParentFile(), "temp");
63
+ file.mkdir();
64
+ for (File child : file.listFiles()) {
65
+ child.delete();
66
+ }
67
+ return file;
68
+ }
69
+
70
+ private List<String> readAll(String name) throws IOException, URISyntaxException
71
+ {
72
+ return readAll(new File(getClass().getResource(name).toURI()));
73
+ }
74
+
75
+ private List<String> readAll(File file) throws IOException
76
+ {
77
+ if (file.isFile()) {
78
+ FileSystem fs = FileSystems.getDefault();
79
+ Charset charset = Charset.forName("UTF-8");
80
+ return Files.readAllLines(fs.getPath(file.getAbsolutePath()), charset);
81
+ }
82
+
83
+ if (file.isDirectory()) {
84
+ List<String> lines = new ArrayList<String>();
85
+ for (File child : file.listFiles()) {
86
+ lines.addAll(readAll(child));
87
+ }
88
+ return lines;
89
+ }
90
+
91
+ return Collections.emptyList();
92
+ }
93
+
94
+ }
@@ -0,0 +1,81 @@
1
+ /*
2
+ * $Id: typical.epf 2627 2010-03-18 01:40:13Z tiba $
3
+ */
4
+ package org.embulk.input.filesplit;
5
+
6
+ import static org.junit.Assert.assertEquals;
7
+
8
+ import java.io.BufferedReader;
9
+ import java.io.File;
10
+ import java.io.IOException;
11
+ import java.io.InputStream;
12
+ import java.io.InputStreamReader;
13
+ import java.net.URISyntaxException;
14
+
15
+ import org.embulk.input.filesplit.LocalFileSplitInputPlugin.LocalFileSplitInput.FileSplitProvider;
16
+ import org.junit.Test;
17
+
18
+ public class LocalFileSplitInputTest {
19
+
20
+ @Test
21
+ public void testHeader() throws Exception
22
+ {
23
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 0, 20)))) {
24
+ assertEquals("id,name,value", reader.readLine());
25
+ assertEquals("1,aaaaa,12345", reader.readLine());
26
+ assertEquals(null, reader.readLine());
27
+ }
28
+
29
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 0, 10)))) {
30
+ assertEquals("id,name,value", reader.readLine());
31
+ assertEquals(null, reader.readLine());
32
+ }
33
+
34
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 1, 2)))) {
35
+ assertEquals("id,name,value", reader.readLine());
36
+ assertEquals(null, reader.readLine());
37
+ }
38
+
39
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 1, 20)))) {
40
+ assertEquals("id,name,value", reader.readLine());
41
+ assertEquals("1,aaaaa,12345", reader.readLine());
42
+ assertEquals(null, reader.readLine());
43
+ }
44
+
45
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 1, 40)))) {
46
+ assertEquals("id,name,value", reader.readLine());
47
+ assertEquals("1,aaaaa,12345", reader.readLine());
48
+ assertEquals("2,bbb,67890", reader.readLine());
49
+ assertEquals(null, reader.readLine());
50
+ }
51
+
52
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-header.csv", 20, 40)))) {
53
+ assertEquals("id,name,value", reader.readLine());
54
+ assertEquals("2,bbb,67890", reader.readLine());
55
+ assertEquals(null, reader.readLine());
56
+ }
57
+ }
58
+
59
+ @Test
60
+ public void testOnlyHeader() throws Exception
61
+ {
62
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-only-header.csv", 0, 10)))) {
63
+ assertEquals("id,name,value", reader.readLine());
64
+ assertEquals(null, reader.readLine());
65
+ }
66
+
67
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(open("/data/test-only-header.csv", 1, 10)))) {
68
+ assertEquals("id,name,value", reader.readLine());
69
+ assertEquals(null, reader.readLine());
70
+ }
71
+ }
72
+
73
+ private InputStream open(String name, int start, int end) throws IOException, URISyntaxException
74
+ {
75
+ File path = new File(getClass().getResource(name).toURI());
76
+ try (FileSplitProvider provider = new FileSplitProvider(new PartialFile(path.getAbsolutePath(), start, end), true)) {
77
+ return provider.openNext();
78
+ }
79
+ }
80
+
81
+ }
@@ -0,0 +1,570 @@
1
+ package org.embulk.input.filesplit;
2
+
3
+ import java.io.ByteArrayInputStream;
4
+ import java.io.IOException;
5
+ import java.io.InputStream;
6
+ import java.io.UnsupportedEncodingException;
7
+
8
+ import org.junit.Test;
9
+
10
+ import static org.junit.Assert.assertEquals;
11
+
12
+ public class PartialFileInputStreamTest
13
+ {
14
+ private byte[] buffer = new byte[8];
15
+
16
+ @Test
17
+ public void testEmpty() throws IOException
18
+ {
19
+ try (InputStream in = createInput("", 0, 0)) {
20
+ assertEquals(-1, in.read());
21
+ }
22
+ try (InputStream in = createInput("", 0, 0)) {
23
+ assertEquals(-1, in.read(buffer));
24
+ }
25
+ }
26
+
27
+ @Test
28
+ public void testNoLineBreak() throws IOException
29
+ {
30
+ try (InputStream in = createInput("12345", 0, 4)) {
31
+ assertEquals('1', in.read());
32
+ assertEquals('2', in.read());
33
+ assertEquals('3', in.read());
34
+ assertEquals('4', in.read());
35
+ assertEquals('5', in.read());
36
+ assertEquals(-1, in.read());
37
+ }
38
+ try (InputStream in = createInput("12345", 0, 4)) {
39
+ assertEquals(5, in.read(buffer));
40
+ assertEquals('1', buffer[0]);
41
+ assertEquals('2', buffer[1]);
42
+ assertEquals('3', buffer[2]);
43
+ assertEquals('4', buffer[3]);
44
+ assertEquals('5', buffer[4]);
45
+ }
46
+
47
+ try (InputStream in = createInput("12345", 1, 4)) {
48
+ assertEquals(-1, in.read());
49
+ }
50
+ try (InputStream in = createInput("12345", 1, 4)) {
51
+ assertEquals(-1, in.read(buffer));
52
+ }
53
+ }
54
+
55
+ @Test
56
+ public void testLF() throws IOException
57
+ {
58
+ try (InputStream in = createInput("12345\n67\n89", 0, 4)) {
59
+ assertEquals('1', in.read());
60
+ assertEquals('2', in.read());
61
+ assertEquals('3', in.read());
62
+ assertEquals('4', in.read());
63
+ assertEquals('5', in.read());
64
+ assertEquals('\n', in.read());
65
+ assertEquals(-1, in.read());
66
+ }
67
+ try (InputStream in = createInput("12345\n67\n89", 0, 4)) {
68
+ assertEquals(6, in.read(buffer));
69
+ assertEquals('1', buffer[0]);
70
+ assertEquals('2', buffer[1]);
71
+ assertEquals('3', buffer[2]);
72
+ assertEquals('4', buffer[3]);
73
+ assertEquals('5', buffer[4]);
74
+ assertEquals('\n', buffer[5]);
75
+ }
76
+
77
+ try (InputStream in = createInput("12345\n67\n89", 1, 4)) {
78
+ assertEquals(-1, in.read());
79
+ }
80
+ try (InputStream in = createInput("12345\n67\n89", 1, 4)) {
81
+ assertEquals(-1, in.read(buffer));
82
+ }
83
+
84
+ try (InputStream in = createInput("12345\n67\n89", 1, 7)) {
85
+ assertEquals('6', in.read());
86
+ assertEquals('7', in.read());
87
+ assertEquals('\n', in.read());
88
+ assertEquals(-1, in.read());
89
+ }
90
+ try (InputStream in = createInput("12345\n67\n89", 1, 7)) {
91
+ assertEquals(3, in.read(buffer));
92
+ assertEquals('6', buffer[0]);
93
+ assertEquals('7', buffer[1]);
94
+ assertEquals('\n', buffer[2]);
95
+ }
96
+
97
+ try (InputStream in = createInput("12345\n67\n89", 1, 10)) {
98
+ assertEquals('6', in.read());
99
+ assertEquals('7', in.read());
100
+ assertEquals('\n', in.read());
101
+ assertEquals('8', in.read());
102
+ assertEquals('9', in.read());
103
+ assertEquals(-1, in.read());
104
+ }
105
+ try (InputStream in = createInput("12345\n67\n89", 1, 10)) {
106
+ assertEquals(5, in.read(buffer));
107
+ assertEquals('6', buffer[0]);
108
+ assertEquals('7', buffer[1]);
109
+ assertEquals('\n', buffer[2]);
110
+ assertEquals('8', buffer[3]);
111
+ assertEquals('9', buffer[4]);
112
+ }
113
+
114
+ try (InputStream in = createInput("12345\n67\n89", 10, 11)) {
115
+ assertEquals(-1, in.read());
116
+ }
117
+ try (InputStream in = createInput("12345\n67\n89", 10, 11)) {
118
+ assertEquals(-1, in.read(buffer));
119
+ }
120
+
121
+ // \nの直後まで
122
+ try (InputStream in = createInput("12345\n67\n89", 4, 9)) {
123
+ assertEquals('6', in.read());
124
+ assertEquals('7', in.read());
125
+ assertEquals('\n', in.read());
126
+ assertEquals(-1, in.read());
127
+ }
128
+ try (InputStream in = createInput("12345\n67\n89", 4, 9)) {
129
+ assertEquals(3, in.read(buffer));
130
+ assertEquals('6', buffer[0]);
131
+ assertEquals('7', buffer[1]);
132
+ assertEquals('\n', buffer[2]);
133
+ assertEquals(-1, in.read(buffer));
134
+ }
135
+ try (InputStream in = createInput("12345\n67\n89", 4, 9)) {
136
+ assertEquals(3, in.read(buffer, 0, 3));
137
+ assertEquals('6', buffer[0]);
138
+ assertEquals('7', buffer[1]);
139
+ assertEquals('\n', buffer[2]);
140
+ assertEquals(-1, in.read(buffer));
141
+ }
142
+ // \nの直後から
143
+ try (InputStream in = createInput("12345\n67\n89", 9, 11)) {
144
+ assertEquals('8', in.read());
145
+ assertEquals('9', in.read());
146
+ assertEquals(-1, in.read());
147
+ }
148
+ try (InputStream in = createInput("12345\n67\n89", 9, 11)) {
149
+ assertEquals(2, in.read(buffer));
150
+ assertEquals('8', buffer[0]);
151
+ assertEquals('9', buffer[1]);
152
+ }
153
+
154
+ // \nの直前まで
155
+ try (InputStream in = createInput("12345\n67\n89", 4, 8)) {
156
+ assertEquals('6', in.read());
157
+ assertEquals('7', in.read());
158
+ assertEquals('\n', in.read());
159
+ assertEquals(-1, in.read());
160
+ }
161
+ try (InputStream in = createInput("12345\n67\n89", 4, 8)) {
162
+ assertEquals(3, in.read(buffer));
163
+ assertEquals('6', buffer[0]);
164
+ assertEquals('7', buffer[1]);
165
+ assertEquals('\n', buffer[2]);
166
+ }
167
+
168
+ // \nの直前から
169
+ try (InputStream in = createInput("12345\n67\n89", 8, 11)) {
170
+ assertEquals('8', in.read());
171
+ assertEquals('9', in.read());
172
+ assertEquals(-1, in.read());
173
+ }
174
+ try (InputStream in = createInput("12345\n67\n89", 8, 11)) {
175
+ assertEquals(2, in.read(buffer));
176
+ assertEquals('8', buffer[0]);
177
+ assertEquals('9', buffer[1]);
178
+ }
179
+
180
+ // \nで終わる(最後まで)
181
+ try (InputStream in = createInput("12345\n67\n", 5, 9)) {
182
+ assertEquals('6', in.read());
183
+ assertEquals('7', in.read());
184
+ assertEquals('\n', in.read());
185
+ assertEquals(-1, in.read());
186
+ }
187
+ try (InputStream in = createInput("12345\n67\n", 5, 9)) {
188
+ assertEquals(3, in.read(buffer));
189
+ assertEquals('6', buffer[0]);
190
+ assertEquals('7', buffer[1]);
191
+ assertEquals('\n', buffer[2]);
192
+ }
193
+
194
+ // \nで終わる(直前まで)
195
+ try (InputStream in = createInput("12345\n67\n", 5, 8)) {
196
+ assertEquals('6', in.read());
197
+ assertEquals('7', in.read());
198
+ assertEquals('\n', in.read());
199
+ assertEquals(-1, in.read());
200
+ }
201
+ try (InputStream in = createInput("12345\n67\n", 5, 8)) {
202
+ assertEquals(3, in.read(buffer));
203
+ assertEquals('6', buffer[0]);
204
+ assertEquals('7', buffer[1]);
205
+ assertEquals('\n', buffer[2]);
206
+ }
207
+ }
208
+
209
+
210
+ @Test
211
+ public void testCRLF() throws IOException
212
+ {
213
+ try (InputStream in = createInput("12345\r\n67\r\n89", 0, 4)) {
214
+ assertEquals('1', in.read());
215
+ assertEquals('2', in.read());
216
+ assertEquals('3', in.read());
217
+ assertEquals('4', in.read());
218
+ assertEquals('5', in.read());
219
+ assertEquals('\r', in.read());
220
+ assertEquals('\n', in.read());
221
+ assertEquals(-1, in.read());
222
+ }
223
+ try (InputStream in = createInput("12345\r\n67\r\n89", 0, 4)) {
224
+ assertEquals(7, in.read(buffer));
225
+ assertEquals('1', buffer[0]);
226
+ assertEquals('2', buffer[1]);
227
+ assertEquals('3', buffer[2]);
228
+ assertEquals('4', buffer[3]);
229
+ assertEquals('5', buffer[4]);
230
+ assertEquals('\r', buffer[5]);
231
+ assertEquals('\n', buffer[6]);
232
+ }
233
+
234
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 4)) {
235
+ assertEquals(-1, in.read());
236
+ }
237
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 4)) {
238
+ assertEquals(-1, in.read(buffer));
239
+ }
240
+
241
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 8)) {
242
+ assertEquals('6', in.read());
243
+ assertEquals('7', in.read());
244
+ assertEquals('\r', in.read());
245
+ assertEquals('\n', in.read());
246
+ assertEquals(-1, in.read());
247
+ }
248
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 8)) {
249
+ assertEquals(4, in.read(buffer));
250
+ assertEquals('6', buffer[0]);
251
+ assertEquals('7', buffer[1]);
252
+ assertEquals('\r', buffer[2]);
253
+ assertEquals('\n', buffer[3]);
254
+ }
255
+
256
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 12)) {
257
+ assertEquals('6', in.read());
258
+ assertEquals('7', in.read());
259
+ assertEquals('\r', in.read());
260
+ assertEquals('\n', in.read());
261
+ assertEquals('8', in.read());
262
+ assertEquals('9', in.read());
263
+ assertEquals(-1, in.read());
264
+ }
265
+ try (InputStream in = createInput("12345\r\n67\r\n89", 1, 12)) {
266
+ assertEquals(6, in.read(buffer));
267
+ assertEquals('6', buffer[0]);
268
+ assertEquals('7', buffer[1]);
269
+ assertEquals('\r', buffer[2]);
270
+ assertEquals('\n', buffer[3]);
271
+ assertEquals('8', buffer[4]);
272
+ assertEquals('9', buffer[5]);
273
+ }
274
+
275
+ try (InputStream in = createInput("12345\r\n67\r\n89", 12, 13)) {
276
+ assertEquals(-1, in.read());
277
+ }
278
+ try (InputStream in = createInput("12345\r\n67\r\n89", 12, 13)) {
279
+ assertEquals(-1, in.read(buffer));
280
+ }
281
+
282
+ // \nの直後まで
283
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 11)) {
284
+ assertEquals('6', in.read());
285
+ assertEquals('7', in.read());
286
+ assertEquals('\r', in.read());
287
+ assertEquals('\n', in.read());
288
+ assertEquals(-1, in.read());
289
+ }
290
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 11)) {
291
+ assertEquals(4, in.read(buffer));
292
+ assertEquals('6', buffer[0]);
293
+ assertEquals('7', buffer[1]);
294
+ assertEquals('\r', buffer[2]);
295
+ assertEquals('\n', buffer[3]);
296
+ assertEquals(-1, in.read(buffer));
297
+ }
298
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 11)) {
299
+ assertEquals(4, in.read(buffer, 0, 4));
300
+ assertEquals('6', buffer[0]);
301
+ assertEquals('7', buffer[1]);
302
+ assertEquals('\r', buffer[2]);
303
+ assertEquals('\n', buffer[3]);
304
+ assertEquals(-1, in.read(buffer));
305
+ }
306
+ // \nの直後から
307
+ try (InputStream in = createInput("12345\r\n67\r\n89", 11, 13)) {
308
+ assertEquals('8', in.read());
309
+ assertEquals('9', in.read());
310
+ assertEquals(-1, in.read());
311
+ }
312
+ try (InputStream in = createInput("12345\r\n67\r\n89", 11, 13)) {
313
+ assertEquals(2, in.read(buffer));
314
+ assertEquals('8', buffer[0]);
315
+ assertEquals('9', buffer[1]);
316
+ }
317
+
318
+ // \rと\nの間まで
319
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 10)) {
320
+ assertEquals('6', in.read());
321
+ assertEquals('7', in.read());
322
+ assertEquals('\r', in.read());
323
+ assertEquals('\n', in.read());
324
+ assertEquals(-1, in.read());
325
+ }
326
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 10)) {
327
+ assertEquals(4, in.read(buffer));
328
+ assertEquals('6', buffer[0]);
329
+ assertEquals('7', buffer[1]);
330
+ assertEquals('\r', buffer[2]);
331
+ assertEquals('\n', buffer[3]);
332
+ }
333
+
334
+ // \rと\nの間から
335
+ try (InputStream in = createInput("12345\r\n67\r\n89", 10, 13)) {
336
+ assertEquals('8', in.read());
337
+ assertEquals('9', in.read());
338
+ assertEquals(-1, in.read());
339
+ }
340
+ try (InputStream in = createInput("12345\r\n67\r\n89", 10, 13)) {
341
+ assertEquals(2, in.read(buffer));
342
+ assertEquals('8', buffer[0]);
343
+ assertEquals('9', buffer[1]);
344
+ }
345
+
346
+ // \rの直前まで
347
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 9)) {
348
+ assertEquals('6', in.read());
349
+ assertEquals('7', in.read());
350
+ assertEquals('\r', in.read());
351
+ assertEquals('\n', in.read());
352
+ assertEquals(-1, in.read());
353
+ }
354
+ try (InputStream in = createInput("12345\r\n67\r\n89", 4, 9)) {
355
+ assertEquals(4, in.read(buffer));
356
+ assertEquals('6', buffer[0]);
357
+ assertEquals('7', buffer[1]);
358
+ assertEquals('\r', buffer[2]);
359
+ assertEquals('\n', buffer[3]);
360
+ }
361
+
362
+ // \rの直前から
363
+ try (InputStream in = createInput("12345\r\n67\r\n89", 9, 13)) {
364
+ assertEquals('8', in.read());
365
+ assertEquals('9', in.read());
366
+ assertEquals(-1, in.read());
367
+ }
368
+ try (InputStream in = createInput("12345\r\n67\r\n89", 9, 13)) {
369
+ assertEquals(2, in.read(buffer));
370
+ assertEquals('8', buffer[0]);
371
+ assertEquals('9', buffer[1]);
372
+ }
373
+
374
+ // \r\nで終わる(最後まで)
375
+ try (InputStream in = createInput("12345\r\n67\r\n", 5, 11)) {
376
+ assertEquals('6', in.read());
377
+ assertEquals('7', in.read());
378
+ assertEquals('\r', in.read());
379
+ assertEquals('\n', in.read());
380
+ assertEquals(-1, in.read());
381
+ }
382
+ try (InputStream in = createInput("12345\r\n67\r\n", 5, 11)) {
383
+ assertEquals(4, in.read(buffer));
384
+ assertEquals('6', buffer[0]);
385
+ assertEquals('7', buffer[1]);
386
+ assertEquals('\r', buffer[2]);
387
+ assertEquals('\n', buffer[3]);
388
+ }
389
+
390
+ // \r\nで終わる(直前まで)
391
+ try (InputStream in = createInput("12345\r\n67\r\n", 5, 10)) {
392
+ assertEquals('6', in.read());
393
+ assertEquals('7', in.read());
394
+ assertEquals('\r', in.read());
395
+ assertEquals('\n', in.read());
396
+ assertEquals(-1, in.read());
397
+ }
398
+ try (InputStream in = createInput("12345\r\n67\r\n", 5, 10)) {
399
+ assertEquals(4, in.read(buffer));
400
+ assertEquals('6', buffer[0]);
401
+ assertEquals('7', buffer[1]);
402
+ assertEquals('\r', buffer[2]);
403
+ assertEquals('\n', buffer[3]);
404
+ }
405
+ }
406
+
407
+ @Test
408
+ public void testCR() throws IOException
409
+ {
410
+ try (InputStream in = createInput("12345\r67\r89", 0, 4)) {
411
+ assertEquals('1', in.read());
412
+ assertEquals('2', in.read());
413
+ assertEquals('3', in.read());
414
+ assertEquals('4', in.read());
415
+ assertEquals('5', in.read());
416
+ assertEquals('\r', in.read());
417
+ assertEquals(-1, in.read());
418
+ }
419
+ try (InputStream in = createInput("12345\r67\r89", 0, 4)) {
420
+ assertEquals(6, in.read(buffer));
421
+ assertEquals('1', buffer[0]);
422
+ assertEquals('2', buffer[1]);
423
+ assertEquals('3', buffer[2]);
424
+ assertEquals('4', buffer[3]);
425
+ assertEquals('5', buffer[4]);
426
+ assertEquals('\r', buffer[5]);
427
+ }
428
+
429
+ try (InputStream in = createInput("12345\r67\r89", 1, 4)) {
430
+ assertEquals(-1, in.read());
431
+ }
432
+ try (InputStream in = createInput("12345\r67\r89", 1, 4)) {
433
+ assertEquals(-1, in.read(buffer));
434
+ }
435
+
436
+ try (InputStream in = createInput("12345\r67\r89", 1, 7)) {
437
+ assertEquals('6', in.read());
438
+ assertEquals('7', in.read());
439
+ assertEquals('\r', in.read());
440
+ assertEquals(-1, in.read());
441
+ }
442
+ try (InputStream in = createInput("12345\r67\r89", 1, 7)) {
443
+ assertEquals(3, in.read(buffer));
444
+ assertEquals('6', buffer[0]);
445
+ assertEquals('7', buffer[1]);
446
+ assertEquals('\r', buffer[2]);
447
+ }
448
+
449
+ try (InputStream in = createInput("12345\r67\r89", 1, 10)) {
450
+ assertEquals('6', in.read());
451
+ assertEquals('7', in.read());
452
+ assertEquals('\r', in.read());
453
+ assertEquals('8', in.read());
454
+ assertEquals('9', in.read());
455
+ assertEquals(-1, in.read());
456
+ }
457
+ try (InputStream in = createInput("12345\r67\r89", 1, 10)) {
458
+ assertEquals(5, in.read(buffer));
459
+ assertEquals('6', buffer[0]);
460
+ assertEquals('7', buffer[1]);
461
+ assertEquals('\r', buffer[2]);
462
+ assertEquals('8', buffer[3]);
463
+ assertEquals('9', buffer[4]);
464
+ }
465
+
466
+ try (InputStream in = createInput("12345\r67\r89", 10, 11)) {
467
+ assertEquals(-1, in.read());
468
+ }
469
+ try (InputStream in = createInput("12345\r67\r89", 10, 11)) {
470
+ assertEquals(-1, in.read(buffer));
471
+ }
472
+
473
+ // \rの直後まで
474
+ try (InputStream in = createInput("12345\r67\r89", 4, 9)) {
475
+ assertEquals('6', in.read());
476
+ assertEquals('7', in.read());
477
+ assertEquals('\r', in.read());
478
+ assertEquals(-1, in.read());
479
+ }
480
+ try (InputStream in = createInput("12345\r67\r89", 4, 9)) {
481
+ assertEquals(3, in.read(buffer));
482
+ assertEquals('6', buffer[0]);
483
+ assertEquals('7', buffer[1]);
484
+ assertEquals('\r', buffer[2]);
485
+ assertEquals(-1, in.read(buffer));
486
+ }
487
+ try (InputStream in = createInput("12345\r67\r89", 4, 9)) {
488
+ assertEquals(3, in.read(buffer, 0, 3));
489
+ assertEquals('6', buffer[0]);
490
+ assertEquals('7', buffer[1]);
491
+ assertEquals('\r', buffer[2]);
492
+ assertEquals(-1, in.read(buffer));
493
+ }
494
+ // \rの直後から
495
+ try (InputStream in = createInput("12345\r67\r89", 9, 11)) {
496
+ assertEquals('8', in.read());
497
+ assertEquals('9', in.read());
498
+ assertEquals(-1, in.read());
499
+ }
500
+ try (InputStream in = createInput("12345\r67\r89", 9, 11)) {
501
+ assertEquals(2, in.read(buffer));
502
+ assertEquals('8', buffer[0]);
503
+ assertEquals('9', buffer[1]);
504
+ }
505
+
506
+ // \rの直前まで
507
+ try (InputStream in = createInput("12345\r67\r89", 4, 8)) {
508
+ assertEquals('6', in.read());
509
+ assertEquals('7', in.read());
510
+ assertEquals('\r', in.read());
511
+ assertEquals(-1, in.read());
512
+ }
513
+ try (InputStream in = createInput("12345\r67\r89", 4, 8)) {
514
+ assertEquals(3, in.read(buffer));
515
+ assertEquals('6', buffer[0]);
516
+ assertEquals('7', buffer[1]);
517
+ assertEquals('\r', buffer[2]);
518
+ }
519
+
520
+ // \rの直前から
521
+ try (InputStream in = createInput("12345\r67\r89", 8, 11)) {
522
+ assertEquals('8', in.read());
523
+ assertEquals('9', in.read());
524
+ assertEquals(-1, in.read());
525
+ }
526
+ try (InputStream in = createInput("12345\r67\r89", 8, 11)) {
527
+ assertEquals(2, in.read(buffer));
528
+ assertEquals('8', buffer[0]);
529
+ assertEquals('9', buffer[1]);
530
+ }
531
+
532
+ // \rで終わる(最後まで)
533
+ try (InputStream in = createInput("12345\r67\r", 5, 9)) {
534
+ assertEquals('6', in.read());
535
+ assertEquals('7', in.read());
536
+ assertEquals('\r', in.read());
537
+ assertEquals(-1, in.read());
538
+ }
539
+ try (InputStream in = createInput("12345\r67\r", 5, 9)) {
540
+ assertEquals(3, in.read(buffer));
541
+ assertEquals('6', buffer[0]);
542
+ assertEquals('7', buffer[1]);
543
+ assertEquals('\r', buffer[2]);
544
+ }
545
+
546
+ // \rで終わる(直前まで)
547
+ try (InputStream in = createInput("12345\r67\r", 5, 8)) {
548
+ assertEquals('6', in.read());
549
+ assertEquals('7', in.read());
550
+ assertEquals('\r', in.read());
551
+ assertEquals(-1, in.read());
552
+ }
553
+ try (InputStream in = createInput("12345\r67\r", 5, 8)) {
554
+ assertEquals(3, in.read(buffer));
555
+ assertEquals('6', buffer[0]);
556
+ assertEquals('7', buffer[1]);
557
+ assertEquals('\r', buffer[2]);
558
+ }
559
+ }
560
+
561
+ private InputStream createInput(String s, int start, int end)
562
+ {
563
+ try {
564
+ return new PartialFileInputStream(new ByteArrayInputStream(s.getBytes("UTF-8")), start, end);
565
+ } catch (UnsupportedEncodingException e) {
566
+ throw new RuntimeException(e);
567
+ }
568
+ }
569
+
570
+ }