/** * 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); }
@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(); }
/** * Read a single byte. * * @return either the byte read or <code>-1</code> if the end of the stream * has been reached * @throws IOException if an I/O error occurs */ @Override public int read() throws IOException { for (;;) { if (encoderOut.hasRemaining()) { return encoderOut.get() & 0xFF; } fillBuffer(); if (endOfInput && !encoderOut.hasRemaining()) { return EOF; } } }
@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) { } } } }
ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset()); isr.close();
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
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; }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(final byte[] b) throws IOException { return read(b, 0, b.length); }
@Override public InputStream getByteStream() { return new ReaderInputStream(characterStream, getEncoding()); }
@Override public Map<LocaleId, List<GlossaryEntry>> extractGlossary(Reader reader, String qualifiedName) throws IOException { ReaderInputStream ris = new ReaderInputStream(reader); try { InputSource potInputSource = new InputSource(ris); potInputSource.setEncoding("utf8"); return extractTemplate(potInputSource, qualifiedName); } finally { ris.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(); }
read += c; } else { fillBuffer(); if (endOfInput && !encoderOut.hasRemaining()) { break;
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
@Benchmark public InputStream apacheReaderInputStream() throws IOException { return new ReaderInputStream(CharSource.wrap(test1).openStream()); }
@Override public Map<LocaleId, List<GlossaryEntry>> extractGlossary(Reader reader, String qualifiedName) throws IOException { ReaderInputStream ris = new ReaderInputStream(reader); try { InputSource potInputSource = new InputSource(ris); potInputSource.setEncoding("utf8"); return extractTemplate(potInputSource, qualifiedName); } finally { ris.close(); } }
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(); }
/** * Read a single byte. * * @return either the byte read or <code>-1</code> if the end of the stream * has been reached * @throws IOException if an I/O error occurs */ @Override public int read() throws IOException { for (;;) { if (encoderOut.hasRemaining()) { return encoderOut.get() & 0xFF; } fillBuffer(); if (endOfInput && !encoderOut.hasRemaining()) { return EOF; } } }
/** * Read the specified number of bytes into an array. * * @param b the byte array to read into * @return the number of bytes read or <code>-1</code> * if the end of the stream has been reached * @throws IOException if an I/O error occurs */ @Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
@Benchmark public InputStream apacheReaderInputStream() throws IOException { return new ReaderInputStream(CharSource.wrap(test1).openStream()); }
public org.zkoss.idom.Document parse(Reader reader) throws Exception { ReaderInputStream inputStream = null; try { if (log.isDebugEnabled()) log.debug("Parsing reader: [" + reader + "]"); inputStream = new ReaderInputStream(reader); return convertToIDOM(Zsoup.parse(inputStream, "UTF-8", null, Parser.xhtmlParser())); } catch (UiExceptionX ue) { String lineNumber = getLineNumber(reader, ue.getKeyword()); if (lineNumber != null) throw new UiException(ue.getMessage() + lineNumber); else throw ue; } finally { if (inputStream != null) inputStream.close(); } }