@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 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(); }
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(); }
isr.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; }
@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(); } }
@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(); } }
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(); } }