Browse Source

implement getting InputStream as String

feature-password-validator-2
binsky 3 years ago
parent
commit
238bad93f0
  1. 16
      src/main/java/ResourceApi.java
  2. 16
      src/test/java/ResourceApiTest.java

16
src/main/java/ResourceApi.java

@ -1,4 +1,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
public class ResourceApi { public class ResourceApi {
@ -13,4 +16,17 @@ public class ResourceApi {
return inputStream; return inputStream;
} }
} }
public String getStringFromInputStream(InputStream inputStream) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine + "\n");
}
in.close();
return response.toString();
}
} }

16
src/test/java/ResourceApiTest.java

@ -1,13 +1,10 @@
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
class ResourceApiTest { class ResourceApiTest {
@ -36,4 +33,11 @@ class ResourceApiTest {
assertTrue(response.toString().contains("Alleinherrschaft")); assertTrue(response.toString().contains("Alleinherrschaft"));
} }
@Test
void getStringFromInputStream() throws IOException {
String testString = "I am a test string!\nAnother test line.\n";
InputStream is = new ByteArrayInputStream(testString.getBytes(StandardCharsets.UTF_8));
assertEquals(resourceApi.getStringFromInputStream(is), testString);
}
} }
Loading…
Cancel
Save