/** * Converts the specified CharSequence to an input stream, encoded as bytes * using the specified character encoding. * * @param input the CharSequence to convert * @param encoding the encoding to use, null means platform default * @return an input stream * @since 2.3 */ public static InputStream toInputStream(final CharSequence input, final Charset encoding) { return toInputStream(input.toString(), encoding); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toInputStream(String)} */ @Deprecated public static InputStream toInputStream(String input) { return org.apache.commons.io.IOUtils.toInputStream(input); }
/** * @deprecated Use instead {@link org.apache.commons.io.IOUtils#toInputStream(String, String)} */ @Deprecated public static InputStream toInputStream(String input, String encoding) throws IOException { return org.apache.commons.io.IOUtils.toInputStream(input, encoding); }
/** * Converts the specified string to an input stream, encoded as bytes * using the default character encoding of the platform. * * @param input the string to convert * @return an input stream * @since 1.1 * @deprecated 2.5 use {@link #toInputStream(String, Charset)} instead */ @Deprecated public static InputStream toInputStream(final String input) { return toInputStream(input, Charset.defaultCharset()); }
/** * Converts the specified CharSequence to an input stream, encoded as bytes * using the default character encoding of the platform. * * @param input the CharSequence to convert * @return an input stream * @since 2.0 * @deprecated 2.5 use {@link #toInputStream(CharSequence, Charset)} instead */ @Deprecated public static InputStream toInputStream(final CharSequence input) { return toInputStream(input, Charset.defaultCharset()); }
/** * Converts the specified CharSequence to an input stream, encoded as bytes * using the specified character encoding. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * * @param input the CharSequence to convert * @param encoding the encoding to use, null means platform default * @return an input stream * @throws IOException if the encoding is invalid * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 2.0 */ public static InputStream toInputStream(final CharSequence input, final String encoding) throws IOException { return toInputStream(input, Charsets.toCharset(encoding)); }
/** * Send out a POST-HTTP request. Errors will be logged, returned values just ignored. * * @param url the URL to be used for the POST request. * @param contentType the content type of the given <code>content</code> * @param content the content to be send to the given <code>url</code> or * <code>null</code> if no content should be send. * @return the response body or <code>NULL</code> when the request went wrong */ static public String sendHttpPostRequest(String url, String contentType, String content) { return HttpUtil.executeUrl("POST", url, IOUtils.toInputStream(content), contentType, 1000); }
@Benchmark public InputStream apacheToInputStream() throws IOException { return IOUtils.toInputStream(test1, StandardCharsets.UTF_8); }
public TestInputStreamWithMeta(long version) { final String DEFAULT_DATA = "some test data for my input stream"; iostream = IOUtils.toInputStream(DEFAULT_DATA); this.version = version; this.fileLength = DEFAULT_DATA.length(); }
@Override public Boolean call() throws Exception { for (int i = 0; i < NUM_EVENTS; ++i) { final InputStream inputStream = IOUtils.toInputStream(inputRow, StandardCharsets.UTF_8); firehose.addAll(inputStream, req); inputStream.close(); } return true; } }
/** * Test for {@link IOUtils#toInputStream(CharSequence)} and {@link IOUtils#toInputStream(CharSequence, String)}. * Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on * {@link #testToByteArray_InputStream()} to ensure this method functions correctly. * * @throws Exception * on error */ @Test public void testToInputStream_CharSequence() throws Exception { final CharSequence csq = new StringBuilder("Abc123Xyz!"); InputStream inStream = IOUtils.toInputStream(csq); // deliberately testing deprecated method byte[] bytes = IOUtils.toByteArray(inStream); assertEqualContent(csq.toString().getBytes(), bytes); inStream = IOUtils.toInputStream(csq, (String) null); bytes = IOUtils.toByteArray(inStream); assertEqualContent(csq.toString().getBytes(), bytes); inStream = IOUtils.toInputStream(csq, "UTF-8"); bytes = IOUtils.toByteArray(inStream); assertEqualContent(csq.toString().getBytes("UTF-8"), bytes); }
/** * Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String, String)}. Note, this test * utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on * {@link #testToByteArray_InputStream()} to ensure this method functions correctly. * * @throws Exception * on error */ @Test public void testToInputStream_String() throws Exception { final String str = "Abc123Xyz!"; InputStream inStream = IOUtils.toInputStream(str); byte[] bytes = IOUtils.toByteArray(inStream); assertEqualContent(str.getBytes(), bytes); inStream = IOUtils.toInputStream(str, (String) null); bytes = IOUtils.toByteArray(inStream); assertEqualContent(str.getBytes(), bytes); inStream = IOUtils.toInputStream(str, "UTF-8"); bytes = IOUtils.toByteArray(inStream); assertEqualContent(str.getBytes("UTF-8"), bytes); }
@Test public void return_content_of_folder_of_installed_core_extension_over_installed_plugin_in_case_of_key_conflict() throws Exception { system.coreExtensionStream = IOUtils.toInputStream("bar of plugin"); when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true); system.coreExtensionStream = IOUtils.toInputStream("bar of core extension"); when(coreExtensionRepository.isInstalled("samekey")).thenReturn(true); Response response = call("/static/samekey/foo/bar.txt"); assertThat(response.isSuccessful()).isTrue(); assertThat(response.body().string()).isEqualTo("bar of core extension"); assertThat(system.pluginResource).isNull(); assertThat(system.coreExtensionResource).isEqualTo("static/foo/bar.txt"); }
@Test public void testMissingProducerSequence() throws IOException { setUpRequestExpectations("producer", null); final InputStream inputStream = IOUtils.toInputStream(inputRow, StandardCharsets.UTF_8); final Response response = firehose.addAll(inputStream, req); Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); inputStream.close(); EasyMock.verify(req); firehose.close(); }
@Test public void testNaNProducerSequence() throws IOException { setUpRequestExpectations("producer", "foo"); final InputStream inputStream = IOUtils.toInputStream(inputRow, StandardCharsets.UTF_8); final Response response = firehose.addAll(inputStream, req); Assert.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); inputStream.close(); EasyMock.verify(req); firehose.close(); }
@Test public void shouldPerformPluginXsdValidationAndFailWhenIDIsNotPresent() throws Exception { InputStream pluginXml = IOUtils.toInputStream("<go-plugin version=\"1\"></go-plugin>"); try { GoPluginDescriptorParser.parseXML(pluginXml, "/tmp/", new File("/tmp/"), true); fail("xsd validation should have failed"); } catch (SAXException e) { assertThat(e.getMessage(), is("XML Schema validation of Plugin Descriptor(plugin.xml) failed")); } }
@Test public void shouldPerformPluginXsdValidationAndFailWhenVersionIsNotPresent() throws Exception { InputStream pluginXml = IOUtils.toInputStream("<go-plugin id=\"some\"></go-plugin>"); try { GoPluginDescriptorParser.parseXML(pluginXml, "/tmp/", new File("/tmp/"), true); fail("xsd validation should have failed"); } catch (SAXException e) { assertThat(e.getMessage(), is("XML Schema validation of Plugin Descriptor(plugin.xml) failed")); } }
@Test public void fail_on_invalid_rule_parameter_type() { expectedException.expect(IllegalStateException.class); load(IOUtils.toInputStream("<rules><rule><key>foo</key><name>Foo</name><param><key>key</key><type>INVALID</type></param></rule></rules>"), StandardCharsets.UTF_8.name()); }
@Test public void testLowProducerSequence() throws IOException { for (int i = 0; i < NUM_EVENTS; ++i) { setUpRequestExpectations("producer", "1"); final InputStream inputStream = IOUtils.toInputStream(inputRow, StandardCharsets.UTF_8); final Response response = firehose.addAll(inputStream, req); Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); Assert.assertEquals(1, firehose.getCurrentBufferSize()); inputStream.close(); } EasyMock.verify(req); firehose.close(); }
@Test public void shouldValidatePluginVersion() throws Exception { InputStream pluginXml = IOUtils.toInputStream("<go-plugin version=\"10\"></go-plugin>"); try { GoPluginDescriptorParser.parseXML(pluginXml, "/tmp/", new File("/tmp/"), true); fail("xsd validation should have failed"); } catch (SAXException e) { assertThat(e.getMessage(), is("XML Schema validation of Plugin Descriptor(plugin.xml) failed")); assertThat(e.getCause().getMessage(), contains("Value '10' of attribute 'version' of element 'go-plugin' is not valid")); } } }