public static byte[] readTextUrl(URL source, String encoding) throws IOException { byte[] result; try { URLConnection urlc = source.openConnection(); StringBuffer sb = new StringBuffer(1024); InputStream input = urlc.getInputStream(); UnicodeReader reader = new UnicodeReader(input, encoding); try { char[] cbuf = new char[32]; int r; while ((r = reader.read(cbuf, 0, 32)) != -1) { sb.append(cbuf, 0, r); } result = sb.toString().getBytes(reader.getEncoding()); } finally { reader.close(); input.close(); } } catch (IOException e) { logger.error("Can't read '" + source.getFile() + "'."); throw e; } return result; }
public static byte[] readTextFile(File source, String encoding) throws IOException { byte[] result; try { StringBuffer sb = new StringBuffer(1024); FileInputStream input = new FileInputStream(source); UnicodeReader reader = new UnicodeReader(input, encoding); try { char[] cbuf = new char[32]; int r; while ((r = reader.read(cbuf, 0, 32)) != -1) { sb.append(cbuf, 0, r); } result = sb.toString().getBytes(reader.getEncoding()); } finally { reader.close(); input.close(); } } catch (IOException e) { logger.error("Can't read '" + source.getAbsolutePath() + "'."); throw e; } return result; }