SequenceInputStream changes (#11590)
Co-authored-by: Seshu Thanneeru <seshukumar.thanneeru@thoughtdata.com>
This commit is contained in:
parent
da41d6e4f9
commit
d2752a384e
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.iostreams;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
public class InputSequenceHandler {
|
||||||
|
|
||||||
|
private SequenceInputStream sequenceInputStream;
|
||||||
|
|
||||||
|
public InputSequenceHandler(Vector<InputStream> inputStreams) {
|
||||||
|
sequenceInputStream = new SequenceInputStream(inputStreams.elements());
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputSequenceHandler(String file1, String file2) throws FileNotFoundException {
|
||||||
|
sequenceInputStream = new SequenceInputStream(new FileInputStream(file1), new FileInputStream(file2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputSequenceHandler(List<String> fileNames) throws FileNotFoundException {
|
||||||
|
Vector<InputStream> inputStreams = new Vector<>();
|
||||||
|
|
||||||
|
for (String fileName: fileNames) {
|
||||||
|
inputStreams.add(new FileInputStream(fileName));
|
||||||
|
}
|
||||||
|
sequenceInputStream = new SequenceInputStream(inputStreams.elements());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int read() throws IOException {
|
||||||
|
return sequenceInputStream.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readAsString() throws IOException {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
int readByte;
|
||||||
|
while ((readByte = sequenceInputStream.read()) != -1) {
|
||||||
|
stringBuilder.append((char) readByte);
|
||||||
|
}
|
||||||
|
return stringBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() throws IOException {
|
||||||
|
sequenceInputStream.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
package com.baeldung.iostreams;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.SequenceInputStream;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class InputSequenceUnitTest {
|
||||||
|
|
||||||
|
private static final String FILE1 = "iostreams/File1.txt";
|
||||||
|
private static final String FILE2 = "iostreams/File2.txt";
|
||||||
|
private static final String FILE3 = "iostreams/File3.txt";
|
||||||
|
|
||||||
|
private static final String FILE1_AND_FILE2_CONTENT = "InputSequenceUnitTest";
|
||||||
|
private static final String ALL_FILES_CONTENT = "InputSequenceUnitTest is Success";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoFiles_readAsString() throws URISyntaxException, IOException {
|
||||||
|
String file1 = Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString();
|
||||||
|
String file2 = Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString();
|
||||||
|
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(file1, file2);
|
||||||
|
String stringValue = inputSequenceHandler.readAsString();
|
||||||
|
inputSequenceHandler.close();
|
||||||
|
assertEquals(stringValue, FILE1_AND_FILE2_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFileList_readAsString() throws URISyntaxException, IOException {
|
||||||
|
List<String> filesList = new ArrayList<>();
|
||||||
|
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE1).toURI()).toString());
|
||||||
|
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE2).toURI()).toString());
|
||||||
|
filesList.add(Paths.get(ClassLoader.getSystemResource(FILE3).toURI()).toString());
|
||||||
|
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(filesList);
|
||||||
|
String stringValue = inputSequenceHandler.readAsString();
|
||||||
|
inputSequenceHandler.close();
|
||||||
|
assertEquals(stringValue, ALL_FILES_CONTENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenVectorOfInputStreams_readAsString() throws IOException {
|
||||||
|
String[] strings = {"Testing", "Leads", "to", "failure",
|
||||||
|
"and", "failure", "leads", "to", "understanding"};
|
||||||
|
Vector<InputStream> inputStreamVector = new Vector<>();
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
for (String string: strings) {
|
||||||
|
inputStreamVector.add(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)));
|
||||||
|
stringBuilder.append(string);
|
||||||
|
}
|
||||||
|
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(inputStreamVector);
|
||||||
|
String combinedString = inputSequenceHandler.readAsString();
|
||||||
|
inputSequenceHandler.close();
|
||||||
|
assertEquals(stringBuilder.toString(), combinedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoStrings_readCombinedValue() throws IOException {
|
||||||
|
InputStream first = new ByteArrayInputStream("One".getBytes());
|
||||||
|
InputStream second = new ByteArrayInputStream("Magic".getBytes());
|
||||||
|
SequenceInputStream sequenceInputStream = new SequenceInputStream(first, second);
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
int byteValue;
|
||||||
|
while ((byteValue = sequenceInputStream.read()) != -1) {
|
||||||
|
stringBuilder.append((char) byteValue);
|
||||||
|
}
|
||||||
|
assertEquals("OneMagic", stringBuilder.toString());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
InputSequence
|
|
@ -0,0 +1 @@
|
||||||
|
UnitTest
|
|
@ -0,0 +1 @@
|
||||||
|
is Success
|
Loading…
Reference in New Issue