public static char[] readChars(InputStream input, int charCount) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, charCount); return output.toCharArray(); }
/** * 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); } }
@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)); }
/** * 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; }
/** * Get the underlying character array or <code>null</code> if * writer not used. */ public char[] toCharArray() { if (writer == null) { return null; } return writer.toCharArray(); }
/** * 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(); }
@Override public void write(String s, int off, int len) { write(s.toCharArray(), off, len); }
/** * Returns a writer. */ public PrintWriter getWriter() { if (outWriter == null) { if (outStream != null) { throw new IllegalStateException("Can't call getWriter() after getOutputStream()"); } bufferedWriter = new FastCharArrayWriter(); outWriter = new PrintWriter(bufferedWriter) { @Override public void close() { // do not close the print writer after rendering // since it will remove reference to bufferedWriter } }; } return outWriter; }
/** * Returns buffered writer content as char array. * Returns <code>null</code> if writer is not used. */ public char[] toCharArray() { if (bufferedWriter != null) { return bufferedWriter.toCharArray(); } return null; }
/** * 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); } }
@Override public void write(final String s, final int off, final int len) { write(s.toCharArray(), off, len); }
public static char[] readChars(Reader input, int charCount) throws IOException { FastCharArrayWriter output = new FastCharArrayWriter(); copy(input, output, charCount); return output.toCharArray(); }
/** * 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); } }
/** * 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()); }
@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)); }
/** * @see java.io.CharArrayWriter#toString() */ @Override public String toString() { return new String(toCharArray()); } }
/** * 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); } }
@Override public void write(final String s, final int off, final int len) { write(s.toCharArray(), off, len); }
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 {