/** * Convert the Reader into an InputStream. * <p> * <strong>Note:</strong> This uses the default encoding. * </p> * * @return Derived InputStream * @throws IOException */ @Override public InputStream getInputStream() throws IOException { return new ReaderInputStream(reader); }
@Override public InputStream getByteStream() { return new ReaderInputStream(characterStream, getEncoding()); }
@Test public void testCharsetMismatchInfiniteLoop() throws IOException { // Input is UTF-8 bytes: 0xE0 0xB2 0xA0 final char[] inputChars = new char[] { (char) 0xE0, (char) 0xB2, (char) 0xA0 }; // Charset charset = Charset.forName("UTF-8"); // works final Charset charset = Charset.forName("ASCII"); // infinite loop try (ReaderInputStream stream = new ReaderInputStream(new CharArrayReader(inputChars), charset)) { while (stream.read() != -1) { } } } }
@Benchmark public InputStream apacheReaderInputStream() throws IOException { return new ReaderInputStream(CharSource.wrap(test1).openStream()); }
@Benchmark public InputStream apacheReaderInputStream() throws IOException { return new ReaderInputStream(CharSource.wrap(test1).openStream()); }
private void testWithSingleByteRead(final String testString, final String charsetName) throws IOException { final byte[] bytes = testString.getBytes(charsetName); final ReaderInputStream in = new ReaderInputStream(new StringReader(testString), charsetName); for (final byte b : bytes) { final int read = in.read(); assertTrue(read >= 0); assertTrue(read <= 255); assertEquals(b, (byte)read); } assertEquals(-1, in.read()); in.close(); }
@SuppressWarnings("deprecation") @Test public void testReadZero() throws Exception { final String inStr = "test"; final ReaderInputStream r = new ReaderInputStream(new StringReader(inStr)); final byte[] bytes = new byte[30]; assertEquals(0, r.read(bytes, 0, 0)); assertEquals(inStr.length(), r.read(bytes, 0, inStr.length()+1)); // Should always return 0 for length == 0 assertEquals(0, r.read(bytes, 0, 0)); r.close(); }
private void testWithBufferedRead(final String testString, final String charsetName) throws IOException { final byte[] expected = testString.getBytes(charsetName); final ReaderInputStream in = new ReaderInputStream(new StringReader(testString), charsetName); final byte[] buffer = new byte[128]; int offset = 0; while (true) { int bufferOffset = random.nextInt(64); final int bufferLength = random.nextInt(64); int read = in.read(buffer, bufferOffset, bufferLength); if (read == -1) { assertEquals(offset, expected.length); break; } else { assertTrue(read <= bufferLength); while (read > 0) { assertTrue(offset < expected.length); assertEquals(expected[offset], buffer[bufferOffset]); offset++; bufferOffset++; read--; } } } in.close(); }
@SuppressWarnings("deprecation") @Test public void testReadZeroEmptyString() throws Exception { final ReaderInputStream r = new ReaderInputStream(new StringReader("")); final byte[] bytes = new byte[30]; // Should always return 0 for length == 0 assertEquals(0, r.read(bytes, 0, 0)); assertEquals(-1, r.read(bytes, 0, 1)); assertEquals(0, r.read(bytes, 0, 0)); assertEquals(-1, r.read(bytes, 0, 1)); r.close(); }
ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset());
/** * Build input stream resource from string value. * * @param value the value * @param description the description * @return the input stream resource */ public static InputStreamResource buildInputStreamResourceFrom(final String value, final String description) { val reader = new StringReader(value); val is = new ReaderInputStream(reader, StandardCharsets.UTF_8); return new InputStreamResource(is, description); }
static public Stream createStream(final String content) { if (content == null) { return null; } String charset = StandardCharsets.UTF_8.name(); return createStream(new ReaderInputStream(new StringReader(content), charset), "text/plain; charset="+charset, -1); }
@Override public void parse(Reader reader, String baseURI) throws IOException, RDFParseException, RDFHandlerException { parse(new ReaderInputStream(reader), baseURI); }
@Override public void parse(Reader reader, String baseURI) throws IOException, RDFParseException, RDFHandlerException { parse(new ReaderInputStream(reader), baseURI); }
@Override public InputStream toAsciiStream(OptimizedElementArray row, int columnPosition, LobService lobService) throws SQLException { return new ReaderInputStream(new StringReader(toString(row, columnPosition)), StandardCharsets.US_ASCII); } @Override
@Override public Properties parse(final Reader reader) throws IOException { final InputStream in = new ReaderInputStream(reader, getFormat() .getCharset()); try { final Properties p = new Properties(); p.loadFromXML(in); return p; } finally { in.close(); } }
public static Resource readFromString(String input) throws IOException { StringReader stringReader = new StringReader(input); ReaderInputStream is = new ReaderInputStream(stringReader, Charset.forName("UTF-8")); Resource resource = readFromStream(is); is.close(); return resource; }
@RequestMapping(value = "/store/{elementId:.+}/metadataContent", method = RequestMethod.GET) public void metadataContent(@PathVariable("elementId") final String elementId, final Model model, final HttpServletRequest request, final HttpServletResponse response, final OutputStream output) { String xml = storeModificationHelper.fetchMetadataAsXml(elementId); try (InputStream inputStream = new ReaderInputStream(new StringReader(xml))) { sendResponse(output, inputStream); } catch (final ServiceException | IOException e) { log.error(e.getMessage(), e); notificationService.publishLocalizedNotification(NotificationLevel.ERROR, "msg.store.element.fetch.error"); } }
@Override public void write(Resource resource, String input) throws ResourceIOException { try (InputStream in = new ReaderInputStream(new StringReader(input), Charset.forName("UTF-8"))) { write(resource, in); } catch (IOException ex) { String msg = "Could not write data to uri " + String.valueOf(resource.getUri()); LOGGER.error(msg, ex); throw new ResourceIOException(msg, ex); } }
private void validateResult( String result ) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); StringReader stringReader = new StringReader( result ); documentBuilder.parse( new ReaderInputStream( stringReader ) ); } catch ( ParserConfigurationException | SAXException | IOException e ) { assertTrue( "unexpected exception", false ); } } }