/** * Returns buffered writer. Buffer will be created if not already used. */ @Override public PrintWriter getWriter() { if (writer == null) { writer = new FastCharArrayWriter(); printWriter = new PrintWriter(writer); } return printWriter; }
/** * Returns new {@link FastCharArrayWriter} using default IO buffer size. * * @return new {@link FastCharArrayWriter} using default IO buffer size. */ private static FastCharArrayWriter createFastCharArrayWriter() { return new FastCharArrayWriter(bufferSize()); }
public static char[] readChars(Reader input, int charCount) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, charCount); return output.toCharArray(); }
public static char[] readChars(InputStream input, int charCount) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, charCount); return output.toCharArray(); }
public static char[] readChars(Reader input) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output); return output.toCharArray(); } public static char[] readChars(Reader input, int charCount) throws IOException {
public static char[] readChars(InputStream input) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output); return output.toCharArray(); } public static char[] readChars(InputStream input, int charCount) throws IOException {
public static char[] readChars(InputStream input, String encoding) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, encoding); return output.toCharArray(); } public static char[] readChars(InputStream input, String encoding, int charCount) throws IOException {
public static char[] readChars(InputStream input, String encoding, int charCount) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, encoding, charCount); return output.toCharArray(); }
/** * Loads properties from input stream. Stream is not closed at the end. */ public Props load(final InputStream in) throws IOException { final Writer out = new FastCharArrayWriter(); StreamUtil.copy(in, out); parse(out.toString()); return this; }
/** * Loads properties from input stream and provided encoding. * Stream is not closed at the end. */ public Props load(final InputStream in, final String encoding) throws IOException { final Writer out = new FastCharArrayWriter(); StreamUtil.copy(in, out, encoding); parse(out.toString()); return this; }
/** * Reads file content as string encoded in provided encoding. * For UTF encoded files, detects optional BOM characters. */ public static String readString(File file, String encoding) throws IOException { if (!file.exists()) { throw new FileNotFoundException(MSG_NOT_FOUND + file); } if (!file.isFile()) { throw new IOException(MSG_NOT_A_FILE + file); } long len = file.length(); if (len >= Integer.MAX_VALUE) { len = Integer.MAX_VALUE; } InputStream in = null; try { in = new FileInputStream(file); if (encoding.startsWith("UTF")) { in = new UnicodeInputStream(in, encoding); } FastCharArrayWriter out = new FastCharArrayWriter((int) len); StreamUtil.copy(in, out, encoding); return out.toString(); } finally { StreamUtil.close(in); } }
/** * Renders tag body to char array. */ public static char[] renderBody(final JspFragment body) throws JspException { FastCharArrayWriter writer = new FastCharArrayWriter(); invokeBody(body, writer); return writer.toCharArray(); }
/** * Initialize processor by creating new Lagarto and root TagWriter. */ public void init(final char[] content) { // create Lagarto lagartoParser = new LagartoParser(content); // prepare root tag writer fastCharArrayWriter = new FastCharArrayWriter(); tagWriter = new TagWriter(fastCharArrayWriter); }
/** * Detects optional BOM and reads UTF string from a file. * If BOM is missing, UTF-8 is assumed. * @see UnicodeInputStream */ public static String readUTFString(File file) throws IOException { if (!file.exists()) { throw new FileNotFoundException(MSG_NOT_FOUND + file); } if (!file.isFile()) { throw new IOException(MSG_NOT_A_FILE + file); } long len = file.length(); if (len >= Integer.MAX_VALUE) { len = Integer.MAX_VALUE; } UnicodeInputStream in = null; try { in = new UnicodeInputStream(new FileInputStream(file), null); FastCharArrayWriter out = new FastCharArrayWriter((int) len); String encoding = in.getDetectedEncoding(); if (encoding == null) { encoding = StringPool.UTF_8; } StreamUtil.copy(in, out, encoding); return out.toString(); } finally { StreamUtil.close(in); } }
protected String readDataFile(String fileName) throws IOException { String dataFolder = this.getClass().getPackage().getName() + ".data."; dataFolder = dataFolder.replace('.', '/'); InputStream is = ClassLoaderUtil.getResourceAsStream(dataFolder + fileName); Writer out = new FastCharArrayWriter(); String encoding = "UTF-8"; if (fileName.endsWith(".properties")) { encoding = "ISO-8859-1"; } StreamUtil.copy(is, out, encoding); StreamUtil.close(is); return out.toString(); }
/** * Detects optional BOM and reads UTF string from an input stream. * If BOM is missing, UTF-8 is assumed. */ public static String readUTFString(InputStream inputStream) throws IOException { UnicodeInputStream in = null; try { in = new UnicodeInputStream(inputStream, null); FastCharArrayWriter out = new FastCharArrayWriter(); String encoding = in.getDetectedEncoding(); if (encoding == null) { encoding = StringPool.UTF_8; } StreamUtil.copy(in, out, encoding); return out.toString(); } finally { StreamUtil.close(in); } }
@Test void testWriteTo() throws IOException { FastCharArrayWriter fcaw = new FastCharArrayWriter(2); fcaw.write("Hello"); fcaw.write(' '); fcaw.write("World"); fcaw.write('!'); StringWriter sw = new StringWriter(); fcaw.writeTo(sw); assertEquals("Hello World!", sw.toString()); }
@Test void testFcat() throws IOException { FastCharArrayWriter fcaw = new FastCharArrayWriter(); fcaw.write(65); fcaw.write(new char[]{'a', 'z', 'r'}); fcaw.write(new char[]{'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}, 4, 3); char[] result = fcaw.toCharArray(); char[] expected = new char[]{'A', 'a', 'z', 'r', 'g', 'h', 'j'}; assertTrue(Arrays.equals(expected, result)); }
@Test void testFcatSingle() throws IOException { FastCharArrayWriter fcaw = new FastCharArrayWriter(); fcaw.write(73); fcaw.write(74); fcaw.write(75); fcaw.write(76); fcaw.write(77); char[] result = fcaw.toCharArray(); char[] expected = new char[]{73, 74, 75, 76, 77}; assertTrue(Arrays.equals(expected, result)); }
@Test void testAppendable() { LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder(); Document document = lagartoDOMBuilder.parse("<div foo=\"123\">some <b>nice</b> text</div>"); Element div = (Element) document.getFirstChild(); String textContent = div.getTextContent(); StringBuilder stringBuilder = new StringBuilder(); div.appendTextContent(stringBuilder); assertEquals(textContent, stringBuilder.toString()); FastCharArrayWriter charBuffer = new FastCharArrayWriter(); div.appendTextContent(charBuffer); assertEquals(textContent, charBuffer.toString()); }