/** * Get a String representation of the entire buffer. * <p> * <b>Do not</b> call this method multiple times on the same wrapper as * new string is created every time. */ @Override public String toString() { if (writer == null) { return StringPool.EMPTY; } return writer.toString(); }
/** * 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); } }
/** * 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); } }
/** * 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); } }
/** * Detects optional BOM and reads UTF {@link String} from a {@link File}. * If BOM is missing, UTF-8 is assumed. * * @param file {@link File} to read. * @return String in UTF encoding. * @throws IOException if copy to {@link InputStream} errors. * @see #unicodeInputStreamOf(File) * @see StreamUtil#copy(InputStream, String) */ public static String readUTFString(final File file) throws IOException { UnicodeInputStream in = unicodeInputStreamOf(file); try { return StreamUtil.copy(in, detectEncoding(in)).toString(); } finally { StreamUtil.close(in); } }
/** * Detects optional BOM and reads UTF {@link String} from an {@link InputStream}. * If BOM is missing, UTF-8 is assumed. * * @param inputStream {@link InputStream} to read. * @return String in UTF encoding. * @throws IOException if copy to {@link InputStream} errors. * @see #unicodeInputStreamOf(File) * @see StreamUtil#copy(InputStream, String) */ public static String readUTFString(final InputStream inputStream) throws IOException { UnicodeInputStream in = null; try { in = new UnicodeInputStream(inputStream, null); return StreamUtil.copy(in, detectEncoding(in)).toString(); } finally { StreamUtil.close(in); } }
/** * Reads {@link File} content as {@link String} encoded in provided encoding. * For UTF encoded files, detects optional BOM characters. * * @param file {@link File} to read. * @param encoding Encoding to use. * @return String representing {@link File} content. * @throws IOException if copy to {@link InputStream} errors. * @see #streamOf(File, String) * @see StreamUtil#copy(InputStream, String) */ public static String readString(final File file, final String encoding) throws IOException { checkExists(file); checkIsFile(file); InputStream in = streamOf(file, encoding); try { return StreamUtil.copy(in, encoding).toString(); } finally { StreamUtil.close(in); } }
@Test void testBigRequest() throws IOException { InputStream inputStream = ClassLoaderUtil.getResourceAsStream("/jodd/http/answer.json"); FastCharArrayWriter writter = StreamUtil.copy(inputStream); String body = writter.toString(); HttpRequest httpRequest = HttpRequest.get("").body(body); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); httpRequest.sendTo(outputStream); String receivedBody = outputStream.toString(); int ndx = receivedBody.indexOf("{"); receivedBody = receivedBody.substring(ndx); assertEquals(body, receivedBody); }
@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()); }
@Test void testDecoraParser() throws IOException { DecoraParser decoraParser = new DecoraParser(); FindFile ff = new WildcardFindFile().include("*.*ml"); ff.matchType(FindFile.Match.NAME); ff.searchPath(testDataRoot); File file; while ((file = ff.nextFile()) != null) { System.out.println("+" + file.getName()); char[] page = FileUtil.readString(file).toCharArray(); String decoratorFileName = StringUtil.replace(file.getAbsolutePath(), ".html", "-decora.htm"); char[] decorator = FileUtil.readString(decoratorFileName).toCharArray(); FastCharArrayWriter writer = new FastCharArrayWriter(); decoraParser.decorate(writer, page, decorator); String out = writer.toString(); String outFileName = StringUtil.replace(file.getAbsolutePath(), ".html", "-out.htm"); String outExpected = FileUtil.readString(outFileName); assertEquals(trimLines(outExpected), trimLines(out)); } }
/** * Detects optional BOM and reads UTF {@link String} from a {@link File}. * If BOM is missing, UTF-8 is assumed. * * @param file {@link File} to read. * @return String in UTF encoding. * @throws IOException if copy to {@link InputStream} errors. * @see #unicodeInputStreamOf(File) * @see StreamUtil#copy(InputStream, String) */ public static String readUTFString(final File file) throws IOException { UnicodeInputStream in = unicodeInputStreamOf(file); try { return StreamUtil.copy(in, detectEncoding(in)).toString(); } finally { StreamUtil.close(in); } }
/** * Detects optional BOM and reads UTF {@link String} from an {@link InputStream}. * If BOM is missing, UTF-8 is assumed. * * @param inputStream {@link InputStream} to read. * @return String in UTF encoding. * @throws IOException if copy to {@link InputStream} errors. * @see #unicodeInputStreamOf(File) * @see StreamUtil#copy(InputStream, String) */ public static String readUTFString(final InputStream inputStream) throws IOException { UnicodeInputStream in = null; try { in = new UnicodeInputStream(inputStream, null); return StreamUtil.copy(in, detectEncoding(in)).toString(); } finally { StreamUtil.close(in); } }
/** * Reads {@link File} content as {@link String} encoded in provided encoding. * For UTF encoded files, detects optional BOM characters. * * @param file {@link File} to read. * @param encoding Encoding to use. * @return String representing {@link File} content. * @throws IOException if copy to {@link InputStream} errors. * @see #streamOf(File, String) * @see StreamUtil#copy(InputStream, String) */ public static String readString(final File file, final String encoding) throws IOException { checkExists(file); checkIsFile(file); InputStream in = streamOf(file, encoding); try { return StreamUtil.copy(in, encoding).toString(); } finally { StreamUtil.close(in); } }