Skip to content

Commit 0eed4e1

Browse files
SeshuTechieSeshu Thanneeru
andauthored
SequenceInputStream changes (eugenp#11590)
Co-authored-by: Seshu Thanneeru <[email protected]>
1 parent 66d8db8 commit 0eed4e1

5 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.iostreams;
2+
3+
import java.io.*;
4+
import java.util.List;
5+
import java.util.Vector;
6+
7+
public class InputSequenceHandler {
8+
9+
private SequenceInputStream sequenceInputStream;
10+
11+
public InputSequenceHandler(Vector<InputStream> inputStreams) {
12+
sequenceInputStream = new SequenceInputStream(inputStreams.elements());
13+
}
14+
15+
public InputSequenceHandler(String file1, String file2) throws FileNotFoundException {
16+
sequenceInputStream = new SequenceInputStream(new FileInputStream(file1), new FileInputStream(file2));
17+
}
18+
19+
public InputSequenceHandler(List<String> fileNames) throws FileNotFoundException {
20+
Vector<InputStream> inputStreams = new Vector<>();
21+
22+
for (String fileName: fileNames) {
23+
inputStreams.add(new FileInputStream(fileName));
24+
}
25+
sequenceInputStream = new SequenceInputStream(inputStreams.elements());
26+
}
27+
28+
29+
public int read() throws IOException {
30+
return sequenceInputStream.read();
31+
}
32+
33+
public String readAsString() throws IOException {
34+
StringBuilder stringBuilder = new StringBuilder();
35+
int readByte;
36+
while ((readByte = sequenceInputStream.read()) != -1) {
37+
stringBuilder.append((char) readByte);
38+
}
39+
return stringBuilder.toString();
40+
}
41+
42+
public void close() throws IOException {
43+
sequenceInputStream.close();
44+
}
45+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.iostreams;
2+
3+
import org.junit.Test;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.SequenceInputStream;
9+
import java.net.URISyntaxException;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Paths;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.Vector;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
18+
public class InputSequenceUnitTest {
19+
20+
private static final String FILE1 = "iostreams/File1.txt";
21+
private static final String FILE2 = "iostreams/File2.txt";
22+
private static final String FILE3 = "iostreams/File3.txt";
23+
24+
private static final String FILE1_AND_FILE2_CONTENT = "InputSequenceUnitTest";
25+
private static final String ALL_FILES_CONTENT = "InputSequenceUnitTest is Success";
26+
27+
@Test
28+
public void givenTwoFiles_readAsString() throws URISyntaxException, IOException {
29+
String file1 = Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString();
30+
String file2 = Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString();
31+
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(file1, file2);
32+
String stringValue = inputSequenceHandler.readAsString();
33+
inputSequenceHandler.close();
34+
assertEquals(stringValue, FILE1_AND_FILE2_CONTENT);
35+
}
36+
37+
@Test
38+
public void givenFileList_readAsString() throws URISyntaxException, IOException {
39+
List<String> filesList = new ArrayList<>();
40+
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString());
41+
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString());
42+
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE3).toURI()).toString());
43+
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(filesList);
44+
String stringValue = inputSequenceHandler.readAsString();
45+
inputSequenceHandler.close();
46+
assertEquals(stringValue, ALL_FILES_CONTENT);
47+
}
48+
49+
@Test
50+
public void givenVectorOfInputStreams_readAsString() throws IOException {
51+
String[] strings = {"Testing", "Leads", "to", "failure",
52+
"and", "failure", "leads", "to", "understanding"};
53+
Vector<InputStream> inputStreamVector = new Vector<>();
54+
StringBuilder stringBuilder = new StringBuilder();
55+
for (String string: strings) {
56+
inputStreamVector.add(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)));
57+
stringBuilder.append(string);
58+
}
59+
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(inputStreamVector);
60+
String combinedString = inputSequenceHandler.readAsString();
61+
inputSequenceHandler.close();
62+
assertEquals(stringBuilder.toString(), combinedString);
63+
}
64+
65+
@Test
66+
public void givenTwoStrings_readCombinedValue() throws IOException {
67+
InputStream first = new ByteArrayInputStream("One".getBytes());
68+
InputStream second = new ByteArrayInputStream("Magic".getBytes());
69+
SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second);
70+
StringBuilder stringBuilder = new StringBuilder();
71+
int byteValue;
72+
while ((byteValue = sequenceInputStream.read()) != -1) {
73+
stringBuilder.append((char) byteValue);
74+
}
75+
assertEquals("OneMagic", stringBuilder.toString());
76+
}
77+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
InputSequence
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
UnitTest
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
is Success

0 commit comments

Comments
 (0)